Build it step by step
0 of 8 doneWire the matrix
VCC to 5V, GND to GND, DIN to D12, CLK to D11, CS to D10.
Wire the button
D2 to GND.
Install LedControl
From the Library Manager.
Upload the sketch
Face one shows at start.
Press the button
The die rolls and slows down.
Read the totals
Open the serial monitor.
Roll fifty times
Compare the six counts.
Fix the orientation
Edit the table if the dots look turned.
How do you build a dice on an LED matrix?
A matrix draws the dots of a die and rolls with a button press, counting each face on the serial monitor. You need Arduino Nano, MAX7219 8x8 LED matrix module, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 75 minutes.
- Level
- Beginner
- Time
- About 75 min
- Parts
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Matrix module VCC | Nano 5V | Power |
| Matrix module GND | Nano GND | Common ground |
| Matrix module DIN | Nano D12 | Data in |
| Matrix module CLK | Nano D11 | Clock |
| Matrix module CS | Nano D10 | Chip select |
| Button | Nano D2 to GND | Roll |
Code
The MAX7219 drives all 64 LEDs of the matrix from three signal wires, and the LedControl library lets you set a whole row with one byte. A die face is just eight bytes: two by two blocks of lit pixels at the corners, the sides and the centre make the dots, so face six is the corners plus the two side pairs. The sketch picks a random face when you press the button, after a short rolling animation that slows down like a real die. Random numbers on a Nano are only pseudo random, so the sketch mixes in analogRead of an unconnected pin at start and the exact time of the press to get a different roll each time. Every roll adds to a counter, and the serial monitor shows how many times each face has come up, so after fifty rolls you can see how close to even the totals are. Modules from different makers can be mounted in a different orientation, so if the dots look turned or mirrored, change the byte order or flip the bits in the table. Keep the intensity low to protect the USB supply and your eyes.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <LedControl.h>
LedControl lc = LedControl(12, 11, 10, 1); // DIN, CLK, CS, one module
const int BTN = 2;
// Six die faces, one byte per row
const byte FACES[6][8] = {
{0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00}, // 1
{0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03}, // 2
{0xC0, 0xC0, 0x00, 0x18, 0x18, 0x00, 0x03, 0x03}, // 3
{0xC3, 0xC3, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xC3}, // 4
{0xC3, 0xC3, 0x00, 0x18, 0x18, 0x00, 0xC3, 0xC3}, // 5
{0xC3, 0xC3, 0x00, 0xC3, 0xC3, 0x00, 0xC3, 0xC3} // 6
};
int counts[6] = {0, 0, 0, 0, 0, 0};
bool wasDown = false;
void draw(int face) {
for (int row = 0; row < 8; row++) lc.setRow(0, row, FACES[face][row]);
}
void setup() {
pinMode(BTN, INPUT_PULLUP);
Serial.begin(9600);
lc.shutdown(0, false); // wake the chip
lc.setIntensity(0, 2); // 0 to 15
lc.clearDisplay(0);
randomSeed(analogRead(A0)); // an unconnected pin gives a different start each time
draw(0);
}
void loop() {
bool down = (digitalRead(BTN) == LOW);
if (down && !wasDown) {
randomSeed(micros()); // button timing adds more randomness
// Roll animation that slows down
for (int i = 0, d = 40; i < 14; i++, d += 12) {
draw(random(6));
delay(d);
}
int face = random(6);
draw(face);
counts[face]++;
Serial.print("Rolled "); Serial.print(face + 1); Serial.print(" totals:");
for (int k = 0; k < 6; k++) { Serial.print(" "); Serial.print(counts[k]); }
Serial.println();
}
wasDown = down;
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power on | One dot in the centre |
| Press the button | Faces flick, then stop |
| Fifty rolls | Each face between about 4 and 14 times |
| Face six | Corners and both side pairs lit |
| Restart | Different first roll sequence |
If it does not work
Matrix blank
Check DIN, CLK and CS pins and wake with shutdown(0,false).
Dots turned or mirrored
Reorder the rows or flip the bits.
Same rolls every time
Check the randomSeed lines.
Button rolls twice
Add a short delay after a press.
Library not found
Install LedControl in the Library Manager.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the dice on an LED matrix?
Arduino Nano, MAX7219 8x8 LED matrix module, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.
How long does the dice on an LED matrix take to build?
About 75 minutes for a beginner maker, in 8 steps.
How do I know the dice on an LED matrix works?
Power on: One dot in the centre. Press the button: Faces flick, then stop. Fifty rolls: Each face between about 4 and 14 times.
Matrix blank: what should I check?
Check DIN, CLK and CS pins and wake with shutdown(0,false).
Dots turned or mirrored: what should I check?
Reorder the rows or flip the bits.
Same rolls every time: what should I check?
Check the randomSeed lines.
Button rolls twice: what should I check?
Add a short delay after a press.
