Matrix ball on an Arduino Uno
An Arduino Uno bounces a dot around an 8x8 LED matrix on a MAX7219, three wires and eight registers. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
IntermediateRuns in your browser. Free, and no account needed.
Step by step, with the wiring and the common mistakes: Arduino MAX7219 8x8 LED Matrix: Wiring and Code
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Matrix ball: a dot bouncing round an 8x8 LED matrix on a MAX7219.
//
// Wiring, module to board:
// DIN -> pin 11
// CS -> pin 10 (marked LOAD on some boards)
// CLK -> pin 13
// VCC -> 5 V, GND -> GND
//
// The MAX7219 holds eight digit registers and scans them out itself. On a
// matrix board those eight registers are the eight rows and the eight segment
// outputs are the columns, so writing a row register *is* drawing a row. The
// driver in mokxi_max7219.h keeps the eight bytes in RAM, lets the sketch poke
// pixels into them, and pushes all eight out on show().
//
// Every register comes up zero at power-on (shut down, one row scanned,
// minimum brightness), which is why nothing happens on a real board until
// begin() writes Shutdown, Scan Limit and Intensity.
//
// The ball is 2x2 and moves one pixel a frame. When it hits a wall it bounces
// and that wall lights for a frame, so the collision is something you can see
// rather than something you have to believe.
#include "mokxi_max7219.h"
const uint8_t DIN_PIN = 11;
const uint8_t CS_PIN = 10;
const uint8_t CLK_PIN = 13;
Max7219 matrix(DIN_PIN, CS_PIN, CLK_PIN, 1);
// Top left of the 2x2 ball, so it runs 0..6 on both axes.
const int8_t BALL = 2;
const int8_t LIMIT = 8 - BALL;
int8_t x = 0;
int8_t y = 0;
int8_t dx = 1;
int8_t dy = 1;
// Which wall was hit last frame: one bit each, top right bottom left.
uint8_t flash = 0;
const int FRAME_MS = 90;
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Uno: a ball round an 8x8 matrix");
matrix.begin(8);
}
void loop() {
x = (int8_t)(x + dx);
y = (int8_t)(y + dy);
flash = 0;
if (x <= 0) {
x = 0;
dx = 1;
flash |= 0x08; // left
} else if (x >= LIMIT) {
x = LIMIT;
dx = -1;
flash |= 0x02; // right
}
if (y <= 0) {
y = 0;
dy = 1;
flash |= 0x01; // top
} else if (y >= LIMIT) {
y = LIMIT;
dy = -1;
flash |= 0x04; // bottom
}
matrix.clear();
if (flash & 0x01) matrix.fillRect(0, 0, 8, 1, true);
if (flash & 0x02) matrix.fillRect(7, 0, 1, 8, true);
if (flash & 0x04) matrix.fillRect(0, 7, 8, 1, true);
if (flash & 0x08) matrix.fillRect(0, 0, 1, 8, true);
matrix.fillRect(x, y, BALL, BALL, true);
matrix.show();
// A bounce is also a flicker of brightness, which costs one packet.
matrix.setIntensity(flash ? 15 : 8);
delay(FRAME_MS);
}
Parts list
3 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; LED matrix, 8x8 pin GND
- Arduino Uno R3 pin 13; LED matrix, 8x8 pin CLK
- Arduino Uno R3 pin 11; LED matrix, 8x8 pin DIN
- Arduino Uno R3 pin 10; LED matrix, 8x8 pin CS
- Arduino Uno R3 pin 5V; LED matrix, 8x8 pin VCC
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.