How to Create a Raspberry Pi Pico Reaction Game with PicoZero
It’s 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 turned out to be a beast much more efficient.
With the Raspberry Pi Pico we can flash LEDs, 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 removed.
MicroPython is a great language for the Raspberry Pi Pico, (only improved by CircuitPython in our humble opinion) but for the uninitiated it can quickly become “word soup”. We need to abstract the code, make it easier to understand and luckily PicoZero seems to be the solution.
PicoZero takes a page out of the book from GPIO Zero in that it is a beginner-friendly Python library for everyday electronics. GPIO Zero, created and maintained by former Raspberry Pi employee Ben Nuttall and Canonical software engineer Dave Jones has created seismic change for learners. He simplified and illustrated how users can interact with common electronic devices using Python on the Raspberry Pi. PicoZero from the Raspberry Pi Foundation follows the same principles and while still in beta, we we just had to build a project with him.
Our project will demonstrate the basic inputs and outputs of the PicoZero module via a reaction game, designed to test the reflexes of two players in the face of an exceptionally loud buzzer.
For this project you will need
- Raspberry Pi Pico
- 2 x push buttons
- 1 x DC Buzzer
- 1 x 10K Ohm Resistor (Brown-Black-Orange-Gold)
- 8 male to male jumper wires
- large bread board
- Modeling clay / Play-Doh
The material construction of this project is simple, if we take it step by step. We have two inputs, our buttons which are used by players. The only output is a buzzer. Typically the buzzer would be connected directly to the GPIO, but in our testing we found it to be too quiet. To work around this problem, we used an NPN transistor (2n 2222) which is essentially an electronically controlled switch. By connecting the center (Base) pin to the Pico’s GPIO through a 10K Ohm resistor, we can trigger the switch to close and allow the buzzer’s GND pin to connect to the Raspberry Pi Pico’s GND.
1. Connect the 3V3(Out) and GND pins of the Pico to the + and – rails (respectively) of the breadboard. This will turn all the pins on each of these rails to 3V3 (3.3V) and GND.
2. Slightly bend the center pin of the NPN transistor backwards and put it in the breadboard with the flat side of the chip facing out.
3. Using the 10K Ohm resistor, connect Pico’s GPIO16 to the center pin of the NPN transistor. This is the base pin, which is used to control the state of the transistor.
4. Connect GND of rail – to collector pin (right leg in the diagram) of the NPN transistor.
5. Connect the negative leg of the buzzer (black wire) to transmitter pin (left leg in the diagram) of the NPN transistor. Connect the positive leg of the buzzer (red wire) to the + rail of the breadboard. We have now wired 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 causes the buzzer to sound. Note that a drop of Play-Doh or modeling clay can be used to lightly cover the buzzer while reducing the volume to less piercing levels.
6. Insert button for player one and wire a leg to GPIO19 and the other at the – rail.
7. Add the button for player two, connecting one leg to the GPIO20 and the other at the – rail.
Installing PicoZero on your Raspberry Pi Pico
PicoZero is a MicroPython module that abstracts the intricacies of electronics and programming for those new to the subjects. We will use the Thonny Python editor, recommended by Raspberry Pi as the default editor.
1. Install (or update) the latest thonny python editor for your operating system.
2. Connect your Raspberry Pi Pico and start Thonny. Thonny should automatically detect the Pico and connect ready to use. Your Raspberry Pi Pico should be running MicroPython, otherwise follow the instructions in our How to configure and program Raspberry Pi Pico guide.
3. Open a PicoZero source web browser and copy the code picozero.py.
4. Paste the code into a blank document.
5. Save the code to the Raspberry Pi Pico as picozero.py
The project code
The project code is delightfully simple thanks to PicoZero’s abstraction. The goal of the project is to create a reaction game. A buzzer will sound a countdown for players, then pause for a random amount of time between five and ten seconds before sounding again. The fastest player to press 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 the 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 allows 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 that each button is connected to.
p1 = Button(19) p2 = Button(20)
6. Use a for loop to sound the buzzer three times, a countdown that will indicate the game has started. A short sleep of 0.1 will cause the buzzer to sound briefly, turning off the buzzer for 0.9 seconds will give us a steady “heartbeat” countdown.
for i in range(3): buzzer.beep() sleep(0.1) buzzer.off() sleep(0.9)
7. Create an object, time, which will store a randomly chosen floating number between 5 and 10 seconds, so use this value to pause the game. It’s time for anticipation, will the buzzer sound quickly or do 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. Activate the buzzer. The buzzer will sound until a player presses its button.
9. Create a while True loop to run the game code. This will continuously run the code that checks which player pressed their button.
ten. Check if player 1 button was pressed. If so, a for loop will print “Green Wins” five times to the Python Shell. Then the loop is broken, which prevents the code from executing.
if p1.is_pressed: for i in range(5): print("Green Wins") break
11. Do the same check for the player 2 button. The only difference is that the for loop displays “Yellow Wins”.
if p2.is_pressed: for i in range(5): print("Yellow Wins") break
12. Turn off the buzzer, ending the game.
13. Save the code as reaction.py on the Raspberry Pi Pico.
14. Click Run to start the game. When the game is over, click Run to start the game over and challenge the contestant to a rematch!
Complete list of codes
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()