Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Last active January 7, 2025 22:50
Show Gist options
  • Save stonehippo/1cf89e4c5e8152ba17da10c71f21aa85 to your computer and use it in GitHub Desktop.
Save stonehippo/1cf89e4c5e8152ba17da10c71f21aa85 to your computer and use it in GitHub Desktop.
Basic setup of an SSD1306-based I2C display using CircuitPython Display
import board
import displayio
import terminalio
import adafruit_displayio_ssd1306
from adafruit_display_text import label
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
# release any displays that have already been set up
displayio.release_displays()
display_address = 0x3c
display_height = 32
display_width = 128
# set up the I2C connection to the display (edit the address as required)
display_bus = displayio.I2CDisplay(board.STEMMA_I2C(), device_address=display_address)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=display_width, height=display_height)
# create a main displayio group and attach it to the display
display_group = displayio.Group()
display.root_group = display_group
layout_offset_x = 0
layout_offset_y = 0
# create a layout for the display
layout = GridLayout(
x=layout_offset_x,
y=layout_offset_y,
width=display_width - (layout_offset_x * 2),
height=display_height - (layout_offset_y * 2),
grid_size=(2, 2),
cell_padding=0,
divider_lines = False
)
font = terminalio.FONT
color = 0xffffff
first_label = label.Label(font, text="First!", color=color)
layout.add_content(first_label, grid_position=(0, 0), cell_size=(1, 1))
last_label = label.Label(font, text="Last", color=color)
layout.add_content(last_label, grid_position=(1,1), cell_size=(1,1))
display_group.append(layout)
while True:
pass
@stonehippo
Copy link
Author

This is a basic example, showing how to set up displayio to work with an SSD1306-based display. It also illustrates using labels from adafruit_display_text, and the GridLayout from adafruit_displayio_layout.

I first wrote and tested this on CircuitPython 7.3.2, but it should work with most versions from CP 4.0 on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment