System Architecture & Gameplay Loop
HU UniversityAs part of the 1st year Technical Computer Science (TI) curriculum at Hogeschool Utrecht, this embedded systems project explores low-level hardware control, timing synchronization, and analog-to-digital signal decoding. The game runs entirely on a Raspberry Pi Pico (RP2040) microcontroller without requiring any operating system.
The game features toroidal wrap-around boundaries ((px ± 1) % 8), automated food target generation preventing coordinate collision with the player, and a non-blocking asynchronous blinking routine using utime.ticks_ms() to distinguish the player's position from the static target dot on the monochrome LED matrix.
Hardware Components & Pin Configuration
Embedded IOThe hardware setup interfaces an 8x8 LED matrix display, discrete score indicator LEDs, and an analog input button ladder through dedicated GPIO and SPI buses on the Raspberry Pi Pico:
MAX7219 8x8 LED Matrix
64 individually addressable bright red LEDs controlled via hardware SPI (SPI0: SCK on Pin 2, MOSI on Pin 3, CS on Pin 5) at a high-speed 10 MHz clock rate.
ADC Resistor Ladder Keypad
5 directional and action tactile switches routed through a resistor voltage divider into a single 16-bit analog input pin (ADC on Pin 26).
5x Score Indicator LEDs
5 discrete external LEDs mapped to GPIO pins 20, 16, 17, 18, and 19 acting as a progressive visual progress bar for scores 1 through 5.
Onboard Status LED
The built-in Pico LED provides instant visual feedback during boot countdown sequences and stop-command counters.
Single-Pin ADC Button Decoding
Voltage DividerTo conserve GPIO pins on the microcontroller, five physical switches (Left, Up, Down, Right, and SW5 / Stop) are wired into a resistor ladder network. When any button is pressed, it forms a unique voltage divider that outputs a distinct raw 16-bit ADC value (ranging from 0 to 65535) into Pin 26:
The software implements deadbands between voltage ranges to prevent false triggers due to minor resistor tolerances or electrical noise, ensuring reliable input registration.
MicroPython Implementation
Source Code
Here is the core game implementation (main.py), showcasing the hardware initialization, directional calculation, score progression, and LED matrix rendering loop:
from machine import Pin, ADC, SPI
import max7219
import utime
import random
# Hardware SPI configuration for MAX7219 8x8 LED Matrix
spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3))
cs = Pin(5, Pin.OUT)
display = max7219.Matrix8x8(spi, cs, 1)
# Analog input for resistor-ladder button keypad
adc = ADC(Pin(26))
pico_led = Pin("LED", Pin.OUT)
# 5 discrete score meter LEDs
led_pins = [20, 16, 17, 18, 19]
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
def spawn_target(px, py):
"""Spawns food at random coordinates, guaranteeing no overlap with player."""
while True:
tx = random.randint(0, 7)
ty = random.randint(0, 7)
if tx != px or ty != py:
return tx, ty
def handle_movement(val, px, py):
"""Decodes 16-bit ADC voltage into 4-way direction vectors with wrap-around."""
moved = False
if val < 60000:
if 13000 <= val <= 18000: # Left
px = (px - 1) % 8
moved = True
elif 20000 <= val <= 25000: # Up
py = (py - 1) % 8
moved = True
elif 30000 <= val <= 33000: # Down
py = (py + 1) % 8
moved = True
elif 38000 <= val <= 43000: # Right
px = (px + 1) % 8
moved = True
return px, py, moved
def handle_stop_button(val, pressed_state, count):
"""Monitors SW5 button presses and stops game after 5 triggers."""
stop_game = False
if 50000 <= val <= 54000:
if not pressed_state:
count += 1
pressed_state = True
pico_led.value(1)
elif val > 60000:
if pressed_state:
pressed_state = False
pico_led.value(0)
if count >= 5:
stop_game = True
return pressed_state, count, stop_game
def start_sequence():
"""Flashes onboard and external LEDs to signify round countdown."""
pico_led.value(1)
for led in leds:
led.value(1)
utime.sleep(1.5)
pico_led.value(0)
for led in leds:
led.value(0)
# Main Game Loop
while True:
pico_led.value(0)
display.fill(0)
display.show()
for led in leds:
led.value(0)
# Wait for initial button trigger to start
while 50000 <= adc.read_u16() <= 54000:
utime.sleep(0.1)
while True:
if 50000 <= adc.read_u16() <= 54000:
break
utime.sleep(0.1)
while 50000 <= adc.read_u16() <= 54000:
utime.sleep(0.1)
start_sequence()
player_x = 3
player_y = 3
score = 0
stop_count = 0
sw5_pressed = False
target_x, target_y = spawn_target(player_x, player_y)
player_visible = True
last_blink_time = utime.ticks_ms()
blink_interval = 400
while True:
current_time = utime.ticks_ms()
# Non-blocking player head blink
if utime.ticks_diff(current_time, last_blink_time) > blink_interval:
player_visible = not player_visible
last_blink_time = current_time
val = adc.read_u16()
sw5_pressed, stop_count, should_stop = handle_stop_button(val, sw5_pressed, stop_count)
if should_stop:
pico_led.value(0)
break
new_x, new_y, moved = handle_movement(val, player_x, player_y)
if moved:
player_x, player_y = new_x, new_y
player_visible = True
last_blink_time = utime.ticks_ms()
# Target collision detection
if player_x == target_x and player_y == target_y:
score += 1
if 1 <= score <= 5:
leds[score - 1].value(1)
# Victory condition
if score >= 5:
pico_led.value(0)
display.fill(0)
display.text("5", 0, 0, 1)
display.show()
utime.sleep(3)
break
target_x, target_y = spawn_target(player_x, player_y)
utime.sleep(0.15)
# Render frame
display.fill(0)
display.pixel(target_x, target_y, 1)
if player_visible:
display.pixel(player_x, player_y, 1)
display.show()
utime.sleep(0.01)
Engineering Takeaways & Skills
ReflectionThis project provided valuable hands-on experience in several foundational areas of Technical Computer Science:
- Hardware Bus Communication: Configuring master SPI buses with clock polarity, phase, and chip select lines to control peripheral driver ICs.
- Analog Signal Multiplexing: Designing and decoding voltage divider ladders to multiplex multiple physical user inputs onto a single microcontroller ADC pin.
- Asynchronous Embedded Timing: Managing real-time input polling, debouncing, and visual blinking using non-blocking millisecond tick differentials (
ticks_diff) instead of blocking sleep calls. - Constrained Resource Optimization: Developing clean, loop-efficient game architecture directly on low-power bare-metal microcontroller hardware.