yahboom
HomeTutorialsProduct
Solution
Blog
Online Store
About Us
EN/中文
Store
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

2. I2C_OLED Display Experiment

I2C OLED Display Experiment


I2C OLED Display Experiment1. I2C OLED Overview2. Core Concepts2.1 I2C Bus2.2 SSD1306 Display Principle2.3 Font Rendering Principle2.4 Core API3. Hardware Dependencies3.1 Pin Mapping3.2 OLED Parameters 4. Project Architecture and Flow4.1 File Architecture4.2 Running Flow5. Source Code Details5.1 Entry main.py — Font Selection5.2 SSD1306 Driver (ssd1306.py)5.3 Font Data (fonts.py)6. Operation Procedure6.1 File Upload6.2 Serial Output Example6.3 Expected Behavior6.4 Verification Method7. Common Issues


1. I2C OLED Overview

SSD1306 is a monochrome OLED driver chip with 128×32 resolution, communicating with the host through the I2C bus (only the two lines SDA / SCL are needed). This experiment is based on a pure MicroPython self-written SSD1306 driver (no third-party dependencies), displaying "HI YAHBOOM" on the OLED with an 11×18 pixel font.

Typical application scenarios:

ApplicationExample
Data displayTemperature/humidity, time, voltage, etc.
Status panelDevice running status, IP address
Menu interfaceEmbedded UI, parameter settings
Debug outputOffline log display

2. Core Concepts

2.1 I2C Bus

I2C is a two-wire serial bus invented by Philips:

SCL (clock line) ──── SCL
                  │
Master ←────────→ Slave (SSD1306, 0x3C)
                  │
SDA (data line) ──── SDA
  • SCL: The master generates the clock to control the transmission rhythm
  • SDA: Bidirectional data line, address + data + acknowledge

I2C requires two wires: SCL (clock) and SDA (data), both with the clock generated by the host. Each slave has a 7-bit address; the host sends the address first after START to select the target slave.

xxxxxxxxxx
Master write: START → Device address(7bit)+W(1bit) → ACK(1bit) → Register address(8bit) → ACK(1bit) → Data(8bit) → ACK(1bit) → STOP
Master read: START → Device address(7bit)+W(1bit) → ACK(1bit) → Register address(8bit) → ACK(1bit) →
         RESTART → Device address(7bit)+R(1bit) → ACK(1bit) → Data(8bit) → NAK(1bit) → STOP

In this experiment the I2C address is 0x3C (SSD1306 default), SCL frequency 100kHz (standard mode).

Each slave has a unique 7-bit address (SSD1306 default 0x3C).

2.2 SSD1306 Display Principle

The SSD1306 128×32 display area is divided into 4 pages, each page being 8 rows (128 columns × 8 rows/page):

​x
  Column 0 ................... 127
​
Page0 ┌─────────────────────────┐ ← Physical top (rows 0~7)
Page1 ├─────────────────────────┤ ← (rows 8~15, swapped with Page2)
Page2 ├─────────────────────────┤ ← (rows 16~23, swapped with Page1)
Page3 └─────────────────────────┘ ← Physical bottom (rows 24~31)

Key: The OLED traces on this board cause a physical swap between Page1 ↔ Page2, which is handled in the code.

Each column of 8 vertical pixels is represented by one byte: bit 0 = the bottommost pixel of that column, bit 7 = the topmost.

2.3 Font Rendering Principle

The glyph data uses row-major order: each data entry corresponds to a row of pixels. During rendering it is transposed column by column — taking the bits of the same column from each row, assembling them into a vertical 8-pixel byte, and writing them to the corresponding SSD1306 page.

xxxxxxxxxx
Row-major storage (11×18):           Transposed rendering (each column assembled into a vertical byte):
  row0: ███░░█████                  col0: ██  ← byte[0]: row0.col0 + ... + row7.col0
  row1: ██░░░████░                  col1: █░
  ...     ...                        ...
  row17:░░█░█░██░                     col10: ░█

This experiment provides 3 fonts: 7×10, 11×18, 16×26. Modify the OLED_FONT variable to switch, the default is '11x18'.

2.4 Core API

xxxxxxxxxx
from machine import Pin, I2C
​
# I2C initialization
i2c = I2C(0, scl=Pin(42), sda=Pin(41), freq=100000)
​
# SSD1306 operations
from I2C_OLED import ssd1306
oled = ssd1306.SSD1306(i2c, addr=0x3C)
oled.show_str(0, "HI YAHBOOM")           # Default 11x18 font
oled.show_str(0, "HELLO", font='16x26')  # Switch to 16x26 font
oled.show_str(0, "OK", font='7x10')      # Switch to 7x10 font
oled.clear()                               # Clear screen

3. Hardware Dependencies

3.1 Pin Mapping

GPIOPeripheralModeDescription
41I2C0 SDAI2CI2C0 data pin
42I2C0 SCLI2CI2C0 clock pin

3.2 OLED Parameters

ParameterValueDescription
Driver chipSSD1306Monochrome OLED driver IC
Resolution128×32128 columns × 32 rows
I2C address0x3C7-bit address (SA0=0)
I2C frequency100kHzStandard mode (SSD1306 supports up to 400kHz)
InterfaceI2COnly requires the two lines SDA + SCL

4. Project Architecture and Flow

4.1 File Architecture

xxxxxxxxxx
2. I2C_OLED/
├── main.py              # Project entry (initialize I2C → SSD1306 → display)
├── ssd1306.py           # SSD1306 driver layer (I2C commands → page/column addressing → transposed rendering)
└── fonts.py             # Font data layer (5 ASCII glyph bitmaps)
xxxxxxxxxx
Three-layer architecture:
  main.py  (application layer)
    ↓
  ssd1306.py (driver layer) — transposed rendering, page addressing, I2C TX/RX
    ↓
  fonts.py (data layer) — character bitmap data
    ↓
  machine.I2C (hardware abstraction) — I2C bus

4.2 Running Flow

xxxxxxxxxx
1. Upload ssd1306.py and fonts.py to ESP32-S3 (I2C_OLED directory)
2. Upload main.py, modify OLED_FONT to select the font, save and run
   ├─ I2C(0, scl=42, sda=41, freq=100kHz)   // Initialize I2C
   ├─ SSD1306(i2c, addr=0x3C)               // Initialize OLED
   │    ├─ Send the initialization sequence (about 15 commands)
   │    ├─ clear()  clear screen
   │    └─ Turn on the display
   └─ oled.show_str(0, "HI YAHBOOM", font=OLED_FONT)  // Render with the selected font

5. Source Code Details

Complete source code: 5.Source Code → MicroPython → 3.Serial Communication and Storage System → 2. I2C_OLED

5.1 Entry main.py — Font Selection

The font parameter of show_str(x, s, font) can be '7x10' / '11x18' / '16x26'; if not passed, it defaults to '11x18'. Modify the OLED_FONT variable to switch fonts.

xxxxxxxxxx
from machine import Pin, I2C          # Pin and I2C classes
from I2C_OLED import ssd1306, fonts   # SSD1306 driver + fonts
​
# ---- Hardware configuration ----
SDA_PIN = 41          # I2C data pin
SCL_PIN = 42          # I2C clock pin
OLED_ADDR = 0x3C      # SSD1306 7-bit address
​
# ---- Initialize ----
print('Init I2C...')
i2c = I2C(0, scl=Pin(SCL_PIN), sda=Pin(SDA_PIN), freq=100000)
​
print('Init SSD1306...')
oled = ssd1306.SSD1306(i2c, addr=OLED_ADDR)
​
# ---- Font selection ----
# show_str(x, s, font) — font: '7x10' | '11x18'(default) | '16x26'
OLED_FONT = '11x18'   # Change this to switch font
​
oled.show_str(0, 'HI YAHBOOM', font=OLED_FONT)
print('Font: %s' % OLED_FONT)

5.2 SSD1306 Driver (ssd1306.py)

I2C low-level TX/RX — command bytes are prefixed with 0x00, data bytes with 0x40:

xxxxxxxxxx
class SSD1306:
    def __init__(self, i2c, addr=0x3C):
        self.i2c = i2c          # I2C bus object
        self.addr = addr        # OLED I2C address
        self._init_seq()        # Run the init sequence
​
    def _cmd(self, c):
        self.i2c.writeto(self.addr, b'\x00' + bytes([c]))   # Command: prefix 0x00
​
    def _data(self, d):
        self.i2c.writeto(self.addr, b'\x40' + bytes([d]))   # Data: prefix 0x40

Initialization sequence — sends about 15 commands after power-up to configure the chip:

xxxxxxxxxx
    def _init_seq(self):
        import time
        time.sleep_ms(100)            # Power stable
​
        self._cmd(0xAE)               # Display OFF
        self._cmd(0xD5); self._cmd(0x80)    # Clock
        self._cmd(0xA8); self._cmd(0x1F)    # MUX=32 (128x32)
        self._cmd(0x8D); self._cmd(0x14)    # Charge pump ON
        self._cmd(0x20); self._cmd(0x02)    # Page addressing mode
        self._cmd(0xA1)                      # Segment remap (left/right flip)
        self._cmd(0xC0)                      # COM scan normal
        self._cmd(0xDA); self._cmd(0x12)    # COM pins configuration
        self._cmd(0x81); self._cmd(0xCF)    # Contrast
        self._cmd(0xA4)                      # Entire display ON
        self._cmd(0xA6)                      # Normal display (no inversion)
        self.clear()                         # Fill the entire screen with 0
        self._cmd(0xAF)                      # Display ON
        time.sleep_ms(100)

Page positioning — SSD1306 128×32 = 4 pages × 128 columns; set the page number + column address before writing data:

xxxxxxxxxx
    def set_pos(self, page, col):
        self._cmd(0xB0 + page)        # Page base + page num
        self._cmd(col & 0x0F)         # Column low nibble
        self._cmd(0x10 | (col >> 4))  # Column high nibble
​
    def clear(self):
        for p in range(4):
            self.set_pos(p, 0)
            for _ in range(128):
                self._data(0x00)

Transposed rendering — the glyph data is row-major (each entry = horizontal bits of one row of pixels), but SSD1306 needs vertical bytes. Traverse column by column, take the bits of the same column from each row, and assemble them into one vertical 8-pixel byte:

xxxxxxxxxx
    def _render_page(self, x, data, w, sr, n, pg):
        self.set_pos(pg, x)
        for col in range(w):                # Per column
            b = 0
            for r in range(n):               # Per row
                if _glyph_bit(data, sr + r, col):
                    b |= (1 << (n - 1 - r))  # Row bit → vertical byte (top row = high bit)
            self._data(b)                     # Write to this column

Character drawing — taking the 11×18 font as an example. Since the OLED traces on this board cause a physical swap between Page1↔Page2, the rendering deliberately writes rows 0~7 to Page2 and rows 8~15 to Page1:

xxxxxxxxxx
    def show_11x18(self, x, ch):
        d = fonts.Font_11x18_data
        i = (ord(ch) - ord(' ')) * 18       # 18 entries per character, indexed from space
        self._render_page(x, d[i:], 11, 0, 8, 2)   # rows 0-7 → Page2 (swapped)
        self._render_page(x, d[i:], 11, 8, 8, 1)   # rows 8-15 → Page1 (swapped)
        self._render_page(x, d[i:], 11, 16, 2, 3)  # rows 16-17 → Page3

String interface — the font parameter switches fonts; if not passed, it defaults to 11x18:

xxxxxxxxxx
    def show_str(self, x, s, font='11x18'):
        draw = {
            '7x10':  (self.show_7x10,  7),
            '11x18': (self.show_11x18, 11),
            '16x26': (self.show_16x26, 16),
        }.get(font, (self.show_11x18, 11))
​
        for ch in s:
            draw[0](x, ch)                  # Call the character drawing function for the corresponding font
            x += draw[1]                    # Step by the character width

5.3 Font Data (fonts.py)

ASCII lookup tables for the 3 fonts (space to ~), stored as arrays. Each entry is 16-bit; character index = (ord(ch) - ord(' ')) × number of entries:

FontWidth×Height (px)Entries/CharacterFormat
Font_7x107×1010Row-major
Font_11x1811×1818Row-major
Font_16x2616×2626Row-major
xxxxxxxxxx
# 3 ASCII font bitmap data
Font_7x10_data = [
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x1000,0x1000, ...       # 10 rows of pixel data for '!'
]
​
Font_11x18_data = [
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0C00,0x0C00, ... # 18 rows of pixel data for '!'
]
​
# Font_16x26_data — 26 entries/character, row-major

6. Operation Procedure

6.1 File Upload

Thonny IDE operation steps: See MicroPython → 1. Before Development → 3.Thonny IDE.

  1. In Thonny IDE, upload ssd1306.py and fonts.py to ESP32-S3

    ①Select the file → Save as...

    Step 1: Save file as

    ②Select MicroPython device

    Step 1: Save file as

    ③Name the file or create a new folder (name the file in the new folder)

    Step 3: Create a folder and enter the file name

    ④Wait for the cache

    Step 4: Wait for the cache

  2. Create the I2C_OLED directory on the device and move the two files into it

    Note: The library name and the name of the newly created folder must correspond, and the names of the .py files placed inside must also correspond one by one.

    Final result

  3. Run main.py

6.2 Serial Output Example

xxxxxxxxxx
Init I2C...
Init SSD1306...
Font: 11x18

Serial print

6.3 Expected Behavior

The OLED screen displays "HI YAHBOOM". Modify the OLED_FONT variable (e.g. '7x10' / '16x26', etc.) to switch the font size.

Display

6.4 Verification Method

ActionExpected result
Run main.pyOLED displays "HI YAHBOOM"
Change "HI YAHBOOM" to "HELLO"Display changes to "HELLO"
Change OLED_ADDR=0x3DOLED does not display (wrong address)
Run oled.clear() firstClears the screen, then displays
OLED_FONT = '16x26'Font becomes larger; 16×26 can only display 8 characters
OLED_FONT = '7x10'Font becomes smaller; 7×10 can display 18 characters per line

7. Common Issues

SymptomCauseSolution
OLED does not light upWrong I2C addressThe I2C address is usually 0x3C (some modules are 0x3D); check the module silkscreen
Garbled characters or abnormal displayPage mapping errorConfirm the Page1/Page2 swap logic in ssd1306.py matches the hardware
ImportError: no module named 'I2C_OLED'Files not uploaded or directory missingCreate the I2C_OLED directory on the device and put ssd1306.py and fonts.py into it
Screen jitter or flickerI2C frequency too highLower freq= to 50000 or 100000
I2C cannot scan the deviceWiring error or not poweredUse a multimeter to check SDA/SCL continuity and confirm the OLED is powered properly
Serial UART Communication Experiment
SPI Bus Communication Experiment

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文