What does this project do?
We build an electronic dice that shows a random number from 1 to 6 when you press a button, just like a real dice. Seven LEDs are arranged like the dots on the face of a real dice. When you press the button, the dice first “rolls” quickly, slows down and finally stops on a number. You can use it instead of a real dice in board games.
- Age group: 10 and up
- Level: Intermediate
- Time: About 45 minutes
- What you will learn: arrays, the
forloop, random numbers withrandom(), showing patterns with code
Parts list
| Part | Qty |
|---|---|
| Arduino Uno (or compatible) | 1 |
| LED (all the same colour) | 7 |
| 220 Ω resistor | 7 |
| Push button | 1 |
| Breadboard | 1 |
| Male-to-male jumper wires | ~16 |
| USB cable | 1 |
Wiring

Place the LEDs on the breadboard like the face of a dice:
A B
C D E
F G
| Component | Position | Arduino pin |
|---|---|---|
| LED A | Top left | D2 |
| LED B | Top right | D3 |
| LED C | Middle left | D4 |
| LED D | Centre | D5 |
| LED E | Middle right | D6 |
| LED F | Bottom left | D7 |
| LED G | Bottom right | D8 |
| All LEDs | Short legs | GND |
| Button | One leg | D12 |
| Button | Diagonally opposite leg | GND |
The long leg of each LED is connected to its pin through a 220 Ω resistor.
Code
// Arduino Electronic Dice - ozandamci.com
const int ledPins[7] = {2, 3, 4, 5, 6, 7, 8}; // A, B, C, D, E, F, G
const int buttonPin = 12;
// Which LEDs light up for each face of the dice (A B C D E F G)
const bool patterns[6][7] = {
{0, 0, 0, 1, 0, 0, 0}, // 1
{1, 0, 0, 0, 0, 0, 1}, // 2
{1, 0, 0, 1, 0, 0, 1}, // 3
{1, 1, 0, 0, 0, 1, 1}, // 4
{1, 1, 0, 1, 0, 1, 1}, // 5
{1, 1, 1, 0, 1, 1, 1} // 6
};
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
randomSeed(analogRead(A0));
showDice(1);
}
void showDice(int number) {
for (int i = 0; i < 7; i++) {
digitalWrite(ledPins[i], patterns[number - 1][i]);
}
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
// The dice is rolling: random numbers that slow down
for (int wait = 30; wait < 300; wait += 25) {
showDice(random(1, 7));
delay(wait);
}
// Final number
int result = random(1, 7);
showDice(result);
// Wait for the button to be released
while (digitalRead(buttonPin) == LOW) {
}
delay(200);
}
}
No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install).
Step-by-step instructions
- Arrange the LEDs. Place the 7 LEDs on the breadboard in the dice layout above. Keep all the long legs facing the same way.
- Add the resistors. Connect a 220 Ω resistor to the long leg of each LED.
- Connect them to the pins. In the order shown in the table, connect A to D2, B to D3 … G to D8. Connect the short legs of the LEDs to the GND rail.
- Place the button. Connect one leg to D12 and the diagonally opposite leg to GND.
- Upload the code. Choose your board and port, then click Upload. At start-up, the dice should show 1.
- Roll the dice. Press the button; the LEDs should change quickly, slow down and stop on a number.
Troubleshooting
- The patterns look wrong: Check that the LEDs are connected to the pins in the right order. For example, when you roll a 1, only the centre LED (D5) should light up.
- You always get the same numbers: Make sure the
randomSeed(analogRead(A0))line is in the code and that the A0 pin is not connected to anything.
Try it yourself
- Add a “click click” sound with a buzzer while the dice is rolling.
- Add a second group of 7 LEDs to make a double dice for board games like backgammon.
For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.





“Arduino Electronic Dice with 7 LEDs and a Button (Code)” için 2 yorum