Raspberry Pico

using two different oled types with the raspberry pico

I was lucky enough to get hold of a couple of raspberry pico’s early to have a play around with and although I never got around to doing what i’d originally planned for at least one of them (Kansas City Standard cassette emulation/tape reading) I did find time to play around with a couple of different oleds. A bog standard ssd1306 based one (actually two as I have both a 128x32 one & a 128x64 one) and an sh1106 based one (part of the Pimoroni Breakout Garden ecosystem btw if you do much breadboarding and/or use multiple SBCs & MCs get yourself a few packs of the Pimoroni Breakout Garden extenders.

)

for an ssd1306 oled you need to install the python module micropython-ssd1306 from pypi. if you are using the thonny ide then the package manager in the ide can grab and install it for you.

once you’ve installed the module then you need to wire up your oled. for messing around I used the “default” I2C pins on the pico. physical pins 11&12 labelled GP8&GP9 for SDA&SCL and physical pins 38&36 labelled GND&3V3(OUT) for power&ground

[a good wiring image is in the "Get started with MicroPython on Raspberry Pi Pico" book available at https://hackspace.raspberrypi.org/books/micropython-pico ]

# Display Image & text on I2C driven ssd1306 OLED display 
from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
import framebuf
import utime
sensor_temp = ADC(4)
conversion_factor = 3.3 / (65535)
WIDTH  = 128                                            # oled display width
HEIGHT = 32                                             # oled display height
i2c = I2C(0)                                            # Init I2C using I2C0 defaults, SCL=Pin(GP9), SDA=Pin(GP8), freq=400000
print("I2C Address      : "+hex(i2c.scan()[0]).upper()) # Display device address
print("I2C Configuration: "+str(i2c))                   # Display I2C config
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)                  # Init oled display
# Raspberry Pi logo as 32x32 bytearray
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
# Load the raspberry pi logo into the framebuffer (the image is 32x32)
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
# Clear the oled display in case it has junk on it.
oled.fill(0)
# Blit the image from the framebuffer to the oled display
oled.blit(fb, 96, 0)
# Add some text
oled.text("Raspberry Pi",5,5)
oled.text("Pico",5,15)
# Finally update the oled display so the image & text is displayed
oled.show()
while True:
    reading = sensor_temp.read_u16() * conversion_factor
    temperature = 27 - (reading - 0.706)/0.001721
    oled.text("Temp: "+str(round(temperature, 2)), 5,23)
    oled.show()
    utime.sleep(2)
    oled.text("Temp: "+str(round(temperature, 2)), 5,23,0)
    oled.show()

The above code will print a raspberry pi logo, some text and will loop forever (until ctrl-c ’d ) printing the rp2040’s temperature as shown in one of the photos at the top of this post.

for an sh1106 based oled such as the Pimoroni 1.12" Mono OLED (128x128, white/black) Breakout – I2C

get the i2c variant as this display comes in both an i2c and spi version and i’ve not had much success with getting the spi version working correctly yet.

unfortunately the module needed to drive this display is not available (afaik) on pypi or via thonny’s package manager so you’ll need to grab and install the file manually from https://github.com/robert-hh/SH1106

Although I used the Pimoroni breakout garden extenders for the display

I again wired it up the same way I did with the ssd1306 oled e.g. the “default” I2C pins on the pico. physical pins 11&12 labelled GP8&GP9 for SDA&SCL and physical pins 38&36 labelled GND&3V3(OUT) for power&ground.

The following code is basically the same code as for the ssd1306 above with just the i2c device changed and will print a raspberry pi logo, some text and will loop forever (until ctrl-c ’d ) printing the rp2040’s temperature as shown in one of the photos at the top of this post.

# Display Image & text on I2C driven sh1106 OLED display 
from machine import Pin, I2C, ADC
from sh1106 import SH1106_I2C
import framebuf
import utime
sensor_temp = ADC(4)
conversion_factor = 3.3 / (65535)
WIDTH  = 128                                            # oled display width
HEIGHT = 128                                            # oled display height
i2c = I2C(0, scl=Pin(9), sda=Pin(8),freq=400000)
oled = SH1106_I2C( WIDTH, HEIGHT, i2c)                   # Init oled display
# Raspberry Pi logo as 32x32 bytearray
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
# Load the raspberry pi logo into the framebuffer (the image is 32x32)
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
# Clear the oled display in case it has junk on it.
oled.fill(0)
# Blit the image from the framebuffer to the oled display
oled.blit(fb, 96, 0)
# Add some text
oled.text("Raspberry Pi",5,5)
oled.text("Pico",5,15)
# Finally update the oled display so the image & text is displayed
oled.show()
while True:
    reading = sensor_temp.read_u16() * conversion_factor
    temperature = 27 - (reading - 0.706)/0.001721
    oled.text("Temp: "+str(round(temperature, 2)), 5,23)
    oled.show()
    utime.sleep(2)
    oled.text("Temp: "+str(round(temperature, 2)), 5,23,0)
    oled.show()

as i’ve sort of fallen down a i2c display rabbithole with a side tunnel of other i2c devices and a fork for spi devices i’ll be writing up more blog posts on the raspberry pico and i2c/spi over the next few months as and when I get time to have a play.