How to Use a 0.91" OLED Display with Arduino

How to Use a 0.91" OLED Display with Arduino

0.91" OLED display is a small, high-contrast screen perfect for displaying text, graphics, and sensor data in Arduino projects. This guide will walk you through wiring, coding, and displaying text and graphics using the SSD1306-based OLED module with Arduino.


1. What You Need

Arduino Board (Uno, Mega, Nano, etc.)
0.91" OLED Display (SSD1306, I2C)
Jumper Wires
Arduino IDE Installed

🔹 Note: This tutorial focuses on I2C communication, which uses only two wires (SDA & SCL), making it ideal for simple projects.


2. Wiring the 0.91" OLED to Arduino (I2C Mode)

OLED Display Pin Arduino Uno Pin Arduino Nano Pin
VCC 5V 5V
GND GND GND
SDA A4 (SDA) A4 (SDA)
SCL A5 (SCL) A5 (SCL)

🔹 For Arduino Mega: Use SDA (Pin 20) and SCL (Pin 21).


3. Installing Required Libraries

Before coding, install the Adafruit SSD1306 and Adafruit GFX libraries:

  1. Open Arduino IDE.
  2. Go to Sketch → Include Library → Manage Libraries.
  3. Search for Adafruit SSD1306 and install it.
  4. Search for Adafruit GFX and install it.

4. Basic Code to Display Text on OLED

This example initializes the OLED and displays "Hello, Arduino!".

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

// Define OLED screen size
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

// Initialize OLED display (I2C address 0x3C)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
    Serial.begin(115200);

    // Start OLED display
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
        Serial.println("SSD1306 allocation failed");
        for (;;);
    }

    display.clearDisplay();  // Clear buffer
    display.setTextSize(1);  // Text size
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(10, 10);
    display.println("Hello, Arduino!");
    display.display(); // Show text on screen
}

void loop() {
    // Nothing here (text is static)
}

🔹 What This Code Does:
✔ Initializes the SSD1306 display.
✔ Prints "Hello, Arduino!" on the screen.
✔ Uses I2C address 0x3C (some modules may use 0x3D).


5. Displaying Graphics (Lines, Circles, Images)

To draw shapes, modify the code using Adafruit GFX functions.

void setup() {
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.clearDisplay();

    // Draw a rectangle
    display.drawRect(10, 10, 50, 20, SSD1306_WHITE);
    
    // Draw a filled circle
    display.fillCircle(90, 16, 10, SSD1306_WHITE);

    display.display();
}

void loop() {}

🔹 Other Drawing Functions:
display.drawLine(x1, y1, x2, y2, color); → Draws a line
display.drawCircle(x, y, radius, color); → Draws a circle
display.drawRect(x, y, width, height, color); → Draws a rectangle


6. Scrolling Text Example

This example scrolls text horizontally across the OLED screen.

void setup() {
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.clearDisplay();
    
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 10);
    display.println("Scrolling Text...");
    display.display();
    
    // Start scrolling
    display.startscrollleft(0x00, 0x0F); 
}

void loop() {}

🔹 Scrolling Functions:
display.startscrollleft(start, stop); → Scroll text left
display.startscrollright(start, stop); → Scroll text right
display.stopscroll(); → Stops scrolling


7. Troubleshooting Tips

Problem: Display not showing anything.
✅ Check wiring (SDA & SCL correctly connected).
✅ Ensure the OLED I2C address is 0x3C (or try 0x3D).
✅ Use an I2C scanner to detect the device:

#include <Wire.h>
void setup() {
    Wire.begin();
    Serial.begin(115200);
    Serial.println("Scanning...");
    
    for (byte address = 8; address < 120; address++) {
        Wire.beginTransmission(address);
        if (Wire.endTransmission() == 0) {
            Serial.print("Found I2C device at 0x");
            Serial.println(address, HEX);
        }
    }
}
void loop() {}

Problem: Text is too small or too large.
✅ Adjust display.setTextSize(1); (increase for larger text).


8. Practical Applications

Displaying Sensor Data – Show real-time readings from DHT11, BMP280, etc.
Clock Display – Combine with DS3231 RTC module to make a digital clock.
Menu Systems – Use buttons to navigate through menus.
Battery Voltage Indicator – Monitor power levels in portable projects.


9. Conclusion

Using a 0.91" OLED with Arduino is a great way to add visual output to your projects. Whether you want to display text, graphics, or sensor data, this small screen offers high contrast and low power consumption.

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.