Built-in project · Arduino Uno R3

Light-up Valentine heart on an Arduino Uno

A heart on an 8x8 MAX7219 LED matrix beats lub-dub, a big heart and a small one, at a pace you set with a knob, ready to glow behind red tissue in a card. The whole build, an Arduino Uno and 2 more parts, 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.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Light-up Valentine heart · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Drawing pictures on an LED matrix with one byte per row
  • Using a MAX7219 chip through the LedControl library
  • Mapping a knob reading to beats per minute with map
  • Timing a pattern as fractions of one beat
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 4

How it works

An 8x8 LED matrix with a MAX7219 chip is wired to pins 11 (DIN), 13 (CLK) and 10 (CS). The LedControl library sends it eight bytes, one per row, and each 1 bit is a lit LED. The sketch has two pictures: a big heart and a small heart.

A heartbeat is two thumps close together, then a rest. So the sketch shows big, small, big, then rests on the small heart. The knob on A0 sets the pace from 50 to 140 beats a minute.

The delay for each step is a fraction of the beat length, and the rest takes whatever time is left. Every tenth beat, the sketch prints a line to the serial monitor.

Parts list

4 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
LED matrix, 8x8 pin CLKArduino Uno R3 pin 13
LED matrix, 8x8 pin DINArduino Uno R3 pin 11
LED matrix, 8x8 pin CSArduino Uno R3 pin 10
Potentiometer pin 3Arduino Uno R3 pin 5V
LED matrix, 8x8 pin VCCArduino Uno R3 pin 5V
Potentiometer pin 1Arduino Uno R3 pin GND
LED matrix, 8x8 pin GNDArduino Uno R3 pin GND
Potentiometer pin 2Arduino Uno R3 pin A0

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Light-up Valentine heart: a heart on an 8x8 LED matrix that beats, a big
// beat and a small one, lub-dub, at a pace you set with a knob.
//
//   MAX7219   8x8 matrix module: DIN -> pin 11, CLK -> pin 13, CS -> pin 10,
//             VCC -> 5 V, GND -> GND
//   knob      potentiometer wiper -> A0, ends to 5 V and GND
//
// Each picture is eight bytes, one per row, a 1 for every lit LED, so the big
// heart and the small heart are two short tables. A heartbeat is not one
// thump: it is two close together and then a rest, so the sketch shows the
// big heart, the small one, the big one again, then rests on the small one.
//
// The knob sets the beats a minute, from a calm 50 to an excited 140. Put the
// matrix behind a sheet of red tissue paper in a card or a little frame and
// it glows like one heart instead of 64 dots.
//
// Things to try. Turn the knob. Draw your own picture in BIG or SMALL: a
// smiling face, or your initials.

#include <LedControl.h>

LedControl matrix(11, 13, 10, 1);   // DIN, CLK, CS, one module

const uint8_t BIG[8] = {
  0b01100110,
  0b11111111,
  0b11111111,
  0b11111111,
  0b01111110,
  0b00111100,
  0b00011000,
  0b00000000,
};
const uint8_t SMALL[8] = {
  0b00000000,
  0b00100100,
  0b01111110,
  0b01111110,
  0b00111100,
  0b00011000,
  0b00000000,
  0b00000000,
};

int beats = 0;

void draw(const uint8_t picture[8]) {
  for (int row = 0; row < 8; row++) matrix.setRow(0, row, picture[row]);
}

void setup() {
  Serial.begin(9600);
  matrix.shutdown(0, false);
  matrix.setIntensity(0, 8);
  matrix.clearDisplay(0);
  draw(SMALL);
  Serial.println("Be my Valentine: the heart is beating");
}

void loop() {
  int bpm = map(analogRead(A0), 0, 1023, 50, 140);
  unsigned long beat = 60000UL / bpm;

  // Lub, dub, rest.
  draw(BIG);
  delay(beat / 8);
  draw(SMALL);
  delay(beat / 10);
  draw(BIG);
  delay(beat / 8);
  draw(SMALL);
  beats++;
  if (beats % 10 == 1) {
    Serial.print("Beat ");
    Serial.print(beats);
    Serial.print(" at ");
    Serial.print(bpm);
    Serial.println(" beats a minute");
  }
  delay(beat - beat / 8 - beat / 10 - beat / 8);
}

Code walkthrough

The key functions of the sketch, and what each one does.

draw sketch.ino

Takes a picture, a table of eight bytes, and sends each row to the matrix with matrix.setRow. Every picture in the sketch goes through this one function.

void draw(const uint8_t picture[8]) {
  for (int row = 0; row < 8; row++) matrix.setRow(0, row, picture[row]);
}

setup sketch.ino

Wakes the matrix with shutdown(0, false), sets a medium brightness of 8, clears it and draws the small heart. It also prints a greeting to the serial monitor.

void setup() {
  Serial.begin(9600);
  matrix.shutdown(0, false);
  matrix.setIntensity(0, 8);
  matrix.clearDisplay(0);
  draw(SMALL);
  Serial.println("Be my Valentine: the heart is beating");
}

loop sketch.ino

Maps the knob to bpm from 50 to 140 and works out beat in milliseconds. Then it draws big, small, big, small, with short delays between, and waits for the rest of the beat. Every tenth beat it prints a line.

void loop() {
  int bpm = map(analogRead(A0), 0, 1023, 50, 140);
  unsigned long beat = 60000UL / bpm;

  // Lub, dub, rest.
  draw(BIG);
  delay(beat / 8);
  draw(SMALL);
  delay(beat / 10);
  draw(BIG);
  delay(beat / 8);
  draw(SMALL);
  beats++;
  if (beats % 10 == 1) {
    Serial.print("Beat ");
    Serial.print(beats);
    Serial.print(" at ");
    Serial.print(bpm);
    Serial.println(" beats a minute");
  }
  delay(beat - beat / 8 - beat / 10 - beat / 8);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The matrix stays dark. This project
    Check DIN on pin 11, CLK on pin 13, CS on pin 10, and 5V and GND. The code must wake the chip, and it does that in setup.
  • The heart looks scrambled. This project
    Make sure the pins are not swapped, especially DIN and CLK.
  • The pace does not change. This project
    The knob's middle pin goes to A0 and its ends to 5V and GND.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • The matrix is blank after upload. LED matrix
    On power up the MAX7219 is in shutdown mode with intensity 0. Wake it, set intensity, set scan limit to all 8 digits, and clear the display.
  • Dim, flickering, or wrong rows. LED matrix
    Set the ISET resistor right for your LEDs (about 10 kohm for 40 mA). Power from 5 V with a 0.1 uF capacitor, and chain DOUT to the next DIN.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.