The OLED used in this evaluation is a 0.96-inch 128*64 screen, the driver chip is SSD1306, and it is driven through the I2C interface.
I2C library for MicroPython
MicroPython provides hardware and software I2C. The operation routine of hardware I2C is as follows:
from machine import I2C
i2c = I2C(0,freq=400000) # create I2C peripheral at frequency of 400kHz
# depending on the port, extra parameters may be required
# to select the peripheral and/or pins to use
i2c.scan() # scan for peripherals, returning a list of 7-bit addresses
i2c.writeto(42, b'123') # write 3 bytes to peripheral with 7-bit address 42
i2c.readfrom(42, 4) # read 4 bytes from peripheral with 7-bit address 42
OLED Driving
When driving OLED, the screen driving function is also required. Here is a post from forum member "Hardcore Wang". The original post address is: [Digi-Key Follow me Issue 1] Second Post Driving Peripherals: OLED, Buzzer - Digi-Key Technology Zone - Electronic Engineering World Forum (eeworld.com.cn)
This driver function is a subclass of MicroPython's framebuf class, so some functions of this class can be used. The usage of this class can be found in the following URL.
framebuf — Framebuf operations — MicroPython Chinese 1.17 documentation
The framebuf class provides simple filling methods for the screen buffer, such as drawing methods for lines, fills, rectangles, characters, etc.
After the buffer is filled, you need to call the show method to refresh the display.
The driver routine is as follows:
from ssd1306 import SSD1306_I2C
from machine import Pin, I2C
from utime import sleep
import framebuf
i2c = I2C(1)
oled = SSD1306_I2C(128,64,i2c)
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")
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
oled.blit(fb, 96, 0)
oled.text("Raspberry Pi",5,5)
oled.text("Pico",5,15)
oled.text("Hello",5,25)
oled.show()
This realizes the OLED display.