Interfacing an OLED display with an Arduino Nano is a fun and useful project, especially for displaying sensor data or status messages. Here’s a simple step-by-step guide to help you get started.
🧰 What You Need
- Arduino Nano
- 0.96″ OLED Display (I2C, typically 128×64 resolution)
- Jumper wires
- Breadboard (optional)
🔌 Wiring (I2C OLED)
Most I2C OLEDs have 4 pins: VCC, GND, SCL, SDA
OLED Pin | Connect To (Arduino Nano) |
---|---|
VCC | 5V |
GND | GND |
SCL | A5 |
SDA | A4 |
🧱 Libraries You Need
- Adafruit SSD1306
- Adafruit GFX
You can install these libraries via the Arduino Library Manager:
- Go to Sketch > Include Library > Manage Libraries
- Search for:
- Adafruit SSD1306
- Adafruit GFX Library
- Install both.
Sample Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin not used on I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the I2C address
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Set text size and color
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("Hello From Tech Craft And Hacks!");
display.display(); // Actually draw it on the display
}
void loop() {
// You can update the display here
}
🛠️ Troubleshooting Tips
- If your display doesn’t turn on:
- Double-check wiring.
- Try scanning I2C address using an I2C scanner sketch to confirm it’s
0x3C
or0x3D
.
- If the screen flickers or acts strangely:
- Check power supply stability.
- Make sure the display is I2C, not SPI (they look very similar).