AI Snake on a Chip: Embedded Reinforcement Learning on ATtiny1616
by mars91 in Circuits > Arduino
2 Views, 0 Favorites, 0 Comments
AI Snake on a Chip: Embedded Reinforcement Learning on ATtiny1616


The idea is simple:
- Download a open-source python Snake AI model
- Customize and run the python (tune the matrices)
- Wait for the model to train
- Copy the matrices and basic linear algebra functions to a chip
- Watch the chip run the trained AI model!
- Bonus: Build a human vs. AI game
Most neural networks (machine learning, AI, deep networks, whatever you call them) boil down to an input, trained matrices (the black box), and an output. Training matrices to "think" is where the magic happens.
So take someone else's magic code, train a small AI model, and put it on a chip. A chip that costs a dollar with only ~2 KB of stack/heap memory, and ~16 KB of Flash memory! That's C++ jargon for your arrays can easily overflow the chip’s memory.
Supplies


I'm using one of my prebuilt business cards because it includes an 8x8 LED matrix for the 8x8 snake game. The instructables link is below as a "supply". Also might sounds pretty impressive to say, “Here’s my business card, it has an embedded, pretrained reinforcement learning model that plays Snake.” Or... you might weird out your future employer :)
I'll also include all the parts in case you simply wanted to build it on a breadboard.
The open source Snake AI with Reinforcement Learning: link here
Parts you'll need:
- ATtiny1616
- UPDI Programmer
- BiColor LED Matrix
- 4 Buttons
- 4 × 10kΩ Resistors
- Some wires
If you want to build your own LED matrix business card:
Train the AI



- Google Snake AI Reinforcement Learning
- Go to the first GitHub repo, link here
- Download the ZIP.
- Open the Python repository.
Step 1: Modify agent.py for a smaller network
The neural network, one line 11, is defined like:
This means a 11 size input, the snake's position and the food location. 256 is the size of the neural net or "the brain". Output size of 3 holds the directions (e.g., left, straight, right)
I think a 256-neuron network is too large for our ATtiny1616's memory. So, I made the neural network smaller, from 256 to 64:
I changed a few training parameters:
Learning rate (line 11):
Initial randomness (line 88):
I figured a lower learning rate and more randomness might help compensate for the smaller neural net. Who knows! I liked the simplicity of this project and didn’t spend any time fine-tuning. Experiment with your own models or try different neural network architectures.
Step 2: Modify game.py for an 8x8 grid
We need to train the AI on an 8x8 grid to match our LED hardware. In this repo, each cell is 20 pixels. To get an 8x8 grid, change line 31 in game.py from:
Step 3: Train Your Model
Run your model in agent.py. I trained it for about 2000 iterations.
You now have trained AI matrices ready to deploy on-chip!
I also wrote a simple Python script that prints your trained matrices as C++ arrays (for Flash memory).
Step 4: Run my code cArrays.py
This prints out the c arrays we can paste in our Arduino or C++ code.
C++ Code

Code is essentially converting the Python Snake game to C++, and implementing a few simple linear algebra functions.
The function void get_action(bool state[11], uint8_t action[3]) moves the snake according to the trained matrices.
The only functions used to integrate the trained matrices (for the AI magic) are: a simple matrix-vector multiplication of the form Ax + b, followed by a ReLU activation function.
The ReLU function is straightforward, set any negative value to zero.
Below is some pseudocode for the Ax + b computation and the ReLU function:
The trained matrices are saved to flash memory using the PROGMEM keyword and accessed using the function pgm_read_float.
Game play:
Gameplay switches between human and AI. Be ready when it's your turn, the snake will start moving and If you're not ready, it will crash into a wall. Letting robots win.
The score is displayed by lighting up the same number of pixels as the snake's length. This is an attempt to save memory by not loading in a pixel-to-bitmap image font.
Downloads
Hardware



I'll assume you have an 8x8 LED matrix driven by an HT16K33A. Note Adafruit's BiColor LED Matrix has everything setup. You need four buttons pulled high. Meaning one side of each button is connected to ground, and the other side is connected to a GPIO pin and pulled up to Vin thru a 10k resistor.
Connections are simple. If you're using the business card version, just leave the expose the male pins for programming. If you have separate parts, the LED matrix's SDA and SCL pins connect to the ATtiny 1616 SDA SCL pins. The buttons connect to pins 0 through 3.
Simple diagram is in the pictures (note: it shows only one button and the HT16K33A driver should already be connected to the LEDs, leaving just the Vin, GND, SDA, and SCL pins to wire)
Programming an ATtiny1616
These are the best directions, but I'll summarize:
- In Arduino, add the following URL to your Boards Manager Preferences:
- http://drazzy.com/package_drazzy.com_index.json
- Install megaTinyCore from the Boards Manager.
- Under Tools, select:
- Board: megaTinyCore → ATtiny1616
- Chip: ATtiny1616
- Clock: 10 MHz
- Programmer: SerialUPDI – SLOW (57600 baud)
- Go to Sketch > Upload Using Programmer and upload a blank sketch to confirm everything works.
You should see your UPDI friend blinking and the code uploading successfully to the board.
Next upload the snake_ai_businessCard.ino code by Upload Using Programmer and thats it. You're ready to play against your own AI.
Finale

You've now just put an AI-reinforced model onto a chip that costs less than a dollar. Play against the AI, use it to impress an employer, or think of other AI-driven applications you could implement on a microcontroller.
My recommendation would be to start thinking about something like MNIST. Take in some data from your environment and have your chip use pre-trained AI models to respond accordingly.
For larger models, you may need a more powerful (but still reasonably priced) chip, such as the ESP32, SAMD21, STM32, or Raspberry Pi Pico.