Play with ESP32 + Arduino (25) SSD1306 library to drive OLED

tags: android  json  arduino  Applets  opengl

This time we used the following libraries:

Compared with the U8G2 library, this library has a lot less functions, and the relative RAM and ROM occupancy is also less. The drawing progress bar is very bright

Still connected to the hardware IIC SCL: 22 SDA: 21

1. How to use the library

  1. Import library (mine is IIC interface)
#include "SSD1306Wire.h"
  1. Instantiate an SSD1306Wire object
SSD1306Wire display(0x3c, 21, 22);
  1. Initialization screen
display.init();
  1. Display and clear
display.clear();
display.display();

2. Related API

1. Clear the screen, clear the display buf area, display.clear

void OLEDDisplay::clear()
display.clear();

2. Clear a point display.clearPixel

void OLEDDisplay::clearPixel(int16_t x, int16_t y)
display.clearPixel(0,0);

3. Display, display the content of the buf area display.display

void SSD1306Wire::display()
display.display();

4. Turn off the display display.displayOff();

5. Turn on the display display.displayOn();

6. Restore display.allocateBuffer() after deep sleep;

//Use it to resume after deep sleep without resetting the display (what init() will do).
//If the connection with the display has been established and the buffer has been allocated, it returns true, otherwise it returns false.

display.allocateBuffer();

7. Close the OLED, clear the object and cache display.end();

void OLEDDisplay::end()

8. Flip the screen vertically display.flipScreenVertically();

display.flipScreenVertically();

9. Screen mirroring display display.mirrorScreen();

display.mirrorScreen();

10. Invert Display display.invertDisplay();

display.invertDisplay();

11. Return to normal display display.normalDisplay();

display.normalDisplay();

12. Reinitialize display.resetDisplay();

display.resetDisplay();

13. Reset the display orientation display.resetOrientation();

display.resetOrientation();

14. Set the display brightness display.setBrightness();

void OLEDDisplay::setBrightness(uint8_t)

15. Set the contrast display.setContrast()

void OLEDDisplay::setContrast(uint8_t contrast, uint8_t precharge = (uint8_t)'�', uint8_t comdetect = (uint8_t)'@')

Set display contrast
For example: extremely low brightness and contrast: contrast = 10, precharge = 5, comdetect = 0
Normal brightness and contrast: Contrast = 100

3. Drawing related API

1. Set a point display.setPixel

This is the basis of all drawing methods

void OLEDDisplay::setPixel(int16_t x, int16_t y)

2. Draw a hollow circle display.drawCircle

void OLEDDisplay::drawCircle(int16_t x, int16_t y, int16_t radius)
display.drawCircle(64,32,20);

3. Draw a filled circle display.fillCircle

void OLEDDisplay::fillCircle(int16_t x, int16_t y, int16_t radius)

4. Draw 1/4 arc display.drawCircleQuads

void OLEDDisplay::drawCircleQuads(int16_t x0, int16_t y0, int16_t radius, uint8_t quads)
display.drawCircleQuads(32,32,20,1);

Where: quads is the angle

quads Upper left Upper right Lower left Lower right
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

5. Draw a horizontal line display.drawHorizontalLine

void OLEDDisplay::drawHorizontalLine(int16_t x, int16_t y, int16_t length)
display.drawHorizontalLine(0,20,100);

6. Draw a vertical line display.drawVerticalLine

void OLEDDisplay::drawVerticalLine(int16_t x, int16_t y, int16_t length)

7. Draw a line display.drawLine

void OLEDDisplay::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1)

8. Draw a hollow rectangle display.drawRect

void OLEDDisplay::drawRect(int16_t x, int16_t y, int16_t width, int16_t height)

9. Draw a solid rectangle display.fillRect

void OLEDDisplay::fillRect(int16_t x, int16_t y, int16_t width, int16_t height)

10. Draw progress bar display.drawProgressBar

void OLEDDisplay::drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress)

Progress value is 0~100

4. Text related API

1. Set the font display.setFont

void OLEDDisplay::setFont(const uint8_t *fontData)
Built-in fonts Character height Word width Contains characters
ArialMT_Plain_10 13 10 224 characters
ArialMT_Plain_16 19 16 224 characters
ArialMT_Plain_24 28 24 224 characters

2. Set the text alignment method display.setTextAlignment()

void OLEDDisplay::setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT textAlignment)

The alignment methods are:

Alignment method description
TEXT_ALIGN_LEFT Align left
TEXT_ALIGN_RIGHT Align right
TEXT_ALIGN_CENTER Align center
TEXT_ALIGN_CENTER_BOTH Align up, down, left and right

3. Draw String display.drawString

Draw string with default or set font

void OLEDDisplay::drawString(int16_t x, int16_t y, String text)
display.setFont(ArialMT_Plain_16);
  display.clear();
  display.drawString(0,0,"hello");
  display.display();

4. Draw string (with maximum width) display.drawStringMaxWidth

Reach the maximum width and return to the line feed display

5. Image related API

1. Display 16*16 icon display.drawIco16x16

void OLEDDisplay::drawIco16x16(int16_t x, int16_t y, const char *ico, bool inverse = false)

The drawing method I use: Use PCtoLCD

Then soul drawing

Then set the output format:

Finally generate the font

Finally written in the program:

#include "SSD1306Wire.h"

SSD1306Wire display(0x3c, 21, 22);

const char image[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0x70, 0x03, 0x18, 0x06, 0x08, 0x04, 0x08, 0x04,
    0x00, 0x06, 0x80, 0x03, 0xE0, 0x00, 0xC0, 0x01, 0x00, 0x01, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x00, /*"Unnamed file",0*/
};

void setup()
{
  Serial.begin(115200);
  display.init();
  display.flipScreenVertically();
  display.clear();
  display.drawIco16x16(0, 0, image, 0);
  display.display();
}

void loop()
{
}

2. Display the XBM image display.drawXbm

void OLEDDisplay::drawXbm(int16_t x, int16_t y, int16_t width, int16_t height, const uint8_t *xbm)

I used an online converter for xbm images:https://convertio.co/zh/

3. Display BMP bitmap image display.drawFastImage (not experimented)

void OLEDDisplay::drawFastImage(int16_t x, int16_t y, int16_t width, int16_t height, const uint8_t *image)

Intelligent Recommendation

Arduino Improvement 04—U8g2 library to drive OLED

There are many driver libraries for OLED displays. This article introduces the powerful U8g2 library, which drives the OLED screen by using the U8g2 library. 1. Introduction to U8g2 library The U8g2 l...

[ESP32-ADuino-IIC2 channel 2] -Drofilling OLED-SSD1306 library-U8G2 library with IIC channel 2

ESP32 -IIC2 display OLED screen ESP32 -IIC2 display OLED screen 1. Introduction to ESP32 IIC: Introduction to I2C 3. SSD1306-I2C-channel 2 display OLED screen Fourth, U8G2-I2C-channel 2 display OLED s...

ESP32--Arduino--use oled

Install the libraryhttps://github.com/ThingPulse/esp8266-oled-ssd1306   First, we need to include the Wire.h library, which is required for I2C communication with the OLED display. We also need t...

Esp32 - Arduino - Using OLED

ESP32, this board is not welded. I borrowed my colleague's soldering iron. Each time you burn it, you have to press Boot and press EN. It's a trouble, the picture is cheap. -------------------------- ...

【Micropython ESP32】 SSD1306 0.96 "OLED+Network Clock

【Micropython ESP32】 SSD1306 0.96 "OLED+Network Clock This example is based onThonnyPlatform development. Display effect Display part usessd 1306 0.96 "OLED screen to display time. Wiring des...

More Recommendation

Play ESP32 + Arduino (twenty-nine) TFT_ESPI library drive ST7789 animation topic

About animation, we have two ideas 1. Implement an animation effect by repeatedly draw the geometric graphic erase geometry, such as the TFT_Meters example in the TFT_ESPI library 2. Fast display imag...

Arduino + SSD1306 OLED Display DEMO program

Arduino + SSD1306 OLED Displays DEMO program The screen used is 1.3 inch OLED screen Development board is Nodemcu Development environment is Arduino IDE code...

esp notes (7) to drive the display of OLED (SSD1306)

The development environment of this article: MCU model: ESP8266 IDE environment: Arduino IDE 1.8.9 1.27 inch full color OELD module The content of this article: esp8266 uses u8g2 graphics library to d...

[K210 + micropython] Drive SSD1306 OLED Display (I2C)

[K210 + micropython] driver SSD1306 display Article catalog [K210 + micropython] driver SSD1306 display Related knowledge preparation First, I2C and SPI Second, SSD1306 OLED display Third, SSD1306 dri...

Support Hongmeng OS product -level SSD1306 OLED screen drive library (MIT license open source)

Code warehouse link after transplantation:GitHub - xusiwei/harmonyos-ssd1306: SSD1306 OLED driver for Harmony OS Function introduction: • Use Harmony OS's IOT hardware interface • Provide ch...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top