Microcontroller Snake Game

Jeongmin Ko

Overview

The goal of this project was to implement the classic Snake game on a microcontroller using an SPI-based 1.3" TFT LCD display (ST7789). The game is controlled via an analog joystick and visually rendered in full color. The snake grows longer each time it eats food, and the game ends either when the snake collides with itself or the wall. If the snake reaches the maximum possible length (filling the entire screen), a different ending animation is triggered. The project emphasizes embedded system programming, graphical rendering on an LCD screen, timer-based task scheduling, and handling user input from external hardware components. Through this project, we aimed to integrate low-level peripheral control with real-time gameplay logic in a fun and engaging way.

Video

Hardware

The following components were used to compose game's PCB:

- Microcontroller: Silicon Labs RFR32BG22
- Display module: 1.3" SPI TFT LCD, 240x240 resolution, ST7789 driver
- Joystick: TSSJ-30 analog joystick module
Board Assembly Schematic

While our goal of this project is making a snake game, the hardware plays a critical role in bringing the game to life. The Silicon Labs EFR32BG22 microcontroller handles all core computations, including input processing, game logic, and communication with the display module. The 1.3" SPI TFT LCD, driven by the ST7789 controller, receives commands via the SPI interface to visually render the snake, food, and background. The analog joystick sends directional input as voltage levels to the ADC pins on the microcontroller, allowing the user to control the snake in four directions. The USB connection is used to power the board as well as to flash and debug the firmware during development. Together, these components work seamlessly to create a responsive and visually engaging embedded game system.

Software

The snake game software was written in C using Silicon Labs' Simplicity Studio IDE. Two hardware timers are used: one polls the joystick every 10 milliseconds, and the other updates game logic every 300 milliseconds. The joystick input controls the snake’s direction, and all movement, collision detection, and food generation are handled in real time. The ST7789 LCD is updated using pixel-based drawing functions, and the game resets automatically if the snake hits a wall or its own body. The score is updated visually after each successful food collection. New random food position is generated by using on-board True Random Number Generator (TRNG).

Program flow

Initialization
The initialization function sets up the game by selecting a random starting point for the snake, clearing the LCD, placing the first food, and starting 2 periodic timers.
Joystick Input
The joystick is polled every 10ms by using the on-board ADC. Direction changes are applied unless they are directly opposite to the current movement.
Timer-Based Game Update
Every 300ms, the snake’s head advances in the current direction. Game state updates are triggered via a sleep timer callback.
Collision Detection
If a collision with the snake body or a wall is detected, the screen flashes and the game restarts.
Food Handling & Score Update
When food is eaten, the snake grows and a new food is placed. The score is updated on-screen in real time.

Final Thoughts

The final version of the snake game successfully displays real-time gameplay on the 1.3" SPI TFT LCD with a resolution of 240x240 pixels. The snake moves smoothly across the screen, and its direction responds accurately to joystick input. The food appears at random positions that do not overlap with the snake's body, and when the snake consumes the food, its body length increases correctly. Collision detection with both the walls and the snake's own body works as expected, and the game resets automatically with a flashing red screen to indicate failure.

The visual quality of the TFT display significantly enhances the user experience compared to simpler LED matrices. The platform can be used for implementing other games.

Downloads