Sub topic 7
Getting started with an SSD1306 OLED
This chapter assumes you can upload an Arduino sketch, use setup() and loop(), wire a breadboard, and install libraries with the Arduino IDE. You will connect a 128x64 monochrome SSD1306 OLED over I2C and display text.
What the SSD1306 OLED provides
An OLED, or organic light-emitting diode display, lights each pixel directly. It needs no backlight, gives strong contrast, and can draw individual pixels and shapes. The SSD1306 is the controller chip used by many small monochrome OLED modules.
A common module has 128 columns and 64 rows. Its coordinate system starts at (0, 0) in the top-left corner:
- The x-coordinate increases from left to right.
- The y-coordinate increases from top to bottom.
- The display contains 8,192 pixels.
- The library stores a complete frame in RAM before sending it to the display.
The Adafruit libraries provide two layers:
Adafruit_GFXsupplies drawing functions such assetCursor(),drawLine(), andfillRect().Adafruit_SSD1306communicates with the SSD1306 controller.
Install both libraries from the Arduino IDE's Library Manager. Search for Adafruit SSD1306 and Adafruit GFX Library. The IDE may also install required dependencies.
Wiring the module
Most I2C OLED modules expose VCC, GND, SDA, and SCL. Connect power and ground first, then connect the two I2C signals.
| OLED pin | Arduino Uno or Nano | Purpose |
|---|---|---|
| VCC | 5V or 3.3V as specified by the module | Power |
| GND | GND | Common ground |
| SDA | A4 | I2C data |
| SCL | A5 | I2C clock |
The I2C pins differ on other boards. For example, an Arduino Mega uses pins 20 for SDA and 21 for SCL. Some OLED modules accept 5V because they include a regulator and level shifting. Others require 3.3V. Check the module's documentation before connecting VCC.
The address is commonly 0x3C, but some modules use 0x3D. The address identifies a device on the I2C bus. If display.begin() fails, scan the bus and use the address reported by the scanner.
Initialize and draw text
The constructor below uses -1 for the reset pin. This is correct for modules whose reset line is already handled or is not exposed. If your module has a separate reset connection, use that pin number instead.
display.begin() initializes the controller. It returns false when the display cannot be found at the selected address. After drawing, call display.display() to copy the RAM buffer to the physical screen.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 64;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 not found"));
for (;;) {
// Stop here until the wiring or address is fixed.
}
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(F("Arduino OLED ready"));
display.drawLine(0, 16, 127, 16, SSD1306_WHITE);
display.drawRect(0, 24, 80, 24, SSD1306_WHITE);
display.fillCircle(108, 36, 10, SSD1306_WHITE);
display.display();
}
void loop() {
}clearDisplay() clears the in-memory frame. It does not immediately clear the glass. The screen changes only when display.display() transmits the new frame. This two-step process lets you build a complete image without showing partially drawn content.
Common errors and design choices
A completely blank screen usually indicates incorrect power, swapped SDA and SCL wires, the wrong I2C address, or a missing display.display() call. A lit screen with no useful content can still have a communication problem, so test the address with an I2C scanner.
Text may appear too small because setTextSize(1) uses the library's smallest font. Increase the value to 2 or 3, but remember that larger text occupies more rows and columns.
The full 128x64 buffer requires 1,024 bytes of RAM because each byte stores eight vertical pixels. On an Arduino Uno, that leaves less RAM for variables and libraries. Keep an eye on memory when adding sensor data, long strings, or other buffers.
Exercise
Your OLED powers on but remains blank. Name two checks to make before changing the drawing code.
Show answer
Check that SDA and SCL are connected to the correct board pins, then run an I2C scanner to verify whether the module uses 0x3C or 0x3D.