Using OLED Displays with MicroPython and the Pico

Running a Pico without showing something may work for sensors pushing to Homeassistant, but for all other usecases we want to show something on a display. There are a lot of different displays out there. A lot of them are compatible to SSD1306 or SH1106.

So we need the library for SSD1306 on the Pico. For instructions on how to get MicroPython an the Pico and use the REPL see a previous post. We get the Python library we need from the MicroPython Github because it is not bundled in MicroPython itself. Download the file and copy it to the Pico with cp ssd1306.py /pyboard.

First display has 128x32 pixels from Waveshare.

import machine
import ssd1306
i2c = machine.I2C(0, sda=machine.Pin(20), scl=machine.Pin(21))
display = ssd1306.SSD1306_I2C(128, 32, i2c)
# show a simple text
display.text('Hello World', 0, 0, 1)
display.show()

More on the possible functions in the MicroPython Tutorial for the ssd1306.

img1

The same works for a 128x64 I2C display. A small change is needed when initializing the display: change 32 into 64.

I tried a different library for SH1106. As SH1106 library I tested one from robert-hh on Github. Both libraries worked with both types of displays, so the difference seems to be very minimal.

This library has more options, i.e. set a rotation for the display in degrees.

import machine
import sh1106
i2c = machine.I2C(0, sda=machine.Pin(20), scl=machine.Pin(21))
display = sh1106.SH1106_I2C(128, 64, i2c, machine.Pin(16), 0x3c, rotate=180)
display.sleep(False)
display.fill(0)
display.text("Hello World", 0, 0, 1)
display.show()
Another display I have a is 1.3" 128x64 Waveshare with a default to 4-wire SPI.
So for this we need SPI instead of I2C.
The pinout for this display is:
- 3v      - Vcc
- GND     - Gnd
- GPIO 16 - CS
- GPIO 17 - D/C
- GPIO 18 - CLK / SCLK
- GPIO 19 - DIN / MOSI
- GPIO 20 - RES

The MicroPython code to use the display:

import machine
import sh1106
spi = machine.SPI(0, 100000, mosi=machine.Pin(19), sck=machine.Pin(18))
display = sh1106.SH1106_SPI(128, 64, spi, machine.Pin(17), machine.Pin(20), machine.Pin(16), rotate=180)
display.sleep(False)
display.fill(0)
display.text("Hello World!", 0, 0, 1)
display.show()

img2

This covers the different types of OLED displays I have.