Using an OLED Display with Arduino (SSD1306): Wiring, Library and Example Code
Adding a small screen to your project lets you see sensor readings, battery voltage or menus without staying connected to a computer. The most popular choice is the SSD1306 OLED display: cheap, low-power and very sharp. This guide shows how to connect a 0.96-inch 128×64 OLED to Arduino and ESP32, which libraries you need, and example code that shows text, shapes and live data.
In this article 9
What is an SSD1306 OLED display?
On an OLED display, every pixel produces its own light; there’s no backlight. That gives very high contrast, true blacks and low power draw. The SSD1306 is a common driver chip for these displays.
| Feature | Value |
|---|---|
| Size | Usually 0.96 inch (also 0.91-inch 128×32 and 1.3-inch versions) |
| Resolution | 128×64 pixels |
| Interface | I2C (4 pins) or SPI (6–7 pins) |
| Supply | 3.3–5 V on most modules |
| I2C address | Usually 0x3C, 0x3D on some modules |
| Colour | Monochrome (white, blue or yellow-blue) |
This guide uses the most common 4-pin I2C module.
Wiring
| OLED pin | Arduino Uno / Nano | ESP32 |
|---|---|---|
| VCC | 5V (or 3.3V) | 3.3V |
| GND | GND | GND |
| SDA | A4 | GPIO 21 |
| SCL | A5 | GPIO 22 |
Several devices (for example a sensor and a display) can share the same two I2C wires, as long as each has a different address. New to ESP32? Start with what is ESP32?
Install the libraries
In the Arduino IDE, open Tools → Manage Libraries and install:
- Adafruit SSD1306
- Adafruit GFX Library (provides text, line and shape functions)
Alternatively, the U8g2 library supports many displays and fonts and has modes that use less RAM.
If the screen stays blank, find the address first
The most common cause of a blank screen is the wrong I2C address. This short sketch prints the address of every device on the I2C bus to the serial monitor:
#include <Wire.h>
void setup() {
Serial.begin(115200);
Wire.begin();
for (byte a = 1; a < 127; a++) {
Wire.beginTransmission(a);
if (Wire.endTransmission() == 0) {
Serial.print("I2C: 0x");
Serial.println(a, HEX);
}
}
}
void loop() {}Example code: text, a frame and live data
This sketch writes a title and text, draws a frame and keeps updating the voltage on pin A0. Try it with a potentiometer on A0:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // No reset pin: -1
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Most modules use I2C address 0x3C
Serial.println("SSD1306 not found");
while (true);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println("makerion");
display.setTextSize(1);
display.setCursor(0, 24);
display.println("Hello world!");
display.drawRect(0, 40, 128, 20, SSD1306_WHITE); // Draw a frame
display.display(); // Send the buffer to the screen
}
void loop() {
float volts = analogRead(A0) * 5.0 / 1023.0; // Read the voltage on pin A0
display.fillRect(2, 42, 124, 16, SSD1306_BLACK);
display.setCursor(6, 46);
display.print("A0: ");
display.print(volts, 2);
display.print(" V");
display.display();
delay(250);
}One thing to note: drawing commands go to a buffer in memory first, and you must call display.display() for them to appear. Clearing and redrawing only the area that changes (here with fillRect) prevents flicker.
If you want to measure and display a battery voltage above 5 V, always use a voltage divider. Find resistor values with the voltage divider calculator.
Useful GFX functions
setTextSize(n): text size (1 = 6×8-pixel characters).setCursor(x, y): where text starts.drawLine,drawRect,fillRect,drawCircle,fillCircle: lines and shapes.drawBitmap: show your own logo or icon.invertDisplay(true): inverts the colours.
Common problems
- Blank screen: the address may be 0x3D; check with the scanner. Double-check the SDA and SCL wires.
- Sketch freezes on an Arduino Uno: the 128×64 buffer uses about 1 KB of RAM, and the Uno has 2 KB in total. If you use lots of text strings, move them to flash with the
F("...")macro or use U8g2’s page mode. - Accented characters don’t show: the default font only covers basic ASCII. Use U8g2’s extended fonts or write without accents.
- Burn-in: OLEDs can retain an image shown for a long time. On long-running projects, lower the brightness or turn the display off periodically.
If you struggle to read resistor colours, the resistor color code calculator makes it easy.
Frequently asked questions
Should I choose I2C or SPI?
I2C is enough for most projects and needs only two data wires. SPI is faster and better for animation or high refresh rates.
Can I connect two OLEDs to the same I2C bus?
Yes, but their addresses must differ. Many modules have a solder jumper or resistor on the back that switches the address between 0x3C and 0x3D.
How much current does an OLED draw?
It depends on how many pixels are lit; in most uses it’s a few to about 20 mA, which makes it suitable for battery-powered projects.
Related guides
- What Is ESP32?
- Voltage Divider Calculator
- Resistor Color Code Calculator
- All Arduino and ESP32 projects







