Fareed Mohmed← Project library

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?

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Simon memory game: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Red LEDLED
Green LEDLED
Yellow LEDLED
Blue LEDLED
Red buttonPush button
Green buttonPush button
Yellow buttonPush button
Blue buttonPush button
BuzzerBuzzer

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)
1ColouredRed LED - anode +Pin 11 (GPIO17)
2BlackRed LED - cathode -Pin 6 (GND)
3ColouredGreen LED - anode +Pin 13 (GPIO27)
4BlackGreen LED - cathode -Pin 9 (GND)
5ColouredYellow LED - anode +Pin 15 (GPIO22)
6BlackYellow LED - cathode -Pin 14 (GND)
7ColouredBlue LED - anode +Pin 16 (GPIO23)
8BlackBlue LED - cathode -Pin 20 (GND)
9ColouredRed button - SignalPin 29 (GPIO5)
10BlackRed button - GNDPin 25 (GND)
11ColouredGreen button - SignalPin 31 (GPIO6)
12BlackGreen button - GNDPin 30 (GND)
13ColouredYellow button - SignalPin 33 (GPIO13)
14BlackYellow button - GNDPin 34 (GND)
15ColouredBlue button - SignalPin 35 (GPIO19)
16BlackBlue button - GNDPin 39 (GND)
17ColouredBuzzer - +Pin 18 (GPIO24)
18BlackBuzzer - -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

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.

#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
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
55 mm LED, green1Robu.in Amazon.in ThinkRobotics
65 mm LED, yellow1Robu.in Amazon.in ThinkRobotics
75 mm LED, blue1Robu.in Amazon.in ThinkRobotics
8330 ohm resistor, 1/4 W
One per LED
4Robu.in Amazon.in ThinkRobotics
9Tactile push button (12 mm, with cap)4Robu.in Amazon.in ThinkRobotics
10Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
11Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
12Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 36. Automatic gate with ultrasonic sensorAll projects38. Quiz buzzer for two teams →

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.