from machine import Pin, I2C
import utime
# Initialize I2C for OLED
i2c = I2C(0, scl=Pin(1), sda=Pin(0)) # GP1 for SCL, GP0 for SDA
# OLED Address (commonly 0x3C)
OLED_ADDR = 0x3C
# Define OLED commands
OLED_WIDTH = 128
OLED_HEIGHT = 64
OLED_PAGES = OLED_HEIGHT // 8
def oled_command(cmd):
i2c.writeto(OLED_ADDR, b'\x00' + bytearray([cmd]))
def oled_data(data):
i2c.writeto(OLED_ADDR, b'\x40' + bytearray(data))
def oled_init():
oled_command(0xAE) # Display off
oled_command(0xA8) # Set multiplex ratio
oled_command(0x3F)
oled_command(0xD3) # Set display offset
oled_command(0x00)
oled_command(0x40) # Set start line address
oled_command(0x8D) # Charge pump
oled_command(0x14)
oled_command(0x20) # Set memory addressing mode
oled_command(0x00) # Horizontal addressing mode
oled_command(0xA1) # Set segment re-map
oled_command(0xC8) # Set COM output scan direction
oled_command(0xDA) # Set COM pins hardware configuration
oled_command(0x12)
oled_command(0x81) # Set contrast control
oled_command(0x7F)
oled_command(0xD9) # Set pre-charge period
oled_command(0xF1)
oled_command(0xDB) # Set VCOMH deselect level
oled_command(0x40)
oled_command(0xA4) # Entire display ON (resume)
oled_command(0xA6) # Set normal display
oled_command(0xAF) # Display ON
def oled_clear():
for page in range(OLED_PAGES):
oled_command(0xB0 + page) # Set page start address
oled_command(0x00) # Set lower column start address
oled_command(0x10) # Set higher column start address
oled_data([0x00] * OLED_WIDTH)
def oled_display_bit_array(bit_array):
for page in range(OLED_PAGES):
oled_command(0xB0 + page)
oled_command(0x00)
oled_command(0x10)
start = page * OLED_WIDTH
end = start + OLED_WIDTH
oled_data(bit_array[start:end])
# Define bit array for custom logo (example with a simple pattern)
s_logo = [
0, 0, 0, 248, 252, 252, 252, 254, 254, 254, 254, 254, 254, 254, 254, 254,
254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 252, 252, 252,
248, 240, 240, 224, 192, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 192, 224, 240, 248,
248, 252, 252, 252, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
254, 254, 254, 254, 254, 254, 254, 254, 254, 252, 252, 252, 248, 0, 0, 0,
0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 254, 252, 248, 240, 224, 224, 192, 128, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 128, 128, 192, 224, 240, 248, 252, 254, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
254, 252, 248, 240, 224, 224, 192, 128, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 192, 224, 240, 248, 252, 254,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 1,
3, 7, 15, 31, 63, 63, 127, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 248, 240, 224, 224, 192,
128, 192, 224, 240, 248, 252, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127, 63, 31, 15, 7, 3,
3, 1, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 7, 15, 31, 63, 127, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 127, 63, 31, 15, 7, 7, 3, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 3, 7, 15, 31, 63, 127, 127, 127, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 127, 127, 63, 31, 15, 7, 7, 3, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 31, 31, 63, 63, 63, 63, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 63, 63, 63, 63, 63, 31, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 31, 63, 63, 63, 63, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 63, 63, 63, 63, 31, 0, 0, 0
]
# Initialize OLED and clear it
oled_init()
oled_clear()
# Display custom logo
oled_display_bit_array(s_logo)
# Keep the display on
while True:
utime.sleep(1)