Wiring and Using OLED 128x64 I2C Display with Arduino
Wiring and Using OLED 128x64 I2C Display with Arduino
This guide walks you through connecting a 1.3-inch OLED display with 128x64 resolution to an Arduino UNO over I2C. You’ll wire the circuit, install the required library, and upload code to display text.
What You Need
- Arduino UNO R3
- OLED 128x64 I2C display 1.3 inch
- 4 jumper wires (male-to-female)
- Breadboard (optional)
Wiring Diagram
| Arduino UNO | OLED Display |
|---|---|
| 5V | VCC |
| GND | GND |
| A4 | SDA |
| A5 | SCL |
Install the Library
- Open Arduino IDE
- Go to Sketch > Include Library > Manage Libraries
- Search for Adafruit SSD1306
- Click Install
Adafruit GFX Library will install automatically as a required dependency.
Working Example Code
#include <SPI.h>
#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() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 20);
display.println(F("Hello"));
display.setCursor(0, 40);
display.println(F("Arduino"));
display.display();
}
void loop() {
// nothing here
}
Adjusting Text Size and Position
display.setTextSize(1); // text size 1-4
display.setCursor(0, 10); // x, y position on screen
display.print(F("Your text"));
Text height by size level:
| setTextSize | Height (pixels) |
|---|---|
| 1 | 8 |
| 2 | 16 |
| 3 | 24 |
Finding Your OLED I2C Address
If the display shows nothing, run the I2C Scanner sketch to find the correct address.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
byte error, address;
int devices = 0;
Serial.println("Scanning I2C...");
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
devices++;
}
}
if (devices == 0) Serial.println("No I2C devices found");
delay(5000);
}
Open Serial Monitor at 9600 baud. If you see 0x3C or 0x3D, update the address in your main code at display.begin(SSD1306_SWITCHCAPVCC, 0x3C).
If OLED V2.0 Does Not Work
Some OLED V2.0 modules use SH1106 driver instead of SSD1306. Switch to U8g8lib instead.
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void setup() {
u8g2.begin();
u8g2.clear();
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0, 20, "Hello");
u8g2.drawStr(0, 40, "Arduino");
u8g2.sendBuffer();
}
Quick Checklist
- Wire 4 connections: 5V, GND, A4 (SDA), A5 (SCL)
- Install Adafruit SSD1306 and Adafruit GFX Library
- Upload the working example code
- If the screen stays blank, run I2C Scanner to find the address
- If using SH1106 driver, switch library to U8g8lib
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย