OLED 128×64 displays (SSD1306)

These small displays (usually 0.96″; there is also a 128×32 version) have 128 × 64 = 8192 pixels. Each pixel is on or off. Each pixel makes its own light, so there is no backlight. Black is fully black, and the contrast is very good. A driver chip, the SSD1306, keeps the image in its own memory. The ESP32 communicates with it through I²C (4 wires) or SPI (7 wires).

A 0.96 inch 128 by 64 OLED module with an SSD1306 driver, showing a logo, with pins and a STEMMA QT cable
A 0.96″ 128×64 SSD1306 OLED module (this version has I²C and SPI pins).
Photo: Adafruit (Kattni Rembor), CC BY-SA 3.0
Move the pointer over the display to see the coordinates and the location of the pixel in the SSD1306 memory.

Try the Adafruit_GFX draw functions

Your drawing as code

Bitmap array (for drawBitmap)

bitmap.h

Two different byte layouts

drawBitmap() arrays (horizontal): Each byte is 8 pixels in a row. The leftmost pixel is the most significant bit. A row that is w pixels wide uses ⌈w/8⌉ bytes. The editor on the left makes this format (tools such as image2cpp also make it).

SSD1306 memory (vertical pages): The chip puts the rows into 8 pages of 8 rows. Each byte is a vertical strip of 8 pixels in one column, with bit 0 at the top, so the byte for pixel (x, y) is buffer[x + (y / 8) * 128] and the bit is y % 8. Use the Inspect tool to see this.

The library converts between the two layouts. You need the page layout only if you write your own driver, or if you want to know why a change to one pixel sends a full byte again.

Wiring (I²C version)

ESP32 3V3 GND GPIO 22 (SCL) GPIO 21 (SDA) Hello ESP32! 128×64 SSD1306 VCC GND SCL SDA Read the pin order on YOUR module: some are GND-VCC-SCL-SDA, others are VCC-GND-…
Most modules have a regulator and I²C pull-up resistors, so you need only four wires.

Connections

OLED pinESP32Notes
VCC3V3Most modules accept 3.3–5 V. With 3.3 V, the I²C lines stay at safe levels.
GNDGND
SCL / SCKGPIO 22Default I²C clock in Arduino-ESP32
SDAGPIO 21Default I²C data. To use other pins, use Wire.begin(sda, scl)

I²C addresses

  • The address is usually 0x3C. Some modules use 0x3D. A solder jumper or a resistor on the back selects the address.
  • A label with 0x78 (or 0x7A) shows the same address in 8-bit form: 0x3C × 2 = 0x78. Use 0x3C in code.
  • I²C is a bus. The OLED can use the same SDA/SCL lines as an MPU-6050 (0x68) and other sensors, if their addresses are different.
  • Two identical OLEDs need different addresses (0x3C and 0x3D). Alternatively, use the second I²C bus (Wire1).

SPI version (7 pins)

The pin labels are GND, VCC, D0 (SCK), D1 (MOSI), RES, DC, CS. SPI is faster (smoother animation), but it uses more pins. Use the Adafruit_SSD1306(128, 64, &SPI, DC, RESET, CS) constructor.

1.3″ modules: SH1106

Many 1.3″ “OLED 128×64” boards use an SH1106 controller. It has 132 columns of memory and a different addressing method. With the SSD1306 library, these boards show a stripe of random pixels on one side. Use Adafruit_SH110X or the U8g2 SH1106 constructor.

Burn-in

OLED pixels become weaker with use. If a static logo stays on the display for weeks, it leaves a ghost image. Dim the display (display.dim(true)), clear it when it is idle, or move the content a small distance.

Step by step

  1. Install the libraries: Library Manager → Adafruit SSD1306. The Library Manager also shows Adafruit GFX and BusIO. Install all of them.
  2. Connect the wires. Make the four connections in the table. Make sure that VCC/GND agree with the labels.
  3. Run the I²C scanner. Upload the sketch below. Open the Serial Monitor at 115200 baud. It must show 0x3C (or 0x3D).
  4. Upload “Hello”. Use the address that the scanner found.
  5. Draw your own image: Draw an icon in the editor above. Copy the array. Use drawBitmap().

Troubleshooting

  • The scanner finds no device: SDA/SCL are in the wrong positions, the pins are wrong, a jumper wire is loose, or there is no power.
  • “SSD1306 allocation failed”: begin() could not get memory for the 1 KB frame buffer. This is rare on the ESP32. Examine the width and height in the constructor.
  • The screen stays black, but the scanner finds the device: Give the address from the scanner to begin(). Call display.display() after you draw. The draw functions change only the RAM buffer.
  • Column of random pixels on the edge: The panel is an SH1106. Use a different library.
  • Text overlaps: Call clearDisplay() before you draw each frame again.

Code

I²C scanner

i2c_scanner.ino


                #include <Wire.h>

                void setup() {
                  Serial.begin(115200);
                  Wire.begin();                 // SDA 21, SCL 22 by default
                  Serial.println("Scanning I2C bus...");
                  for (uint8_t addr = 1; addr < 127; addr++) {
                    Wire.beginTransmission(addr);
                    if (Wire.endTransmission() == 0) {
                      Serial.printf("Found device at 0x%02X\n", addr);
                    }
                  }
                  Serial.println("Done.");
                }

                void loop() {}
              

U8g2 alternative

Library Manager → U8g2. It has many fonts. It operates with the SSD1306, the SH1106 and many other displays.

u8g2_hello.ino


                #include <U8g2lib.h>
                #include <Wire.h>

                // Full-buffer, hardware I2C. Use U8G2_SH1106_128X64_NONAME_F_HW_I2C for SH1106 panels.
                U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);

                void setup() {
                  u8g2.begin();
                }

                void loop() {
                  u8g2.clearBuffer();
                  u8g2.setFont(u8g2_font_ncenB10_tr);
                  u8g2.drawStr(0, 14, "Hello ESP32!");
                  u8g2.drawFrame(0, 20, 128, 44);
                  u8g2.sendBuffer();              // like display.display()
                  delay(500);
                }
              

Adafruit_SSD1306 + GFX

oled_hello.ino


              #include <Wire.h>
              #include <Adafruit_GFX.h>
              #include <Adafruit_SSD1306.h>

              #define SCREEN_W 128
              #define SCREEN_H 64        // 32 for 128x32 modules
              Adafruit_SSD1306 display(SCREEN_W, SCREEN_H, &Wire, -1);   // -1 = no reset pin

              // Paste an array from the editor above here, for example:
              // const unsigned char myBitmap[] PROGMEM = { ... };

              void setup() {
                Serial.begin(115200);
                if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {      // 0x3C or 0x3D
                  Serial.println("SSD1306 allocation failed");
                  for (;;);                                            // stop here
                }
                display.clearDisplay();
                display.setTextSize(1);                                // 6x8 px characters
                display.setTextColor(SSD1306_WHITE);
                display.setCursor(0, 0);
                display.println("Hello ESP32!");
                display.drawRect(0, 12, 128, 52, SSD1306_WHITE);
                display.fillCircle(64, 38, 12, SSD1306_WHITE);
                display.display();                                     // send the buffer to the screen
              }

              void loop() {
                // Live value: clear only the area that you draw again (in the frame)
                display.fillRect(1, 55, 126, 8, SSD1306_BLACK);
                display.setCursor(2, 55);
                display.printf("uptime %lus", millis() / 1000);
                display.display();
                delay(1000);

                // Show a bitmap:
                // display.drawBitmap(x, y, myBitmap, w, h, SSD1306_WHITE);
              }
            

Resources