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.
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.
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:
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.
| Function | ESP32-S3 GPIO |
|---|---|
| SDA | GPIO 8 |
| SCL | GPIO 9 |
There are two main formats:
For a PCB, prefer a breakout module with built-in pull-ups (it simplifies routing).
ESP32
GPIO8 ─────────── SDA ─┐
GPIO9 ─────────── SCL ─┤ SSD1306 display
3.3V ─────────── VCC ─┤ (I2C module)
GND ─────────── GND ─┘
The I2C lines need pull-up resistors to 3.3V:
Add a 4.7kΩ resistor to 3.3V on the following lines:
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.
Place the following capacitors between VCC and GND of the module, as close as possible to the VCC pin:
These capacitors stabilise the supply and prevent flicker or display corruption.
Before powering your PCB for the first time:
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
#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() {}
#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() {}
#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
}
Check that the display is powered:
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.
Check:
Wire.setClock(100000);Text that goes beyond the edges of the display is simply ignored. Check:
setCursor(x, y) points inside the display area (x < 128, y < 64)setTextSize()) does not make the characters overflowdisplay.getTextBounds() to compute the width of a text before displaying itOn 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:
Wire.setClock(100000);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).
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.
You now know how to add an SSD1306 OLED display to a PCB with the ESP32 and display text, graphics and animations.
display.display() after each update to refresh the displaydisplay.drawBitmap()Explore other tutorials to master the ESP32 and its peripherals