Skip to main content

Getting Started with CircuitPython

CircuitPython is a great way to quickly and easily get started with programming microcontrollers and learn Python. In this tutorial, we will show you how to use CircuitPython on your senseBox MCU-S2.

CircuitPython Logo

Editor for CircuitPython

CircuitPython can be used with various editors. In this example, we use the CircuitPython Editor. The CircuitPython Editor is an online editor specifically developed for programming CircuitPython. You can use the editor directly in your web browser without needing to install any software.

Hello World Program

To verify that CircuitPython works, we'll show you a simple Hello World program.

  1. Visit the CircuitPython Editor.
  2. Connect your senseBox MCU-S2 to your computer using a USB cable.
  3. Connect to your senseBox MCU-S2 via USB.
  4. Click on Connect to Device and select senseBox MCU-S2 ESP32S2 as the device. You may need to allow the editor to access your senseBox MCU-S2.
  5. Click on Select New Folder and choose the CIRCUITPY folder.
  6. You may need to allow file editing.
  7. Click on Use CIRCUITPY.
  8. Click on Open and open the code.py file.
  9. The file should already contain some example code. If not, insert the following code:
print("Hello World!")
  1. Click on Save + Run. You should now see Hello World! in the console.

Display Output on the OLED Display

Now we want to display something on the OLED display of the senseBox MCU-S2. For this, we use the SSD1306 CircuitPython library and the adafruit_display_text library. Extract the .zip files and copy the .mpy files from the lib folders to the lib folder of your senseBox (CIRCUITPY drive).

Here is an example code that displays "Hi senseBox! :)" on the display:

import board
import displayio
import digitalio
import terminalio
from adafruit_display_text import label
import adafruit_displayio_ssd1306

# IO Enable
io_enable_pin = digitalio.DigitalInOut(board.IO_POWER)
io_enable_pin.direction = digitalio.Direction.OUTPUT
io_enable_pin.value = False

displayio.release_displays()

# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D)

WIDTH = 128
HEIGHT = 64
BORDER = 5

display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT)

# Make the display context
splash = displayio.Group()
display.root_group = splash

color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF # White

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(WIDTH - BORDER * 2, HEIGHT - BORDER * 2, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000 # Black
inner_sprite = displayio.TileGrid(
inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
)
splash.append(inner_sprite)

# Draw a label with shorter text
text = "Hi senseBox!"
text_area = label.Label(
terminalio.FONT, text=text, color=0xFFFFFF, x=12, y=HEIGHT // 2 - 1
)
splash.append(text_area)

# Draw smiley face on the right side
smiley_palette = displayio.Palette(1)
smiley_palette[0] = 0xFFFFFF # White

# Left eye
left_eye = displayio.Bitmap(4, 4, 1)
left_eye_sprite = displayio.TileGrid(left_eye, pixel_shader=smiley_palette, x=95, y=20)
splash.append(left_eye_sprite)

# Right eye
right_eye = displayio.Bitmap(4, 4, 1)
right_eye_sprite = displayio.TileGrid(right_eye, pixel_shader=smiley_palette, x=110, y=20)
splash.append(right_eye_sprite)

# Mouth
mouth = displayio.Bitmap(25, 3, 1)
mouth_sprite = displayio.TileGrid(mouth, pixel_shader=smiley_palette, x=92, y=40)
splash.append(mouth_sprite)

# Mouth corners
left_corner = displayio.Bitmap(3, 3, 1)
left_corner_sprite = displayio.TileGrid(left_corner, pixel_shader=smiley_palette, x=90, y=38)
splash.append(left_corner_sprite)

right_corner = displayio.Bitmap(3, 3, 1)
right_corner_sprite = displayio.TileGrid(right_corner, pixel_shader=smiley_palette, x=114, y=38)
splash.append(right_corner_sprite)

while True:
pass