oled-i2c.py

Raspberry Pi Pico W · File : oled-i2c.py · Language : Python · Use case : Afficher texte et compteur sur un écran OLED I2C 0,96"

A 0.96" SSD1306 OLED (128x64) wired in I2C shows text and a counter. The script first scans the bus to confirm the module is present, then uses the ssd1306.py driver shipped with MicroPython.

Hardware

A Raspberry Pi Pico W (RP2040 + 2.4 GHz Wi-Fi radio chip) with MicroPython installed, powered over USB. 1 SSD1306 I2C OLED module (4 pins: VCC, GND, SCL, SDA), address usually 0x3C.

Wiring

SDA to GP4, SCL to GP5, VCC to 3V3, common GND. Pull-up resistors are usually already on the module.

How it works

I2C(0, ...) sets up the hardware controller; i2c.scan() lists answering addresses. The driver builds the 128x64 frame in RAM then oled.show() transfers it in one block.

Running it

Install the « Pico W » MicroPython build (BOOTSEL + .uf2), copy the script via Thonny. Since the user LED sits behind the radio chip, output examples use an external LED.

Variants

Switch to SPI for faster refresh, or display the internal temperature by combining with the matching ADC script.

oled-i2c.py

# OLED SSD1306 128x64 en I2C avec le pilote ssd1306.py de MicroPython.
from machine import I2C, Pin
import ssd1306
import time

# I2C0 : SDA = GP4, SCL = GP5 (broches arbitraires mais classiques).
i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400000)

# Verifier la presence du module (adresse usuelle 0x3C) :
trouves = i2c.scan()
print("Peripheriques I2C :", [hex(a) for a in trouves])

oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.text("Bonjour !", 0, 0, 1)
oled.text("I2C 0x3C", 0, 10, 1)
oled.show()
time.sleep(2)

n = 0
while True:
    oled.fill(0)
    oled.text("Compteur", 0, 0, 1)
    oled.text(str(n), 0, 20, 1)
    oled.text("RP2 MicroPython", 0, 44, 1)
    oled.show()          # transfere l'ecran entier
    n += 1
    time.sleep(1)

← Back to the board