Electronic Dice
Press the button and seven LEDs light up in a real dice pattern from one to six.
What You'll Build
Never lose a dice again. Seven LEDs are arranged in the classic dice layout, and pressing the button rolls a random number from one to six. The right LEDs light instantly, and a short flicker before the result makes it feel like a real tumbling roll.
What You Need
- ✓Arduino Uno
- ✓7 × LED
- ✓7 × 220Ω resistor
- ✓Push button
- ✓Breadboard
- ✓Jumper wires
How It Works
Each dice face uses a specific combination of the seven positions, so the code stores those combinations and simply lights the correct set. The random number comes from Arduino's random() function, seeded with the noise on an unconnected analog pin so you do not get the same sequence every time you power up.
Wiring
- 1Seven LEDs on pins 2 to 8, each with a 220Ω resistor to GND.
- 2Button straight between pin 9 and GND — INPUT_PULLUP in the code replaces the extra resistor.
Step-by-Step Instructions
- 1
Lay out the dice face
Arrange the LEDs as they appear on a real dice: four corners, two middles, one centre. Sketch it on paper first and number each position.
- 2
Map positions to pins
Write down which pin drives which position. Getting this list right is what makes the faces look correct.
- 3
Wire the button
Use INPUT_PULLUP so the pin sits HIGH normally and drops to LOW when pressed — one less resistor to worry about.
- 4
Add randomness
Call randomSeed(analogRead(A0)) in setup. The floating pin picks up electrical noise, giving a different starting point each time.
- 5
Add the roll animation
Show a few fast random faces before the final one. It only takes half a second but makes the dice feel alive.
Arduino Code
const int LEDS[7] = {2, 3, 4, 5, 6, 7, 8};
const int BUTTON = 9;
// which LEDs light for each face 1..6
const byte FACES[7][7] = {
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,1},
{1,0,0,0,0,1,0},
{1,0,0,0,0,1,1},
{1,1,0,0,1,1,0},
{1,1,0,0,1,1,1},
{1,1,1,1,1,1,0}
};
void showFace(int n) {
for (int i = 0; i < 7; i++) digitalWrite(LEDS[i], FACES[n][i]);
}
void setup() {
for (int i = 0; i < 7; i++) pinMode(LEDS[i], OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
randomSeed(analogRead(A0));
showFace(6);
}
void loop() {
if (digitalRead(BUTTON) == LOW) {
for (int i = 0; i < 8; i++) {
showFace(random(1, 7));
delay(70);
}
showFace(random(1, 7));
delay(300);
}
}Understanding the Code
const byte FACES[7][7]A table with a row per dice face. 1 means that LED is on. Data like this keeps the code short and readable.
INPUT_PULLUPUses a resistor built into the Arduino, so the pin reads HIGH until the button connects it to ground.
randomSeed(analogRead(A0))Uses random electrical noise to start the random sequence somewhere different every time.
random(1, 7)Gives a whole number from 1 up to but not including 7 — exactly a dice roll.
Challenges & Upgrades
Troubleshooting
😖 Same numbers every time you power up
Fix: You forgot randomSeed, or A0 is connected to something. Leave it floating.
😖 One press rolls many times
Fix: Add a small delay after the roll, or wait for the button to be released first.
Up next
🚨 Temperature Alarm