Build it step by step
0 of 6 doneWire the matrix
DIN to D12, CLK to D11, CS to D10, VCC to 5V, GND to GND.
Wire the joystick
VRx to A0, VRy to A1, VCC to 5V, GND to GND.
Add sound
Passive buzzer to D3 and GND.
Upload
Install LedControl and upload. A three-LED snake should start moving right.
Play
Push the stick to turn. Eat the food dot to grow and speed up.
End of game
When you hit a wall or yourself, the matrix flashes and shows your score as lit LEDs.
How do you build a snake on an LED matrix?
Play Snake on an 8 × 8 LED matrix with a joystick. The snake grows when it eats and speeds up, and the game shows your score at the end. You need Arduino Nano, MAX7219 8x8 LED matrix module, Joystick module (dual axis), Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 6 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 6
Parts list
- Development BoardsBuyArduino Nano × 1
- DisplaysBuy
- SensorsBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| MAX7219 DIN / CLK / CS | Nano D12 / D11 / D10 | SPI-style data |
| MAX7219 VCC / GND | Nano 5V / GND | Matrix power |
| Joystick VRx / VRy | Nano A0 / A1 | Direction |
| Joystick VCC / GND | Nano 5V / GND | Power |
| Passive buzzer + / − | Nano D3 / GND | Sounds |
Code
Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "LedControl" by Eberhard Fahle. If the picture is mirrored, swap sx and sy in setLed, or rotate the module.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <LedControl.h> // library "LedControl" by Eberhard Fahle
LedControl lc(12, 11, 10, 1); // DIN, CLK, CS, one device
const byte BUZ = 3;
byte sx[64], sy[64], len;
int dx, dy, fx, fy;
unsigned int stepMs;
unsigned long last;
void placeFood() {
bool ok;
do {
fx = random(8); fy = random(8); ok = true;
for (byte i = 0; i < len; i++) if (sx[i] == fx && sy[i] == fy) ok = false;
} while (!ok);
}
void newGame() {
len = 3; sx[0] = 4; sy[0] = 4; sx[1] = 3; sy[1] = 4; sx[2] = 2; sy[2] = 4;
dx = 1; dy = 0; stepMs = 400; placeFood();
}
void setup() {
lc.shutdown(0, false); lc.setIntensity(0, 4); lc.clearDisplay(0);
randomSeed(analogRead(A5));
newGame();
}
void gameOver() {
tone(BUZ, 150, 600);
for (byte k = 0; k < 3; k++) { lc.clearDisplay(0); delay(200); for (byte r = 0; r < 8; r++) lc.setRow(0, r, 255); delay(200); }
lc.clearDisplay(0);
byte score = len - 3; // show the score as that many lit LEDs
for (byte i = 0; i < score && i < 64; i++) lc.setLed(0, i / 8, i % 8, true);
delay(3000);
newGame();
}
void loop() {
int jx = analogRead(A0), jy = analogRead(A1); // turn, but never straight back on yourself
if (jx < 300 && dx == 0) { dx = -1; dy = 0; }
else if (jx > 700 && dx == 0) { dx = 1; dy = 0; }
else if (jy < 300 && dy == 0) { dy = -1; dx = 0; }
else if (jy > 700 && dy == 0) { dy = 1; dx = 0; }
if (millis() - last < stepMs) return;
last = millis();
int nx = sx[0] + dx, ny = sy[0] + dy;
if (nx < 0 || nx > 7 || ny < 0 || ny > 7) { gameOver(); return; } // wall
for (byte i = 0; i < len - 1; i++) if (sx[i] == nx && sy[i] == ny) { gameOver(); return; } // itself
bool eat = (nx == fx && ny == fy);
if (eat && len < 64) len++;
for (byte i = len - 1; i > 0; i--) { sx[i] = sx[i - 1]; sy[i] = sy[i - 1]; }
sx[0] = nx; sy[0] = ny;
if (eat) { tone(BUZ, 1000, 60); placeFood(); if (stepMs > 120) stepMs -= 15; }
lc.clearDisplay(0);
for (byte i = 0; i < len; i++) lc.setLed(0, sy[i], sx[i], true);
lc.setLed(0, fy, fx, true);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Stick left or right, up or down | The snake turns without reversing into itself |
| Reach the food | Snake grows by one, beep, and a new food dot appears |
| Hit the wall | Three flashes, then the score, then a new game |
| Score of 10 | Ten LEDs light for three seconds |
If it does not work
The matrix stays dark
Check power and DIN, CLK and CS pins and that LedControl is installed.
The snake turns the wrong way
Swap the A0 and A1 signs, or rotate the joystick.
The picture is mirrored or rotated
Many 8 × 8 modules differ. Swap rows and columns in setLed.
Random food always lands in the same place
randomSeed needs a floating pin like A5 left unconnected.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the snake on an LED matrix?
Arduino Nano, MAX7219 8x8 LED matrix module, Joystick module (dual axis), Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the snake on an LED matrix take to build?
About 90 minutes for a beginner maker, in 6 steps.
How do I know the snake on an LED matrix works?
Stick left or right, up or down: The snake turns without reversing into itself. Reach the food: Snake grows by one, beep, and a new food dot appears. Hit the wall: Three flashes, then the score, then a new game.
The matrix stays dark: what should I check?
Check power and DIN, CLK and CS pins and that LedControl is installed.
The snake turns the wrong way: what should I check?
Swap the A0 and A1 signs, or rotate the joystick.
The picture is mirrored or rotated: what should I check?
Many 8 × 8 modules differ. Swap rows and columns in setLed.
Random food always lands in the same place: what should I check?
randomSeed needs a floating pin like A5 left unconnected.
