Scoreboard on an Arduino Uno
A MAX7219 eight-digit display driven with LedControl: home score, a ten-minute game clock and the guest score, with a button for each team and one for the clock. The whole build, an Arduino Uno and 4 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.
Intermediate About 20 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.
What you'll learn
- Driving a MAX7219 display with the LedControl library
- Splitting a display into score and clock regions
- Telling a short press from a one second hold
- Counting seconds with millis() without blocking
- Redrawing only when something changed
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
The eight-digit MAX7219 display is split into three parts: the home score on the left, a game clock in the middle, and the guest score on the right. The clock starts at 10:00 and counts down.
Three buttons do the work. Pin 2 adds a home point, pin 3 adds a guest point, and pin 4 starts and stops the clock. Hold a score button for one second to take a point back. When the clock hits zero, the whole display flashes.
Parts list
6 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Eight-digit display, MAX7219In Christmas Lights Kit.
- 3 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
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 pin | Connects to |
|---|---|
| Eight-digit display, MAX7219 pin GND | Arduino Uno R3 pin GND |
| Eight-digit display, MAX7219 pin DIN | Arduino Uno R3 pin 12 |
| Eight-digit display, MAX7219 pin CLK | Arduino Uno R3 pin 11 |
| Eight-digit display, MAX7219 pin CS | Arduino Uno R3 pin 10 |
| Pushbutton (3) pin 1a | Arduino Uno R3 pin 4 |
| Pushbutton (2) pin 1a | Arduino Uno R3 pin 3 |
| Pushbutton (1) pin 1a | Arduino Uno R3 pin 2 |
| Eight-digit display, MAX7219 pin VCC | Arduino Uno R3 pin 5V |
| Pushbutton (1) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (2) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (3) pin 2a | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Scoreboard: a MAX7219 eight-digit display as a game scoreboard, the home
// score on the left, a ten-minute game clock in the middle and the guest
// score on the right:
//
// 1 2 1 0.0 0 0 7
// home clock guest
//
// MAX7219 module DIN -> 12 CLK -> 11 CS -> 10 VCC -> 5 V GND -> GND
// buttons 2: home point 3: guest point 4: start and stop the clock
// (each button to GND; the pins use their pull-ups)
//
// LedControl talks to the module: shutdown(0, false) wakes it, setDigit()
// writes one digit, and digit 0 is the rightmost. The point between minutes
// and seconds is the decimal point of the minutes digit. When the clock runs
// out the whole display flashes, by switching the chip's shutdown on and off.
//
// Hold the home or guest button for a second to take that team's point back.
#include "LedControl.h"
LedControl lc = LedControl(12, 11, 10, 1); // DIN, CLK, CS, one module
const int HOME_BUTTON = 2;
const int GUEST_BUTTON = 3;
const int CLOCK_BUTTON = 4;
const uint16_t GAME_SECONDS = 10 * 60;
uint8_t home = 0;
uint8_t guest = 0;
uint16_t secondsLeft = GAME_SECONDS;
bool running = false;
unsigned long lastTick = 0;
uint32_t shown = 0xffffffff;
struct Button {
int pin;
bool down;
unsigned long since;
bool held;
};
Button buttons[3] = {
{HOME_BUTTON, false, 0, false},
{GUEST_BUTTON, false, 0, false},
{CLOCK_BUTTON, false, 0, false},
};
// Two digits of a score at positions `right` and `right + 1`, no leading zero.
void showScore(uint8_t score, int right) {
lc.setDigit(0, right, score % 10, false);
if (score >= 10) {
lc.setDigit(0, right + 1, score / 10, false);
} else {
lc.setChar(0, right + 1, ' ', false);
}
}
void showClock() {
uint8_t minutes = secondsLeft / 60;
uint8_t seconds = secondsLeft % 60;
lc.setDigit(0, 5, minutes / 10, false);
lc.setDigit(0, 4, minutes % 10, true);
lc.setDigit(0, 3, seconds / 10, false);
lc.setDigit(0, 2, seconds % 10, false);
}
void show() {
showScore(home, 6);
showClock();
showScore(guest, 0);
}
// What a button did since last time: 1 for a press, 2 for a hold of a second.
uint8_t check(Button &b) {
bool down = digitalRead(b.pin) == LOW;
unsigned long now = millis();
uint8_t event = 0;
if (down && !b.down) {
b.since = now;
b.held = false;
} else if (down && !b.held && now - b.since >= 1000) {
b.held = true;
event = 2;
} else if (!down && b.down && !b.held && now - b.since >= 20) {
event = 1;
}
b.down = down;
return event;
}
void setup() {
for (int i = 0; i < 3; i++) pinMode(buttons[i].pin, INPUT_PULLUP);
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
show();
}
void loop() {
uint8_t e = check(buttons[0]);
if (e == 1 && home < 99) home++;
if (e == 2 && home > 0) home--;
e = check(buttons[1]);
if (e == 1 && guest < 99) guest++;
if (e == 2 && guest > 0) guest--;
if (check(buttons[2]) == 1) {
if (secondsLeft == 0) {
secondsLeft = GAME_SECONDS; // a new period
lc.shutdown(0, false);
} else {
running = !running;
}
lastTick = millis();
}
unsigned long now = millis();
if (running && now - lastTick >= 1000) {
lastTick += 1000;
if (secondsLeft > 0) secondsLeft--;
if (secondsLeft == 0) running = false;
}
// Only what changed goes out: eight digits are 128 clocks each time.
uint32_t state = ((uint32_t)home << 24) | ((uint32_t)guest << 16) | secondsLeft;
if (state != shown) {
shown = state;
show();
}
if (secondsLeft == 0) {
// Time: flash the whole board, twice a second.
lc.shutdown(0, (now / 250) % 2 == 0);
}
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
showScore sketch.ino
Writes a score of up to two digits at a pair of display positions. The ones digit goes in the first spot. The tens digit goes next to it, or a blank if the score is under 10, so there is no leading zero.
// Two digits of a score at positions `right` and `right + 1`, no leading zero.
void showScore(uint8_t score, int right) {
lc.setDigit(0, right, score % 10, false);
if (score >= 10) {
lc.setDigit(0, right + 1, score / 10, false);
} else {
lc.setChar(0, right + 1, ' ', false);
}
}check sketch.ino
Watches one button and reports an event: 1 for a short press, 2 for a hold of a second. A press is reported on release, and only if it lasted at least 20 ms. This is how one button does two jobs.
// What a button did since last time: 1 for a press, 2 for a hold of a second.
uint8_t check(Button &b) {
bool down = digitalRead(b.pin) == LOW;
unsigned long now = millis();
uint8_t event = 0;
if (down && !b.down) {
b.since = now;
b.held = false;
} else if (down && !b.held && now - b.since >= 1000) {
b.held = true;
event = 2;
} else if (!down && b.down && !b.held && now - b.since >= 20) {
event = 1;
}
b.down = down;
return event;
}loop sketch.ino
Applies button events to the scores and clock, ticks the clock down once a second while it runs, and redraws only when the packed state changes. At zero it flashes the display by switching the chip's shutdown on and off.
void loop() {
uint8_t e = check(buttons[0]);
if (e == 1 && home < 99) home++;
if (e == 2 && home > 0) home--;
e = check(buttons[1]);
if (e == 1 && guest < 99) guest++;
if (e == 2 && guest > 0) guest--;
if (check(buttons[2]) == 1) {
if (secondsLeft == 0) {
secondsLeft = GAME_SECONDS; // a new period
lc.shutdown(0, false);
} else {
running = !running;
}
lastTick = millis();
}
unsigned long now = millis();
if (running && now - lastTick >= 1000) {
lastTick += 1000;
if (secondsLeft > 0) secondsLeft--;
if (secondsLeft == 0) running = false;
}
// Only what changed goes out: eight digits are 128 clocks each time.
uint32_t state = ((uint32_t)home << 24) | ((uint32_t)guest << 16) | secondsLeft;
if (state != shown) {
shown = state;
// ... 8 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The display stays blank This project
Check DIN on pin 12, CLK on pin 11 and CS on pin 10, and that the module has 5 V and ground. - A button does nothing This project
Wire each button between its pin (2, 3 or 4) and GND. The pins use their pull-ups, so a press reads LOW. - The display is blank or shows garbage. The chip starts in shutdown, with scan limit and intensity not set. Eight-digit display
In LedControl call shutdown(0, false), setIntensity(0, 8) and clearDisplay(0) at start. Also set the scan limit to 8 digits. Check that DIN, CLK and CS match the pins in the constructor. - The display dims, flickers, or resets. The segment current comes from one resistor, RSET, and the chip needs a clean 5 V. Eight-digit display
Power it from 5 V with a 10 uF cap and a 0.1 uF cap near the chip. Keep ISET resistor values as the datasheet says, about 10k for a standard LED display. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - 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. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.