Using an SSD1306 OLED display with Arduino

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.

4 min read
In this article 9
  1. What is an SSD1306 OLED display?
  2. Wiring
  3. Install the libraries
  4. If the screen stays blank, find the address first
  5. Example code: text, a frame and live data
  6. Useful GFX functions
  7. Common problems
  8. Frequently asked questions
  9. Related guides

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.

FeatureValue
SizeUsually 0.96 inch (also 0.91-inch 128×32 and 1.3-inch versions)
Resolution128×64 pixels
InterfaceI2C (4 pins) or SPI (6–7 pins)
Supply3.3–5 V on most modules
I2C addressUsually 0x3C, 0x3D on some modules
ColourMonochrome (white, blue or yellow-blue)

This guide uses the most common 4-pin I2C module.

Wiring

OLED pinArduino Uno / NanoESP32
VCC5V (or 3.3V)3.3V
GNDGNDGND
SDAA4GPIO 21
SCLA5GPIO 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.

Found this useful?Share it with a friend or follow us on Instagram for new projects.

Similar Posts