SSD1306 OLED display with an ESP32

I2C integration and graphics on a PCB

SSD1306 OLED display with an ESP32

This tutorial explains how to add an SSD1306 OLED display to a PCB with an ESP32, use the I2C bus and display text and graphics.

Duration :
Difficulty :
Documentation completed: 100% 100
Created by:  Alban Petit

Introduction

The SSD1306 OLED display is a compact, low-power monochrome graphic display, ideal for embedded projects. Paired with the ESP32, it can show text, graphics, icons and user interfaces without a backlight.

In this tutorial, you will learn to:

  • Understand the I2C protocol used by the SSD1306
  • Add an OLED display directly to a PCB with the ESP32
  • Display text, graphics and bitmap images
  • Optimise memory use and performance
  Logic level compatibility

The ESP32 uses 3.3V logic. The SSD1306 accepts a supply from 3.0V to 5.5V and its I2C inputs are 3.3V compatible. A direct connection is possible without a level shifter.


The I2C protocol and the SSD1306

The I2C bus

The SSD1306 communicates over the I2C (Inter-Integrated Circuit) protocol, a 2-wire serial bus:

Signal Role
SDA (Data) Bidirectional data line
SCL (Clock) Clock generated by the master (ESP32)

I2C lets several peripherals share the same bus thanks to a system of unique addresses.

Default I2C pins (ESP32-S3)

Function ESP32-S3 GPIO
SDA GPIO 8
SCL GPIO 9
  Customisable I2C pins

On the ESP32-S3, the I2C bus can be assigned to any pins with Wire.begin(SDA, SCL). GPIO 8 and 9 are the pins used in this tutorial and match the wiring of the ESP32 S3 UNO board.

  With a XIAO ESP32S3

On the XIAO ESP32S3 of the Microcontrollers workshop, the console board brings the I2C bus out to a 4-pin connector (3V3, SDA, SCL, GND), with SDA on D3 (GPIO4) and SCL on D4 (GPIO5) and 4.7 kΩ pull-ups already soldered: write Wire.begin(D3, D4); and choose board = seeed_xiao_esp32s3 in platformio.ini. These are not the XIAO’s default I2C pins (D4/D5), hence the explicit call to Wire.begin().

SSD1306 specifications

  • Resolution: 128×64 pixels (0.96” version) or 128×32 (0.91” version)
  • I2C address: 0x3C (default) or 0x3D (configurable in hardware)
  • Supply: 3.0V to 5.5V
  • Current: ~20mA in normal display, < 10µA in sleep
  • Technology: OLED (self-emissive, no backlight)
  • Contrast: Adjustable in software (0-255)

Adding it to a PCB

Choosing the module

There are two main formats:

  • I2C breakout module: 4 pins (VCC, GND, SCL, SDA), built-in pull-ups
  • Raw module: 7-8 pins, needs external pull-ups

For a PCB, prefer a breakout module with built-in pull-ups (it simplifies routing).

  I2C address

Most modules use address 0x3C by default. Some modules expose a jumper or a solder pad to switch to 0x3D if several displays must coexist on the same I2C bus.

Wiring diagram

               ESP32
               GPIO8  ─────────── SDA  ─┐
               GPIO9  ─────────── SCL  ─┤  SSD1306 display
               3.3V   ─────────── VCC  ─┤  (I2C module)
               GND    ─────────── GND  ─┘

Pull-up resistors

The I2C lines need pull-up resistors to 3.3V:

Pull-ups on SDA and SCL

Add a 4.7kΩ resistor to 3.3V on the following lines:

  • SDA: keeps the line high when no peripheral is transmitting
  • SCL: keeps the line high between clock pulses

Warning: Most OLED breakout modules already include these pull-ups (typically 10kΩ). Check your schematic before adding more. Pull-ups that are too low (< 2.2kΩ in total) can cause communication problems.

Decoupling capacitors

Power supply decoupling

Place the following capacitors between VCC and GND of the module, as close as possible to the VCC pin:

  • 100nF ceramic: filters high-frequency noise
  • 10µF ceramic or tantalum: absorbs the current peaks when the display refreshes

These capacitors stabilise the supply and prevent flicker or display corruption.

Checklist before powering up

Checks before the first test

Before powering your PCB for the first time:

  • Visually check the solder joints of the module (no short circuit between pads)
  • Check continuity between the module GND and the ESP32 GND
  • Check continuity between the module VCC and 3.3V
  • Check that the decoupling capacitors are fitted (100nF + 10µF)
  • Check that SDA and SCL are not swapped
  • Measure 3.3V on VCC before powering the ESP32

Code

PlatformIO project configuration

The Adafruit_SSD1306 library is the most widely used. Add it to platformio.ini:

[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
monitor_speed = 115200
lib_deps =
    adafruit/Adafruit SSD1306@^2.5.7
    adafruit/Adafruit GFX Library@^1.11.3
  GFX Library dependency

The Adafruit_SSD1306 library depends on Adafruit_GFX_Library for the drawing functions (lines, circles, text). Both must be installed.

Code 1: Initialisation and text display

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1  // No RESET pin (shared with the ESP32)
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(115200);
  delay(500);

  // Initialise I2C
  Wire.begin(8, 9);  // SDA=8, SCL=9

  // Initialise the display
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println("Error: SSD1306 display not detected");
    while (true) delay(1000);
  }

  Serial.println("SSD1306 display initialised successfully!");

  // Clear the display
  display.clearDisplay();

  // Set up the text
  display.setTextSize(1);             // Normal size
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);

  // Print text
  display.println("ESP32 + SSD1306");
  display.println("");
  display.setTextSize(2);
  display.println("Hello!");

  // Send to the display
  display.display();
}

void loop() {}
  The display.display() function

The display is buffered: all the drawing functions modify a RAM buffer. You must call display.display() to send the buffer to the physical display. This improves performance by grouping updates.

Code 2: Graphics (shapes and pixels)

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(115200);
  Wire.begin(8, 9);

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println("SSD1306 error");
    while (true);
  }

  display.clearDisplay();

  // Draw a rectangle
  display.drawRect(10, 10, 50, 30, SSD1306_WHITE);

  // Draw a filled circle
  display.fillCircle(100, 25, 15, SSD1306_WHITE);

  // Draw a line
  display.drawLine(0, 50, 127, 50, SSD1306_WHITE);

  // Draw individual pixels
  for (int i = 0; i < 20; i++) {
    display.drawPixel(i * 6, 55 + random(-5, 5), SSD1306_WHITE);
  }

  display.display();
}

void loop() {}

Code 3: Animation and dynamic updates

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int ballX = 64;
int ballY = 32;
int ballVX = 2;
int ballVY = 1;
const int ballRadius = 3;

void setup() {
  Serial.begin(115200);
  Wire.begin(8, 9);

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println("SSD1306 error");
    while (true);
  }

  display.clearDisplay();
  display.display();
}

void loop() {
  // Clear the display
  display.clearDisplay();

  // Update the position of the ball
  ballX += ballVX;
  ballY += ballVY;

  // Bounce off the edges
  if (ballX <= ballRadius || ballX >= SCREEN_WIDTH - ballRadius) {
    ballVX = -ballVX;
  }
  if (ballY <= ballRadius || ballY >= SCREEN_HEIGHT - ballRadius) {
    ballVY = -ballVY;
  }

  // Draw the ball
  display.fillCircle(ballX, ballY, ballRadius, SSD1306_WHITE);

  // Show the frame counter
  display.setCursor(0, 0);
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.print("FPS: ");
  display.print(1000 / 50);  // Approximation

  // Send to the display
  display.display();

  delay(50);  // ~20 FPS
}
  Performance and RAM

The display buffer uses 1024 bytes of RAM for a 128×64 display. On the ESP32, this is not a problem. However, avoid calling display.display() too often: each call transfers 1 KB over I2C (~8ms at 400 kHz). Aim for 10-30 FPS at most for animations.


Troubleshooting

The display does not turn on

Check the power

Check that the display is powered:

  • Measure 3.3V on the module’s VCC pin
  • Check continuity between the module GND and the ESP32 GND
  • Test with an external 3.3V supply to isolate the problem

Scan the I2C address

Use the following code to scan the I2C bus and detect the address of the SSD1306:

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin(8, 9);
  Serial.println("I2C scan...");

  for (byte address = 1; address < 127; address++) {
    Wire.beginTransmission(address);
    if (Wire.endTransmission() == 0) {
      Serial.print("Device found at 0x");
      Serial.println(address, HEX);
    }
  }
  Serial.println("Scan finished.");
}

void loop() {}

The address should be 0x3C or 0x3D. If nothing appears, check the wiring.

The display is corrupted or flickers

Decoupling or pull-up problem

Check:

  • That the decoupling capacitors are fitted (100nF + 10µF)
  • That the pull-up resistors are not too low (< 2.2kΩ in total)
  • That the I2C tracks are not too long (< 10 cm recommended)
  • That the I2C frequency is not too high: lower it to 100 kHz with Wire.setClock(100000);

The display shows truncated text

Buffer overflow

Text that goes beyond the edges of the display is simply ignored. Check:

  • That setCursor(x, y) points inside the display area (x < 128, y < 64)
  • That the text size (setTextSize()) does not make the characters overflow
  • Use display.getTextBounds() to compute the width of a text before displaying it

The ESP32 restarts during initialisation

RAM or power problem

On the ESP32, the SSD1306 buffer uses 1 KB of RAM. If you have many global variables, this can saturate the stack.

If the problem persists:

  • Check that the ESP32’s 3.3V supply can deliver > 500mA
  • Add a 100µF capacitor on the ESP32’s 3.3V line
  • Lower the I2C frequency: Wire.setClock(100000);

Optimisations

Reducing memory use

Partial display

To save RAM, display only part of the screen:

// Create a buffer for only 128×32 pixels (512 bytes)
Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET);

Or use the U8g2 library, which offers reduced buffer modes (1/8, 1/4 page).

Improving performance

Increase the I2C speed

By default, I2C runs at 100 kHz. Increase it to 400 kHz for faster transfers:

Wire.setClock(400000);  // 400 kHz (Fast Mode)

Be careful: long tracks or pull-ups that are too low can cause errors at 400 kHz.


Resources


Conclusion

You now know how to add an SSD1306 OLED display to a PCB with the ESP32 and display text, graphics and animations.

Key points to remember

  • I2C bus: SDA=8, SCL=9 (pins used on the ESP32 S3 UNO board)
  • 4.7kΩ pull-ups on SDA and SCL (often built into the module)
  • Decoupling: 100nF + 10µF as close as possible to VCC
  • RAM buffer: 1024 bytes for 128×64 pixels
  • Call display.display() after each update to refresh the display

Going deeper

  • Use U8g2 for optimised memory management and more fonts
  • Display bitmaps (logos, icons) with display.drawBitmap()
  • Create user interfaces with menus and buttons
  • Pair the display with sensors to show temperature, humidity and so on
  Well done!

Your ESP32 can now show graphics on an OLED display. It is an essential building block for creating user interfaces and interactive IoT projects.


Going further

Other ESP32 tutorials

Explore other tutorials to master the ESP32 and its peripherals


Need help?