How To Make a Raspberry Pi Pico Reaction Game With PicoZero

Raspberry Pi Pico Reaction Game With PicoZero
(Image credit: Tom's Hardware)

It is fair to say that the Raspberry Pi Pico disrupted the microcontroller market in early 2021. The $4 board has more in common with an Arduino than a Raspberry Pi, but the Pico has proven to be a much more capable beast.

With the Raspberry Pi Pico, we can make LEDs blink, build robots and even run Doom. But for those new to programming, microcontrollers and electronics there are a few barriers to entry that need to be broken down.

MicroPython is an excellent language for the Raspberry Pi Pico, (bettered only by CircuitPython in our humble opinion) but to the uninitiated it can quickly become “word soup”. Abstracting the code, making it simpler to understand is what we need and luckily PicoZero looks to be the solution.

PicoZero takes a page from GPIO Zero’s book in that it is a beginner friendly Python library for common electronics. GPIO Zero, created and maintained by former Raspberry Pi employee Ben Nuttall and Canonical software engineer Dave Jones created a seismic shift for learners. It simplified and exemplified how users could interact with common electronics using Python on the Raspberry Pi. PicoZero from the Raspberry Pi Foundation follows the same principles and while it is still in beta, we just had to build a project with it.

Our project will introduce the basic inputs and outputs of the PicoZero module via a reaction game, designed to test the reflexes of two players versus an exceptionally loud buzzer.

For This Project You Will Need

Wiring The Game

(Image credit: Tom's Hardware)

The hardware build for this project is simple, if we take it step by step. We have two inputs, our buttons which are used by the players. The only output is a buzzer. Typically the buzzer would be connected directly to the GPIO but in our tests we found that it was too quiet. To get around this, we have used an NPN transistor (2n 2222) which is essentially an electronically controlled switch. By connecting the middle pin (Base) to the GPIO of the Pico via a 10K Ohm resistor we can trigger the switch to close, and enable the GND pin of the buzzer to connect to the GND of the Raspberry Pi Pico.

1. Connect the 3V3(Out) and GND pins of the Pico to the + and - (respectively) rails of the breadboard. This will turn all of the pins in each of these rails into 3V3 (3.3V) and GND.

(Image credit: Tom's Hardware)

2. Bend the middle pin of the NPN transistor slightly backward and place it into the breadboard with the flat of the chip facing away.

(Image credit: Tom's Hardware)

3. Using the 10K Ohm resistor, connect GPIO16 of the Pico to the middle pin of the NPN transistor. This is the Base pin, which is used to control the state of the transistor.

(Image credit: Tom's Hardware)

4. Connect GND from the - rail to the Collector pin (right leg in diagram) of the NPN transistor.

(Image credit: Tom's Hardware)

5. Connect the negative leg of the buzzer (black wire) to the Emitter pin (left leg in diagram) of the NPN transistor. Connect the positive leg of the buzzer (red wire) to the + rail of the breadboard. We’ve now wired up an open switch. When the NPN transistor is not triggered, the connection is broken.

When we send a signal to the Base pin, we cause the circuit to close which sounds the buzzer. Note that a blob of Play-Doh or modelling clay can be used to lightly cover the buzzer while reducing the volume to less ear piercing levels.

(Image credit: Tom's Hardware)

6. Insert the button for player one and wire one leg to GPIO19 and the other to the - rail.

(Image credit: Tom's Hardware)

7. Add the button for player two, connecting one leg to GPIO20 and the other to the - rail.

(Image credit: Tom's Hardware)

Installing PicoZero on your Raspberry Pi Pico

PicoZero is a MicroPython module that abstracts the complexities of electronics and programming for those new to the topics. We’ll be using the Thonny Python editor, recommended by Raspberry Pi as the default editor.

1. Install (or update to) the latest Thonny Python editor for your operating system.

2. Connect your Raspberry Pi Pico and start Thonny. Thonny should auto detect the Pico and connect ready for use. Your Raspberry Pi Pico should be running MicroPython, if not follow the instructions in our How to Set Up and Program the Raspberry Pi Pico guide.

3. Open a web browser to the PicoZero source and copy the picozero.py code.

4. Paste the code into a blank document.

5. Save the code, to the Raspberry Pi Pico as picozero.py

The Project Code

The code for the project is delightfully simple thanks to PicoZero’s abstraction. The goal of the project is to create a reaction game. A buzzer will beep a countdown for the players, then pause for a random time between five and ten seconds before sounding again. The fastest player to hit their button will win the game, and turn off the annoying buzzer.

1. Create a new blank file in Thonny.

2. From the PicoZero module import the Button and Buzzer classes. These two classes are used to interact with the physical buttons and buzzer.

from picozero import Button, Buzzer

3. Import two more modules. Time is used to control the delay in our code; the sleep function adds a pause. Random enables the use of classes and functions for random number generation, random choice.

from time import sleep
import random

4. Create an object, buzzer, which will store the GPIO pin connected to the transistor that controls our button.

buzzer = Buzzer(16)

5. Create two more objects, p1 and p2 which will store the GPIO pin to which each button is connected.

p1 = Button(19)
p2 = Button(20)

6. Use a for loop to beep the buzzer three times, a countdown that will indicate the game has started. A short 0.1 sleep will cause the buzzer to briefly sound, turning the buzzer off for 0.9 seconds give us a regular “heart beat” countdown.

for i in range(3):
   buzzer.beep()
   sleep(0.1)
   buzzer.off()
   sleep(0.9)

7. Create an object, time, that will store a randomly chosen float number between 5 and 10 seconds, then use that value to pause the game. This is the moment of anticipation, will the buzzer sound quickly or will we have to wait the full 10 seconds? This is the random nature of the reaction game.

time = random.uniform(5, 10)
sleep(time)

8. Turn the buzzer on. The buzzer will sound until a player presses their button.

buzzer.on()

9. Create a while True loop to run the game code. This will constantly run the code that checks which player has pressed their button.

while True:

10. Check if player 1’s button has been pressed. If it has, then a for loop will print “Green Wins” five times to the Python Shell. Then the loop is broken, stopping the code from running.

  if p1.is_pressed:
        for i in range(5):
            print("Green Wins")
        break

11. Do the same check for player 2’s button. The only difference is that the for loop print “Yellow Wins”.

   if p2.is_pressed:
       for i in range(5):
           print("Yellow Wins")
       break

12. Turn the buzzer off, ending the game.

buzzer.off()

13. Save the code as reaction.py to the Raspberry Pi Pico.

14. Click Run to start the game. Once the game is complete, click on Run to start the game again, and challenge the competitor to a rematch! 

Complete Code Listing

from picozero import Button, Buzzer
from time import sleep
import random

buzzer = Buzzer(16)
p1 = Button(19)
p2 = Button(20)

for i in range(3):
    buzzer.beep()
    sleep(0.1)
    buzzer.off()
    sleep(0.9)
time = random.uniform(5, 10)
sleep(time)
buzzer.on()
while True:
    if p1.is_pressed:
        for i in range(5):
            print("Green Wins")
        break
    if p2.is_pressed:
        for i in range(5):
            print("Yellow Wins")
        break
buzzer.off()
Les Pounder

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".