Simon Says Game

A tutorial on creating a Simon Says game using the Raspberry Pi Pico

Introduction

In this tutorial, we will create a fun and interactive Simon Says game using the Raspberry Pi Pico microcontroller. This classic memory game will challenge players to remember and replicate a sequence of LED flashes. Each round, the game will display a new random sequence, increasing the difficulty as the player progresses. This project is perfect for beginners looking to learn about programming microcontrollers, working with LEDs, and handling user input.

We will create the game on breadboard with just a few components.

The image below is the popular Simon Says electronic game you can buy.

Components Needed

To build this Simon Says game, you will need the following components:

ComponentQuantity
Raspberry Pi Pico1
Micro USB Cable1
Breadboard1
WiresSeveral
LEDs (Blue,Green, Yellow and Red)1
Passive Buzzer1
4 Push Buttons1
1k Ohm Resistor3
220 Ohm Resistor1

Software

Thonny IDE: A Python IDE that supports MicroPython, which you will use to write and upload your code to the Pico.

MicroPython Firmware: Make sure your Raspberry Pi Pico has MicroPython installed. You can download it from the official Raspberry Pi website.

If it is your first time using the Raspberry Pi Pico, head on over to the Introduction section to learn how to setup the Pico with MicroPython

Schematic Diagram

Below is the wire diagram for the simon says game

Code:

Main

import machine
import time
import random

# Define GPIO pins for LEDs and buttons
LED_PINS = [12, 13, 14, 15]  # Blue, Red, Yellow, Green
BUTTON_PINS = [8, 9, 10, 11]  # Blue, Red, Yellow, Green

# Initialize LEDs and buttons
leds = [machine.Pin(pin, machine.Pin.OUT) for pin in LED_PINS]
buttons = [machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_UP) for pin in BUTTON_PINS]

# Game variables
sequence = []
user_input = []
speed = 1.0  # Start speed

def light_led(index):
    leds[index].on()
    time.sleep(0.5)  # LED on for 0.5 seconds
    leds[index].off()
    time.sleep(0.2)  # Short pause between LEDs

def flash_button(index):
    # Flash the button LED while pressed
    leds[index].on()
    time.sleep(0.2)
    leds[index].off()

def get_user_input():
    global user_input
    user_input = []
    
    while len(user_input) < len(sequence):
        for i, button in enumerate(buttons):
            if button.value() == 0:  # Button is pressed
                flash_button(i)  # Flash the corresponding LED
                user_input.append(i)  # Add the index of the pressed button
                time.sleep(0.3)  # Debounce delay
                while button.value() == 0:  # Wait for button release
                    time.sleep(0.2)

def check_sequence():
    return user_input == sequence

def play_game():
    global speed

    while True:
        # Generate a new LED index that is not already in the sequence
        next_led = random.randint(0, 3)  # Random index for LEDs
        sequence.append(next_led)
        print("Sequence:", sequence)  # Debugging line

        # Display the sequence
        for led_index in sequence:
            light_led(led_index)
            time.sleep(0.1)

        # Get user input
        get_user_input()

        # Check if the user input matches the sequence
        if not check_sequence():
            print("Game Over! You failed.")
            break

        print("Correct! Next round.")
        

play_game()