Using Displays with MicroPython and the Pico

Running a Pico without showing something may work for sensors pushing to Homeassistant or getting pulled from 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 and we will today use two kinds of them.

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. So we download the file and copy it to the Pico with cp /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)

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


The same works for a 128x64, only changing the 32 into 64 when initializing the display.

I tried a different library for SH1106. As SH1106 library I tested on 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.text("Hello World", 0, 0, 1)

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 code for my 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.text("Hello World!", 0, 0, 1)


This covers the OLED displays I have.