8×8 LED matrix animation (MAX7219)
Draw a smiley and a beating heart on an 8×8 LED matrix driven by a MAX7219 over three wires.
Displays · Beginner
In a real product: Scrolling sign (Consumer electronics): An LED matrix shows messages and animations.
Sixty-four LEDs would need sixty-four pins driven directly. The MAX7219 driver chip scans the matrix itself and takes commands over a three-wire serial bus, so the board only says what to show.
What you'll learn
- Storing an image as eight bytes, one bit per LED
- Sending commands to a serial-bus driver with the LedControl library
- Brightness control and why the chip starts switched off
How it works
Each row of the picture is one byte: bit 7 is the left LED, bit 0 the right. 0x3C is 00111100, the top of the smiley's face. lc.setRow(0, row, bits) sends that byte to the MAX7219's register for that row.
Under the hood every command is 16 bits: a register number and a value, shifted in on DIN with a pulse on CLK for each bit, then applied when CS rises. Register 0x0C is shutdown (the chip starts asleep, so lc.shutdown(0, false) wakes it), 0x0A is the brightness, and 0x01 to 0x08 hold the rows.
Modules chain: each one passes the data it shifts on from DOUT to the next module's DIN, so one set of pins drives a long scrolling display. Address 0 is the module nearest the board.
Try this
- Draw your own 8×8 picture (graph paper helps) and add it to the loop.
- Set the matrix's chain to 2 in its settings, create
LedControl lc(12, 11, 10, 2)and draw on module 1 as well. - Light a single LED with
lc.setLed(0, row, col, true)and move it with a joystick.
sketch.ino
// A smiley, then a heart that beats by changing the brightness, on a MAX7219 8x8 matrix.
#include <LedControl.h>
LedControl lc(12, 11, 10, 1); // DIN, CLK, CS, number of modules
const byte SMILE[8] = { 0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C };
const byte HEART[8] = { 0x00, 0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18 };
void draw(const byte *image) {
for (int row = 0; row < 8; row++) lc.setRow(0, row, image[row]);
}
void setup() {
lc.shutdown(0, false); // it starts in shutdown mode
lc.setIntensity(0, 6); // 0 to 15
lc.clearDisplay(0);
}
void loop() {
lc.setIntensity(0, 6);
draw(SMILE);
delay(1500);
draw(HEART);
for (int beat = 0; beat < 2; beat++) {
for (int level = 2; level <= 15; level++) { lc.setIntensity(0, level); delay(25); }
for (int level = 15; level >= 2; level--) { lc.setIntensity(0, level); delay(25); }
}
}