Bird
0
0
Arduinoprogramming~5 mins

OLED display with I2C (SSD1306) in Arduino

Choose your learning style9 modes available
Introduction

OLED displays show text and images clearly using very little power. Using I2C makes wiring simple with just two wires.

You want to show sensor readings like temperature or humidity on a small screen.
You need a compact display for a wearable device or small gadget.
You want to add a user interface with menus and options to your project.
You want to display status messages or icons without using many pins.
You want a bright, sharp display that works well in low light.
Syntax
Arduino
#include <Wire.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

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

void setup() {
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for(;;); // Loop forever if display not found
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.println("Hello, OLED!");
  display.display();
}

void loop() {
  // Your code here
}

Use the Adafruit_SSD1306 library for easy control of the SSD1306 OLED display.

The I2C address 0x3C is common but check your display's datasheet.

Examples
This example shows how to initialize the display and print "Hi!" with bigger text.
Arduino
#include <Wire.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10, 10);
  display.println("Hi!");
  display.display();
}

void loop() {}
Use these commands inside loop() to update the display with a changing number.
Arduino
display.clearDisplay();
display.setCursor(0,0);
display.print("Count: ");
display.println(counter);
display.display();
Sample Program

This program shows a counter that increases every second on the OLED screen.

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

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

int counter = 0;

void setup() {
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for(;;); // Stop here if display not found
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  display.clearDisplay();
  display.setCursor(0,0);
  display.print("Counter: ");
  display.println(counter);
  display.display();
  counter++;
  delay(1000);
}
OutputSuccess
Important Notes

Always call display.display() after drawing to update the screen.

Use display.clearDisplay() to erase old content before drawing new content.

Check your OLED's I2C address; it might be 0x3C or 0x3D.

Summary

OLED with I2C uses only two wires for easy connection.

Use the Adafruit_SSD1306 library to control the display simply.

Remember to clear and update the display to show new content.