How To Make a Raspberry Pi Pico Reaction Game With PicoZero
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
- Raspberry Pi Pico
- 2 x Push buttons
- 1 x DC buzzer
- 1 x 10K Ohm Resistor (Brown-Black-Orange-Gold)
- 8 x Male to male jumper wires
- Large breadboard
- Modelling clay / Play-Doh
Wiring The Game
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.
Wiring your project
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.
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.
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.
4. Connect GND from the – rail to the Collector pin (right leg in diagram) of the NPN transistor.
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.
6. Insert the button for player one and wire one leg to GPIO19 and the other to the – rail.
7. Add the button for player two, connecting one leg to GPIO20 and the other to the – rail.
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 PicoZero 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.
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()
Read more: https://www.tomshardware.com/how-to/raspberry-pi-pico-reaction-game