กลับหน้าหลัก
views
Connect ESP32 to 2.42 inch OLED 128x64 Display via SPI
Last updated on

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
Diagram showing all components - ESP32 board, 2.42 inch OLED display, colored jumper wires, breadboard arranged for SPI connection

ESP32 to OLED SPI Wiring

Using ESP32’s hardware SPI pins for best performance.

ESP32OLED SPI
3V3VCC
GNDGND
TX2RST
D5CS
RX2DC
D18SCK
D23SDA
ESP32 pinout diagram with arrows pointing to TX2, RX2, D5, D18, D23 pins highlighted and labeled

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)
Close-up of ESP32 board with circled pin numbers TX2, RX2, D5, D18, D23 clearly visible

Install Adafruit SSD1306 Library

  1. Download Adafruit SSD1306 library from MediaFire (RAR format)
  2. Extract using WinRAR or WinZip
  3. Copy Adafruit_SSD1306 folder to Documents/Arduino/libraries
  4. 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

  1. Open Arduino IDE and paste the code above
  2. Go to Tools > Board > ESP32 Arduino and select ESP32 Dev Module
  3. Go to Tools > Port and select the COM port
  4. Click Upload button (right arrow)
  5. If Save sketch dialog appears, name it and click OK
  6. Wait for Connecting… message. If it stays more than 5 seconds, press and hold BOOT button on ESP32
  7. 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

จ้างทำโปรเจคเลย

Project estimate

Want something like this? Open the estimate page.

The long form is now on a separate estimate page, so this article stays clean.

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...