Build it step by step
0 of 8 doneFind the display pins
Check the datasheet: two common pins in the middle of the top and bottom rows.
Wire the common pin
Both common pins to GND.
Wire each segment
a to g on D2 to D8, each through 330 ohm.
Wire the button
D9 to GND.
Upload the sketch
The display shows a 6.
Press and hold the button
The segments spin round.
Release
A number from 1 to 6 shows.
Roll twenty times
Check that all six numbers come up.
How do you build an electronic dice?
A button rolls a random number from one to six on a single seven segment display, with a short spin animation. You need Arduino Uno R3, 7-segment display 1 digit (common cathode), Resistor 330 Ω × 7, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 6
Parts list
- Development BoardsBuyArduino Uno R3 × 1
- DisplaysBuy
- PassivesBuyResistor 330 Ω × 7
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Segment a | Uno D2 through 330 ohm | Top bar |
| Segment b | Uno D3 through 330 ohm | Top right |
| Segment c | Uno D4 through 330 ohm | Bottom right |
| Segment d | Uno D5 through 330 ohm | Bottom bar |
| Segment e | Uno D6 through 330 ohm | Bottom left |
| Segment f | Uno D7 through 330 ohm | Top left |
| Segment g | Uno D8 through 330 ohm | Middle bar |
| Common cathode pin | Uno GND | Both common pins of the display |
| Roll button | Uno D9 to GND | Internal pull-up, pressed reads LOW |
Code
A seven-segment digit is seven LEDs labelled a to g that share one pin, the common cathode, which goes to ground. To light a segment you put its pin HIGH through a resistor. Each digit is stored as a pattern of seven bits, so the number 2, which lights a, b, d, e and g, is 0101 1011 in binary, or 0x5B. The show() function writes each bit to its pin. Without resistors the LEDs and the Uno pins would be overloaded, so every segment has its own 330 ohm resistor, giving about 7 milliamps per segment. A computer cannot choose truly random numbers, so the sketch seeds the random number generator from a floating analog pin and, on each press, from the exact microsecond of your press, which you cannot repeat. random(1, 7) picks a number from 1 to 6. The spin animation runs a light round the outer segments and slows down, like a die losing speed. Try wiring a second display for a pair of dice.
Runs on a virtual Arduino Uno 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).
// Segments a to g are on pins 2 to 8. Bit 0 of a pattern is segment a, bit 6 is segment g.
const uint8_t SEG_PIN[7] = {2, 3, 4, 5, 6, 7, 8};
const uint8_t BTN = 9;
const uint8_t DIGIT[7] = {
0x3F, // 0 a b c d e f
0x06, // 1 b c
0x5B, // 2 a b d e g
0x4F, // 3 a b c d g
0x66, // 4 b c f g
0x6D, // 5 a c d f g
0x7D // 6 a c d e f g
};
void show(uint8_t pattern) {
for (uint8_t i = 0; i < 7; i++) digitalWrite(SEG_PIN[i], (pattern >> i) & 1);
}
void spin() {
// Light the outer segments a, b, c, d, e, f in turn, slowing down like a rolling die.
uint16_t gap = 40;
for (uint8_t n = 0; n < 24; n++) {
show(1 << (n % 6));
delay(gap);
gap += 6;
}
}
void setup() {
for (uint8_t i = 0; i < 7; i++) pinMode(SEG_PIN[i], OUTPUT);
pinMode(BTN, INPUT_PULLUP);
randomSeed(analogRead(A0));
show(DIGIT[6]); // start by showing a 6
}
void loop() {
if (digitalRead(BTN) == LOW) {
randomSeed(micros() + analogRead(A0)); // timing of your press adds real randomness
spin();
uint8_t roll = random(1, 7); // 1 to 6
show(DIGIT[roll]);
while (digitalRead(BTN) == LOW) delay(10);
delay(50);
}
}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 | Shows 6 |
| Press the button | Spinning animation for about a second |
| After the spin | A single digit 1 to 6 |
| Twenty rolls | No digit missing or repeated always |
| Hold the button | No extra rolls until released |
If it does not work
Some segments never light
Check the pin order and that each resistor is in the circuit.
Wrong digit shapes
The display may be common anode, invert the pattern bits with a tilde.
Segments are dim
Use 220 ohm instead of 330 ohm resistors.
Same number appears often
Leave A0 unconnected so it floats.
Display lights all segments always
The common pin is not connected to GND.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the electronic dice?
Arduino Uno R3, 7-segment display 1 digit (common cathode), Resistor 330 Ω × 7, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the electronic dice take to build?
About 60 minutes for a beginner maker, in 8 steps.
How do I know the electronic dice works?
Power on: Shows 6. Press the button: Spinning animation for about a second. After the spin: A single digit 1 to 6.
Some segments never light: what should I check?
Check the pin order and that each resistor is in the circuit.
Wrong digit shapes: what should I check?
The display may be common anode, invert the pattern bits with a tilde.
Segments are dim: what should I check?
Use 220 ohm instead of 330 ohm resistors.
Same number appears often: what should I check?
Leave A0 unconnected so it floats.
