STANNUM
Open the simulator

Tutorials / Output

8×8 LED matrix (MAX7219)

Sixty-four LEDs driven through three wires by a MAX7219 — pixel art in eight bytes.

Parts in the example:
  • 8×8 matrix
The 8×8 matrix block as it appears on the canvas.

What it is

An 8×8 matrix on its own needs 16 pins scanned very fast. The MAX7219 does that scanning by itself: you send it eight bytes — one per row, one bit per LED — over a three-wire serial link, and it keeps the picture lit. Modules chain: the data out of one feeds the next.

In the simulator

The block shows the 64 LEDs in the color chosen in Settings, with brightness following the chip's intensity register. The example draws two frames of a heart and alternates them.

Pins

PinWhat it is
DINdata in — to a GPIO
CLKclock — to a GPIO
CSchip select (LOAD) — to a GPIO
VCCpower — to a 3V3 symbol
GNDground — to a GND symbol

Wiring

The circuit below is the example Beating heart on the 8×8 matrix from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
8×8 matrix pinGoes to
DIND23 on the board (GPIO 23)
CLKD18 on the board (GPIO 18)
CSD5 on the board (GPIO 5)
VCCa 3V3 symbol
GNDa GND symbol

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the 8×8 matrix.
SettingAcceptsDefaultNotes
LED colorRed · Green · Blue · Yellow · Orange · WhiteRedThe off-the-shelf module is red; green and blue exist made to order.

Code

sketch.cpp
#include <Arduino.h>
#include <LedControl.h>

// Each row of the drawing is a byte: bit 7 is the leftmost column.
// Draw your own on graph paper and translate it into binary.
const byte CHEIO[8] = {
  B01100110, B11111111, B11111111, B11111111,
  B01111110, B00111100, B00011000, B00000000};
const byte VAZIO[8] = {
  B00000000, B01100110, B01111110, B01111110,
  B00111100, B00011000, B00000000, B00000000};

LedControl matriz(23, 18, 5, 1);   // DIN, CLK, CS, 1 module

void desenhar(const byte quadro[8]) {
  for (int linha = 0; linha < 8; linha++)
    matriz.setRow(0, linha, quadro[linha]);
}

void setup() {
  matriz.shutdown(0, false);       // the MAX7219 BOOTS UP asleep!
  matriz.setIntensity(0, 8);
  matriz.clearDisplay(0);
}

void loop() {
  desenhar(CHEIO);                 // systole
  delay(450);
  desenhar(VAZIO);                 // diastole
  delay(300);
}

The LedControl library takes the three pins and the number of modules. Two things every MAX7219 sketch needs: shutdown(0, false), because the chip boots asleep, and setIntensity(). Then setRow(0, row, byte) paints one row — bit 7 is the leftmost LED. Draw on graph paper, translate each row into binary.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

The heart beats: full for 450 ms, small for 300 ms. Comment out the shutdown() line and rebuild — nothing lights, exactly like the module out of the box.

How the simulation models it

Try this

  1. Scroll a letter across the matrix by shifting each row byte one bit per frame.
  2. Add a button that switches between three animations.

See also