Connect ESP32 to 2.42 inch OLED 128x64 Display via SPI
Connect ESP32 to 2.42 inch OLED 128x64 Display via SPI
This guide covers wiring the 128x64 pixel OLED display (2.42 inch) to ESP32 using SPI Protocol. This display module supports both SPI and I2C interfaces, but we’ll use SPI for faster data transfer.
Required Components
- ESP32 NodeMCU ESP-WROOM-32 board
- OLED 128x64 SPI display 2.42 inch
- Breadboard 170 points
- Jumper wires (M-M, M-F, F-F)
- Micro USB cable for ESP32
- Power Adapter micro USB 5V 2A
ESP32 to OLED SPI Wiring
Using ESP32’s hardware SPI pins for best performance.
| ESP32 | OLED SPI |
|---|---|
| 3V3 | VCC |
| GND | GND |
| TX2 | RST |
| D5 | CS |
| RX2 | DC |
| D18 | SCK |
| D23 | SDA |
Pin Notes
- TX2 (GPIO17) - Used as RST (Reset) pin
- RX2 (GPIO16) - Used as DC (Data/Command) pin
- D18 (GPIO18) - Hardware SPI Clock (SCK)
- D23 (GPIO23) - Hardware SPI MOSI (Data out)
- D5 (GPIO5) - Chip Select (CS)
Install Adafruit SSD1306 Library
- Download Adafruit SSD1306 library from MediaFire (RAR format)
- Extract using WinRAR or WinZip
- Copy Adafruit_SSD1306 folder to
Documents/Arduino/libraries - Restart Arduino IDE
Note: For detailed Library installation guide, check [Arduino Library Installation Tutorial]
ESP32 OLED SPI Example Code
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// SPI pins for ESP32
#define OLED_MOSI 23
#define OLED_CLK 18
#define OLED_DC 16 // RX2
#define OLED_CS 5 // D5
#define OLED_RESET 17 // TX2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
void setup() {
Serial.begin(115200);
// Initialize OLED SPI
if(!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println("SSD1306 allocation failed");
for(;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 20);
display.println("ESP32");
display.setCursor(10, 40);
display.println("OLED OK");
display.display();
delay(2000);
}
void loop() {
// Draw rectangle
display.clearDisplay();
display.drawRect(10, 10, 50, 30, SSD1306_WHITE);
display.display();
delay(500);
// Draw circle
display.clearDisplay();
display.drawCircle(64, 32, 20, SSD1306_WHITE);
display.display();
delay(500);
// Draw line
display.clearDisplay();
display.drawLine(0, 0, 128, 64, SSD1306_WHITE);
display.display();
delay(500);
}
Adjustment Points for Different Displays
If the display doesn’t work, try these adjustments:
- contrast - Increase contrast value in
display.begin()if image is too dim - RST pin - Some 2.42 inch displays need RST connected through 10kΩ resistor to GND
- Voltage - Some 2.42 inch modules may require 5V on VCC (check your display’s datasheet)
How to Upload to ESP32
- Open Arduino IDE and paste the code above
- Go to Tools > Board > ESP32 Arduino and select ESP32 Dev Module
- Go to Tools > Port and select the COM port
- Click Upload button (right arrow)
- If Save sketch dialog appears, name it and click OK
- Wait for Connecting… message. If it stays more than 5 seconds, press and hold BOOT button on ESP32
- Wait for Done Uploading confirmation
[image: Arduino IDE screenshot showing Done Uploading message in black console text]
Reference Video
ESP32 OLED 2.42 inch SPI Tutorial
Summary
Connecting ESP32 to 128x64 OLED via SPI requires 7 wires using hardware SPI pins (D18=SCK, D23=MOSI) plus control pins (D5=CS, RX2=DC, TX2=RST). After installing Adafruit SSD1306 library, upload the example code. If upload fails, press BOOT button during connection phase.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย