Electronic Dice
Press a button, watch seven LEDs roll, and let your program pick a number from 1 to 6.
What you’ll make
Press a button and seven LEDs “roll,” then settle on a dice face from 1 to 6, chosen at random by your program.
What you need
- A Raspberry Pi Pico with its header pins already fitted (Pico H or Pico WH), a data-capable USB cable, and Thonny with MicroPython (Lesson 24)
- Two breadboards: a full-size one for the Pico and a half-size one for the dice face, plus jumper wires
- A printed copy of Raspberry Pi’s Pico pinout diagram
- 7 LEDs, 5 mm, all the same colour (red is easiest)
- 7 resistors, 220 Ω
- 1 pushbutton
Safety numbers for this build
| Power | 5 V from USB; the Pico’s pins use 3.3 V |
|---|---|
| LED current | about 6 mA each; at most six LEDs on at once, about 36 mA in total, under the Pico’s limit of about 50 mA for all pins together |
| Supervision | An adult nearby for the setup |
Build it
- Unplug the USB cable. Press the Pico into the full-size breadboard across the centre gap, with pin 1 in row 1.
- Ground: jumper from row 8, column a (GND, pin 8) to the main board’s left − rail. Then jumper from that rail to both − rails on the half-size dice-face board.
- Watch the GND pin: the LED pins are GP2 to GP8, which are physical pins 4, 5, 6, 7, then 9, 10 and 11. Pin 8 between them is GND, so don’t count straight down.
- The dice face: each breadboard row half (a to e, or f to j) is one connection, so each LED needs rows of its own. Build the face on the half-size board using this table. “Rail” means the − rail along that side. The resistors bend to span two rows, and each short leg bends out to reach the rail.
| TL (GP2, pin 4) | LED long leg row 3 col a, short leg left rail; resistor row 1 col c to row 3 col c; jumper from main row 4 col a to face row 1 col e |
|---|---|
| ML (GP4, pin 6) | long leg row 8 col a, short leg left rail; resistor row 6 col c to row 8 col c; jumper from main row 6 col a to face row 6 col e |
| BL (GP7, pin 10) | long leg row 13 col a, short leg left rail; resistor row 11 col c to row 13 col c; jumper from main row 10 col a to face row 11 col e |
| C (GP5, pin 7) | across the centre gap: long leg row 7 col e, short leg row 7 col f; jumper row 7 col j to right rail; resistor row 5 col b to row 7 col b; jumper from main row 7 col a to face row 5 col d |
| TR (GP3, pin 5) | long leg row 3 col j, short leg right rail; resistor row 1 col h to row 3 col h; jumper from main row 5 col a to face row 1 col f |
| MR (GP6, pin 9) | long leg row 8 col j, short leg right rail; resistor row 6 col h to row 8 col h; jumper from main row 9 col a to face row 6 col f |
| BR (GP8, pin 11) | long leg row 13 col j, short leg right rail; resistor row 11 col h to row 13 col h; jumper from main row 11 col a to face row 11 col f |
- Button: place it across the dice-face board’s centre gap at rows 18 to 20. GP16 is physical pin 21 on the Pico’s right side (row 20, column j). Jumper from main row 20 col j to face row 18 col a, and from face row 20 col j to the right rail. This uses diagonally opposite button legs.
- Check every connection against the table and the pinout diagram, then plug in the USB cable.
Program it
from machine import Pin
import time, random
# LED positions: TL TR
# ML C MR
# BL BR
pins = {"TL": 2, "TR": 3, "ML": 4, "C": 5, "MR": 6, "BL": 7, "BR": 8}
leds = {name: Pin(gp, Pin.OUT) for name, gp in pins.items()}
button = Pin(16, Pin.IN, Pin.PULL_UP)
faces = {
1: ["C"],
2: ["TL", "BR"],
3: ["TL", "C", "BR"],
4: ["TL", "TR", "BL", "BR"],
5: ["TL", "TR", "C", "BL", "BR"],
6: ["TL", "TR", "ML", "MR", "BL", "BR"],
}
def show(n):
for name, led in leds.items():
led.value(1 if name in faces[n] else 0)
show(1)
while True:
if button.value() == 0: # button pressed
for i in range(12): # quick "rolling" animation
show(random.randint(1, 6))
time.sleep(0.05 + i * 0.02)
show(random.randint(1, 6)) # the final roll
while button.value() == 0: # wait for release
time.sleep(0.01)
time.sleep(0.05) # let the contacts settle
time.sleep(0.01)Run it and press the button. To make it start by itself, choose File, Save as, then Raspberry Pi Pico, and name it main.py.
How it works
The faces table lists which LEDs make each dice face. random.randint(1, 6) picks a whole number from 1 to 6. The program waits for the button (which reads 0 when pressed, thanks to the pull-up), plays a short animation that slows down like a real die, shows the final number, then waits for you to let go, with a short pause so a bouncing contact (Lesson 11) can’t start a second roll.
Strictly, computers make pseudo-random numbers from a mathematical recipe, but for a dice game they are random enough.
It doesn’t work?
- One LED never lights: check its orientation and that its resistor goes to the right GP pin.
- The pattern looks wrong: two LEDs may be connected to swapped pins. Compare with the TL/TR/ML/C/MR/BL/BR list.
- The button does nothing: use diagonally opposite button legs, one to GP16 and one to the − rail.
For teachers and parents
Open the teacher guide
Time: 60 minutes.
Learners will: wire multiple outputs and an input; read a program that uses a lookup table, loops and random numbers; collect and discuss probability data.
Risk and supervision: low. USB power only.
Discussion questions: Is the dice fair? How could you test it? Why does the program wait for the button to be released?
Common misconceptions: “a 6 coming up three times in a row means the dice is broken” (streaks happen by chance); “the program counts presses” (it only checks whether the button is down right now).
Worksheet: 1. Which LEDs light for a 3? 2. What range of numbers can random.randint(1, 6) give? 3. In 60 rolls, about how many 4s would you expect? 4. Why does the program wait for the button to be released?
Answers: 1. TL, C and BR. 2. 1 to 6, including both. 3. About 10, but anywhere from roughly 5 to 15 is normal. 4. So one press gives one roll.
Cross-curricular link: pool every group’s tallies into one class table. With hundreds of rolls, the results move closer to equal, a hands-on look at probability.
Learn more
- Raspberry Pi: Pico documentation: official getting-started guides
- Project 6: Traffic Light Controller
- Back to Electronics School
Electronics School is part of the Discovery Zone. Every activity here runs on small batteries or a USB cable: never wall outlets. The one plug-in tool is the optional soldering iron, set up by an adult.