Engineering · Raspberry Pi · Project 39 of 47 · Games and logic · Beginner
Electronic dice on an LED bar
Press the button and the LED bar races, then stops on a random number from 1 to 5. A small step into random numbers.

Parts in this circuit
| Part | Type |
|---|---|
| Roll | Push button |
| Dice display | LED bar |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Roll - Signal | Pin 13 (GPIO27) |
| 2 | Black | Roll - GND | Pin 6 (GND) |
| 3 | Coloured | Dice display - Signal | Pin 29 (GPIO5) |
| 4 | Coloured | Dice display - Signal | Pin 31 (GPIO6) |
| 5 | Coloured | Dice display - Signal | Pin 33 (GPIO13) |
| 6 | Coloured | Dice display - Signal | Pin 35 (GPIO19) |
| 7 | Coloured | Dice display - Signal | Pin 37 (GPIO26) |
| 8 | Black | Dice display - GND | Pin 9 (GND) |
Python code (gpiozero)
from gpiozero import Button, LEDBarGraph
from random import randint
from time import sleep
button = Button(27)
bar = LEDBarGraph(5, 6, 13, 19, 26)
print("Press the Roll button")
while True:
button.wait_for_press()
for _ in range(12): # a quick "rolling" animation
bar.value = randint(1, 5) / 5
sleep(0.08)
number = randint(1, 5)
bar.value = number / 5 # number of LEDs lit = the roll
print("You rolled a", number)
button.wait_for_release()
How the code works
- randint(1, 5) gives a whole number from 1 to 5.
- The loop shows 12 random numbers very quickly, which looks like a rolling die.
- bar.value = number / 5 lights that many LEDs out of five.
You will learn
- Random numbers with randint
- Loops with range
- Showing a number as lit LEDs
Try this
- Roll 20 times and count how often you get a 5.
- Change the code to roll 1 to 6 with a six-LED bar.
- Add a buzzer that clicks on each rolling step.
On a real Pi
- A real bar of LEDs needs one resistor per LED. A bar module has them built in.
- The Pi random numbers are good enough for games. Not for security or lotteries.
- Seven LEDs in dice layout look better. Try arranging them yourself.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 330 ohm resistor, 1/4 W One per LED | 5 | Robu.in Amazon.in ThinkRobotics |
| 5 | LED bar graph module (5 LEDs) | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Tactile push button (12 mm, with cap) | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.