I2C OLED (SSD1306/SSD1315) screen with Raspberry Pi Pico 2 (RP2350) using MicroPython
This video show steps to install ssd1306@micropython-lib OLED driver on
Raspberry Pi Pico 2 (RP2350) running MicroPython v1.24.0-preview.201 to display with
0.96 inch 128x64 SSD1315 I2C OLED (SSD1306 compatible)
and
0.91 inch 128x32 SSD1306 I2C OLED. Also compare with another driver micropython-ssd1306@PyPI.
Connection:
I2C OLED Pico
====================
SDA GP4
SCL GP5
VCC 3V3
GND GND
mpy_i2c_scanner.py, I2C devices scanner.
import machine
import os
print(os.uname())
print()
# How many hardware I2C supported
# and show the I2C info
print("number of I2C supported:")
print("========================")
i=0
while True:
try:
print(machine.I2C(i))
except ValueError as e:
print(e)
break
i += 1
print()
#Using machine.SoftI2C
#scl = machine.Pin(9, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP)
#sda = machine.Pin(8, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP)
#i2c = machine.SoftI2C(scl, sda)
#Using machine.I2C
i2c = machine.I2C(0)
print(i2c)
print()
print('=== I2C Scanner ===')
devices = i2c.scan()
if len(devices) == 0:
print("No I2C device found!")
else:
print('I2C devices found:',len(devices))
for device in devices:
print(device, ":", hex(device))
Exercises using ssd1306.py, notice that SSD1306 subclassing FrameBuffer provides support for graphics primitives.
ref: http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
mpyPico2_SSD1306_i2c_hello.py
"""
MicroPython/Raspberry Pi Pico 2 exercise
display on 0.96" 128x64 SSD1306/SSD1306 I2C OLED.
(It should work on both Pico/Pico 2)
Install ssd1306 @ micropython-lib SSD1306 OLED driver (ver: 0.1.0)
in Thonny > Tools > Manager packages...
OLED is connected to I2C(0)
SCL - GP5
SDA - GP4
"""
import sys
import os
import time
import ssd1306
DISP_WIDTH=128
DISP_HEIGHT=64
print("====================================")
print(sys.implementation[0], os.uname()[3],
"\nrun on", os.uname()[4])
print("====================================")
package_info = ssd1306.__name__
try:
package_info = package_info + " ver:" + ssd1306.__version__
except AttributeError as exc:
print("AttributeError!", exc)
print("ssd1306 package: ", package_info)
print("====================================")
oled_i2c = machine.I2C(0)
print("Using I2C:", oled_i2c)
print()
try:
oled_ssd1306 = ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, oled_i2c)
print("Default SSD1306 I2C address:",
oled_ssd1306.addr, "/", hex(oled_ssd1306.addr))
oled_ssd1306.fill(1)
oled_ssd1306.show()
time.sleep(2)
oled_ssd1306.fill(0)
oled_ssd1306.show()
time.sleep(2)
oled_ssd1306.rect(0, 0, DISP_WIDTH, DISP_HEIGHT, 1)
oled_ssd1306.text('Hello, World!', 5, 5, 1)
oled_ssd1306.show()
except OSError as exc:
print("OSError!", exc)
if exc.errno == errno.ENODEV:
print("No such device")
print("~ bye ~")
mpyPico2_SSD1306_i2c_hello_fb.py
"""
MicroPython/Raspberry Pi Pico exercise
display on 0.96" 128x64 SSD1306/SSD1306 I2C OLED, using FrameBuffer.
(It should work on both Pico/Pico 2)
Install ssd1306 @ micropython-lib SSD1306 OLED driver (ver: 0.1.0)
in Thonny > Tools > Manager packages...
OLED is connected to I2C(0)
SCL - GP5
SDA - GP4
"""
import sys
import os
import time
import ssd1306
import framebuf
DISP_WIDTH=128
DISP_HEIGHT=64
print("====================================")
print(sys.implementation[0], os.uname()[3],
"\nrun on", os.uname()[4])
print("====================================")
package_info = ssd1306.__name__
try:
package_info = package_info + " ver:" + ssd1306.__version__
except AttributeError as exc:
print("AttributeError!", exc)
print("ssd1306 package: ", package_info)
print("====================================")
oled_i2c = machine.I2C(0)
print("Using I2C:", oled_i2c)
print()
# Raspberry Pi logo as 32x32 bytearray
fb_width=32
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)
"""
If you install another lib micropython-ssd1306 @ PyPI ssd1306 module for MicroPython (ver 0.3),
it's lack of rotate() function.
This function (rotate_screen()) is special handle it for information only, just skip rotate if:
AttributeError! 'SSD1306_I2C' object has no attribute 'rotate'
"""
def rotate_screen(rot=False):
try:
oled_ssd1306.rotate(rot)
except AttributeError as exc:
print("AttributeError!", exc)
def demo():
oled_ssd1306.fill(0)
oled_ssd1306.rect(0, 0, DISP_WIDTH, DISP_HEIGHT, 1)
oled_ssd1306.text('Hello', 5, 5, 1)
oled_ssd1306.text('coXXect', 5, 15, 1)
oled_ssd1306.show()
time.sleep(2)
oled_ssd1306.blit(fb, DISP_WIDTH-fb_width, 0)
oled_ssd1306.show()
time.sleep(1)
for i in range(DISP_WIDTH-fb_width, 0-1, -1):
oled_ssd1306.blit(fb, i, 0)
oled_ssd1306.show()
time.sleep(0.1)
try:
oled_ssd1306 = ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, oled_i2c)
print("Default SSD1306 I2C address:",
oled_ssd1306.addr, "/", hex(oled_ssd1306.addr))
rotate_screen(False) #oled_ssd1306.rotate(False)
oled_ssd1306.invert(0)
oled_ssd1306.fill(1)
oled_ssd1306.show()
time.sleep(2)
oled_ssd1306.fill(0)
oled_ssd1306.show()
time.sleep(2)
demo()
rotate_screen(True) #oled_ssd1306.rotate(True)
oled_ssd1306.invert(1)
demo()
time.sleep(1)
oled_ssd1306.invert(0)
time.sleep(1)
rotate_screen() #oled_ssd1306.rotate(False)
oled_ssd1306.invert(0)
oled_ssd1306.fill(0)
step_width = fb_width+2
for i in range(0, DISP_WIDTH, step_width):
oled_ssd1306.blit(fb, i, 0)
oled_ssd1306.show()
time.sleep(0.5)
except OSError as exc:
print("OSError!", exc) # may be no connected to I2C OLED
print("~ bye ~")
Remark:
I have a old post of Raspberry Pi Pico/MicroPython exercise using SSD1306 I2C OLED. But the link to MicroPython ssd1306 driver (ssd1306.py) lost, so I re-post the steps and improved code here.
Next:
~ Raspberry Pi Pico 2/MicroPython display on SPI SSD1306 OLED, with exercises to display polygon.
~ Raspberry Pi Pico 2 (RP2350)/MicroPython display on two OLED, SSD1306 I2C and SPI.
Comments
Post a Comment