Electronic dice with a 7-segment display
Build it step by step
0 of 9 donePlace the chip and display
Put the CD4511 across the breadboard centre gap and the display next to it.
Wire the power
Pin 16 to 5V, pin 8 to GND, and join pins 3 and 4 to 5V and pin 5 to GND.
Wire the number lines
Pins 7, 1, 2 and 6 to Uno D2, D3, D4 and D5.
Wire the segments
CD4511 outputs a to g each through a 330 ohm resistor to the matching display segment.
Wire the display common
The common pin of the display to GND.
Wire the button
One leg to D8 and the other to GND.
Upload the sketch
The display shows 0 and the monitor says Press the button to roll.
Press and hold the button
The digit rolls fast, slows down and stops on a number.
Release and press again
Another number is chosen.
How do you build an electronic dice with a 7-segment display?
Press the button and a single 7-segment digit rolls through numbers, slows down and settles on a random one to six. A CD4511 chip turns four wires into the seven segments, so you learn how decoders save pins. You need Arduino Uno R3, CD4511 BCD to 7-segment decoder, 7-segment display 1 digit (common cathode), Tactile push button 6 mm, Resistor 330 Ω × 7, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 9 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 7
Parts list
- Development BoardsBuyArduino Uno R3 × 1
- Integrated CircuitsBuy
- DisplaysBuy
- SwitchesBuy
- PassivesBuyResistor 330 Ω × 7
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| CD4511 pin 16 VDD and pin 8 VSS | Uno 5V and GND | Power |
| CD4511 pin 3 LT and pin 4 BI | Uno 5V | Lamp test off, blanking off |
| CD4511 pin 5 LE | Uno GND | Latch always open so it follows the inputs |
| CD4511 pin 7 A, pin 1 B, pin 2 C, pin 6 D | Uno D2, D3, D4, D5 | The number in binary, A is the smallest bit |
| CD4511 outputs a b c d e f g | Pins 13, 12, 11, 10, 9, 15, 14, each through 330 ohms to the display | One resistor per segment |
| Display common pins | Uno GND | This is a common cathode display |
| Button | Uno D8 and GND | Press to roll |
Code
A seven-segment digit has seven lights, and driving it straight from the Uno would use seven pins. The CD4511 is a decoder that fixes that. You give it a number from 0 to 9 on four wires, written in binary, and it lights the right segments by itself. The four pins A, B, C and D carry the bits of the number with weights 1, 2, 4 and 8, so 5 is 0101, which is D low, C high, B low and A high. The sketch does exactly this in show(), taking one bit at a time from the number with a shift. The three control pins are tied so the chip is simply always on: LT is lamp test and BI is blanking, both are active low and are held high, while LE is the latch and is held low, so the outputs follow the inputs live. Each segment needs its own 330 ohm resistor, because the CD4511 outputs have no current limit. A roll shows random numbers fast, and the delay is made a little longer each time round, so the digit slows and settles, just like a tumbling die. The random numbers come from random(1, 7), which gives 1 to 6, and randomSeed() is fed from an unconnected analog pin, so the sequence is different on every start. The final value is printed to the serial monitor. Try adding a roll counter or a second digit, or stretch it to a 12 sided die by decoding two digits.
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).
const uint8_t BCD_PINS[4] = {2, 3, 4, 5}; // A (1), B (2), C (4), D (8)
const uint8_t BTN = 8;
void show(uint8_t n) {
for (uint8_t i = 0; i < 4; i++) {
digitalWrite(BCD_PINS[i], (n >> i) & 1); // one bit of the number per pin
}
}
void setup() {
for (uint8_t i = 0; i < 4; i++) pinMode(BCD_PINS[i], OUTPUT);
pinMode(BTN, INPUT_PULLUP);
Serial.begin(9600);
randomSeed(analogRead(A0) + micros()); // an unconnected pin is a bit random
show(0);
Serial.println(F("Press the button to roll"));
}
void loop() {
if (digitalRead(BTN) == LOW) {
uint8_t value = 1;
int wait = 40;
while (wait < 400) { // roll quickly, then slow down
value = random(1, 7); // 1 to 6
show(value);
delay(wait);
wait += wait / 4 + 5;
}
show(value);
Serial.print(F("Rolled "));
Serial.println(value);
while (digitalRead(BTN) == LOW) delay(10); // wait for the button to be released
delay(100);
}
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Press the button to roll
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 | The display shows 0 |
| Press the button | Digits roll and then settle between 1 and 6 |
| Roll 20 times | Every number from 1 to 6 appears, and 0 or 7 never do |
| Monitor | Each result is printed as Rolled and the number |
| Hold the button down | It rolls once and waits for release |
| Unplug the Uno and plug it in again | A different first roll sequence |
If it does not work
Display is blank
Check pin 4 BI is joined to 5V and the display common goes to GND.
Wrong digits
Swap the order of the four number wires, A must be the smallest bit on pin 7.
Digit shows odd shapes
The segment wires are in the wrong order, so check them against a to g.
Digit is dim
Use 220 ohm resistors instead of 330 ohm.
Same numbers every time
Keep randomSeed and leave A0 unconnected.
Button does nothing
Check it goes to D8 and GND.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the electronic dice with a 7-segment display?
Arduino Uno R3, CD4511 BCD to 7-segment decoder, 7-segment display 1 digit (common cathode), Tactile push button 6 mm, Resistor 330 Ω × 7, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the electronic dice with a 7-segment display take to build?
About 90 minutes for a beginner maker, in 9 steps.
How do I know the electronic dice with a 7-segment display works?
Power on: The display shows 0. Press the button: Digits roll and then settle between 1 and 6. Roll 20 times: Every number from 1 to 6 appears, and 0 or 7 never do.
Display is blank: what should I check?
Check pin 4 BI is joined to 5V and the display common goes to GND.
Wrong digits: what should I check?
Swap the order of the four number wires, A must be the smallest bit on pin 7.
Digit shows odd shapes: what should I check?
The segment wires are in the wrong order, so check them against a to g.
Digit is dim: what should I check?
Use 220 ohm resistors instead of 330 ohm.
