Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Electronic dice on an LED bar: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
RollPush button
Dice displayLED 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.

#ColourFrom (part and leg)To (header pin)
1ColouredRoll - SignalPin 13 (GPIO27)
2BlackRoll - GNDPin 6 (GND)
3ColouredDice display - SignalPin 29 (GPIO5)
4ColouredDice display - SignalPin 31 (GPIO6)
5ColouredDice display - SignalPin 33 (GPIO13)
6ColouredDice display - SignalPin 35 (GPIO19)
7ColouredDice display - SignalPin 37 (GPIO26)
8BlackDice display - GNDPin 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

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.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4330 ohm resistor, 1/4 W
One per LED
5Robu.in Amazon.in ThinkRobotics
5LED bar graph module (5 LEDs)1Robu.in Amazon.in ThinkRobotics
6Tactile push button (12 mm, with cap)1Robu.in Amazon.in ThinkRobotics
7Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
8Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 38. Quiz buzzer for two teamsAll projects40. Button code lock →

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.