Engineering · Raspberry Pi · Project 37 of 47 · Games and logic · Intermediate
Simon memory game
Watch the LEDs flash in a pattern, then repeat it with the buttons. Each round adds one more step. How far can you go?

Parts in this circuit
| Part | Type |
|---|---|
| Red LED | LED |
| Green LED | LED |
| Yellow LED | LED |
| Blue LED | LED |
| Red button | Push button |
| Green button | Push button |
| Yellow button | Push button |
| Blue button | Push button |
| Buzzer | Buzzer |
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 | Red LED - anode + | Pin 11 (GPIO17) |
| 2 | Black | Red LED - cathode - | Pin 6 (GND) |
| 3 | Coloured | Green LED - anode + | Pin 13 (GPIO27) |
| 4 | Black | Green LED - cathode - | Pin 9 (GND) |
| 5 | Coloured | Yellow LED - anode + | Pin 15 (GPIO22) |
| 6 | Black | Yellow LED - cathode - | Pin 14 (GND) |
| 7 | Coloured | Blue LED - anode + | Pin 16 (GPIO23) |
| 8 | Black | Blue LED - cathode - | Pin 20 (GND) |
| 9 | Coloured | Red button - Signal | Pin 29 (GPIO5) |
| 10 | Black | Red button - GND | Pin 25 (GND) |
| 11 | Coloured | Green button - Signal | Pin 31 (GPIO6) |
| 12 | Black | Green button - GND | Pin 30 (GND) |
| 13 | Coloured | Yellow button - Signal | Pin 33 (GPIO13) |
| 14 | Black | Yellow button - GND | Pin 34 (GND) |
| 15 | Coloured | Blue button - Signal | Pin 35 (GPIO19) |
| 16 | Black | Blue button - GND | Pin 39 (GND) |
| 17 | Coloured | Buzzer - + | Pin 18 (GPIO24) |
| 18 | Black | Buzzer - - | Pin 6 (GND) |
Python code (gpiozero)
from gpiozero import LED, Button, Buzzer
from random import choice
from time import sleep
leds = [LED(17), LED(27), LED(22), LED(23)]
buttons = [Button(5), Button(6), Button(13), Button(19)]
buzzer = Buzzer(24)
def flash(i):
leds[i].on()
buzzer.on()
sleep(0.4)
leds[i].off()
buzzer.off()
sleep(0.2)
def wait_for_choice():
while True:
for i in range(4):
if buttons[i].is_pressed:
leds[i].on()
while buttons[i].is_pressed:
sleep(0.02)
leds[i].off()
return i
sleep(0.02)
sequence = []
while True:
sequence.append(choice([0, 1, 2, 3])) # one new random step
print("Round", len(sequence), "- watch!")
sleep(0.6)
for i in sequence:
flash(i)
print("Your turn")
for i in sequence:
if wait_for_choice() != i:
print("Wrong! You reached round", len(sequence))
buzzer.beep(0.5, 0.1, n=1)
sequence = []
sleep(2)
break
else:
print("Correct!")
sleep(0.8)
How the code works
- leds and buttons are lists. Index 0 is red, 1 is green, 2 is yellow, 3 is blue.
- choice() picks a random LED and the sequence list remembers every step so far.
- wait_for_choice() waits until a button is pressed and gives back its number.
- for ... else: the else part runs only when the loop finished without break, which means every step was right.
You will learn
- Lists of devices and indexes
- Random numbers
- Remembering a growing sequence
Try this
- Play 3 rounds. Hold the colour buttons in the inputs box.
- Make the flashes faster as the rounds go up.
- Print your best round at the end of each game.
On a real Pi
- Use 220 to 470 ohm resistors with every LED.
- Buttons bounce. gpiozero handles this, but with RPi.GPIO add a bounce time.
- You can build the same game on a breadboard with four coloured LEDs and arcade buttons.
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 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 5 mm LED, green | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | 5 mm LED, yellow | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 5 mm LED, blue | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | 330 ohm resistor, 1/4 W One per LED | 4 | Robu.in Amazon.in ThinkRobotics |
| 9 | Tactile push button (12 mm, with cap) | 4 | Robu.in Amazon.in ThinkRobotics |
| 10 | Active buzzer, 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 12 | 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.