Introduction
In this guide, we’ll walk through how to use an I2C (Inter-Integrated Circuit) display with an Arduino board. I2C displays are popular due to their simplicity and ease of use. We’ll cover the necessary components, libraries, circuit connections, code implementation, and some key considerations to keep in mind. We’re focusing on the SSD1306 model of OLED display. It’s a single-color display measuring 0.96 inches diagonally, with a resolution of 128×64 pixels.
Unlike traditional displays, OLED screens like the SSD1306 don’t need a backlight. This gives them excellent contrast in low-light conditions. Plus, they’re energy-efficient because they only use power when displaying something.
The SSD1306 we’re using connects to the Arduino with just four pins, using the I2C communication protocol. Some models may have an extra RESET pin, while others might use SPI communication instead of I2C.
Things You Need
- Arduino board (e.g., Uno, Nano, MEGA)
- SSD1306 Display
- Jumper wires
- Breadboard (optional)
Libraries
To control the OLED display you need the adafruit_SSD1306.h and the adafruit_GFX.h libraries. If you do not know to install libraries click here
- Adafruit_SSD1306: For OLED displays
You can install these libraries through the Arduino IDE Library Manager.
Diagram
Code
#include <Wire.h>
#include <Adafruit_GFX.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() {
// Initialize display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println(F("Hello, World!"));
display.display();
}
void loop() {
// No need for loop in this example
}
To ensure everything is wired correctly and the libraries are installed properly, run a test example provided by the Adafruit SSD1306 library:
- Go to File > Examples > Adafruit SSD1306 in your Arduino IDE.
- Select the example corresponding to the display you’re using.
- Upload the example to your Arduino board.
Tips for writing text using these libraries
The Functions that will help you handle the OLED display library to write text and draw simple graphics.
- display.clearDisplay() – all pixels are off
- display.drawPixel(x,y, color) – plot a pixel in the x,y coordinates
- display.setTextSize(n) – set the font size, supports sizes from 1 to 8
- display.setCursor(x,y) – set the coordinates to start writing text
- display.print(“message”) – print the characters at location x,y
- display.display() – call this method for the changes to make effect
Troubleshooting
If your OLED display is not showing anything:
- Check that the OLED display is properly wired to the Arduino
- Double-check the OLED display I2C address: with the OLED connected to the Arduino,upload the code below and check the I2C address in the Serial Monitor
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
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.println(address,HEX);
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);
}
You should change the OLED address in the following line, if necessary. In our case, the address is 0x3C.
Conclusion
You can modify the code to display different messages, draw shapes, or integrate sensor readings to create more complex projects. Refer to the Adafruit SSD1306 library documentation for detailed usage instructions and advanced features.