Introduction
Using the SSD1306 OLED Display with Arduino is a popular choice for showing text, graphics, and sensor readings in projects. This guide will help you set up a 0.96 inch I2C OLED Display with Arduino, covering wiring, code examples, and tips to get your display working quickly. Whether you’re a beginner or working on an advanced project, this article will simplify using OLED displays in your circuits.
Read more about OLED displays here.
Components Required:
- Arduino Uno (or any compatible Arduino board)
- 0.96 inch OLED Display (I2C interface)
- Jumper Wires
- Breadboard (optional, for easier prototyping)
Wiring the OLED Display with Arduino
OLED Display Pin | Arduino Pin | Description |
---|---|---|
VCC | 5V | Provides power to the display |
GND | GND | Common ground connection |
SDA | A4 | I2C Data line |
SCL | A5 | I2C Clock line |

Installing the Required Libraries
Before using the OLED Display with Arduino, you need to install two essential libraries:
- Open Arduino IDE.
- Go to Sketch → Include Library → Manage Libraries.
- In the search bar, type Adafruit GFX. Click Install.
- Next, search for Adafruit SSD1306 and click Install.
Once both libraries are installed, you’re ready to start working with your OLED display.


Basic Code to Display Text
Once your OLED Display with Arduino is wired up, displaying text is simple. Here’s an example code to show “Hello, World!” on the OLED screen:
// I2C OLED Display with Arduino by ArduinoYard.com
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
}
void loop() {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 10);
display.println("Hello,");
display.setCursor(0, 30);
display.println("World!");
display.display();
delay(2000);
}
How It Works – Code Breakdown
Let’s walk through the code step-by-step to understand how it displays “Hello, World!” on the OLED Display:
1. Initialization: setup() function
This part is executed once when the Arduino starts.
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
- display.begin() – Initializes the OLED display.
- SSD1306_SWITCHCAPVCC specifies that the display is powered by a 3.3V supply generated internally.
- 0x3C is the I2C address of the OLED display (common for most 0.96” OLEDs).
- display.display() – Refreshes the display to show any content (even if empty) after initialization.
- delay(2000) – Pauses the program for 2000 milliseconds (2 seconds), allowing the initial screen to stay visible before the loop starts.
2. Displaying Text: loop() function
This part runs repeatedly after setup().
display.clearDisplay();
- Clears the display buffer (removes any previous content).
display.setTextColor(WHITE);
- Sets the text color to white. Since most OLED displays are monochrome (black and white),
WHITE
is the color used to show text or graphics.
display.setTextSize(2);
- Sets the text size.
- Size
1
is the smallest (default),2
is larger, and so on. - Each increase in size multiplies the text by that factor.
- Size
display.setCursor(0, 10);
- Positions the cursor at coordinates
(0, 10)
on the display.(x, y)
refers to pixels, with(0, 0)
being the top-left corner.- Here, text will start 10 pixels down from the top.
display.println("Hello,");
- Prints the text “Hello,” on the display at the current cursor position.
- Moves the cursor to the next line automatically after printing (similar to
Serial.println()
).
display.setCursor(0, 30);
display.println("World!");
- Moves the cursor to
(0, 30)
, then prints “World!” on the display.
display.display();
- Pushes the content from the display buffer to the screen.
- Nothing appears on the OLED until you call
display.display()
.
- Nothing appears on the OLED until you call
delay(2000);
- Pauses the display for 2 seconds, keeping the text visible.
Summary of Key Functions:
Function | Purpose |
---|---|
display.begin() | Initializes the OLED display. |
display.display() | Updates the screen with what’s in the buffer. |
display.clearDisplay() | Clears the display buffer. |
display.setTextColor() | Sets the text color (WHITE or BLACK). |
display.setTextSize() | Sets the text size (1, 2, 3…). |
display.setCursor() | Sets the cursor position for text. |
display.println() | Prints text and moves to a new line. |
This is the basic structure for displaying text on an OLED Display with Arduino.
Demonstration

Troubleshooting OLED Display Address Issues
If your OLED display is not working as expected, it could be due to an incorrect I2C address. Different OLED displays can have varying addresses depending on the driver chip or manufacturer.
Common I2C Addresses:
- SSD1306 driver: Usually uses 0x3C.
- SH1106 driver: Typically uses 0x3D.
However, some displays may have different addresses, so it’s important to confirm the correct one.
How to Find the Correct I2C Address:
- Check the Display Documentation:
Refer to your OLED display’s datasheet, product description, or seller’s details for the default I2C address. - Use an I2C Scanner Sketch:
If you’re unsure of the address, you can run an I2C scanner sketch. This simple program scans all possible I2C addresses and shows which devices are detected.
I2C Scanner Sketch Example:
Here’s an example sketch you can upload to your Arduino to find the address of your OLED display:
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial); // Wait for serial monitor to open
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning…");
nDevices = 0;
for (address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error == 4)
{
Serial.print("Unknow error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("Done\n");
delay(5000); // Wait 5 seconds before scanning again
}
What to Do Next:
- Open the Serial Monitor (set baud rate to 9600).
- Note the address detected (e.g., 0x3C or 0x3D).
- Update the address in your OLED initialization code like this:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Replace 0x3C with the detected address
This should solve any I2C address issues and get your OLED Display with Arduino working smoothly.
Conclusion
OLED displays are a versatile and powerful tool for displaying information in your Arduino projects. By following this beginner’s guide, you should be able to connect an OLED display to an Arduino and display text or graphics on it using a library like the Adafruit SSD1306 library.
With some creativity and experimentation, you can use OLED displays to create unique and innovative projects like:
YOUTUBE CHANNEL STATISTICS DISPLAY WITH NODEMCU(ESP8266) AND I2C OLED DISPLAY
Thank you!