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.
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 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.
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.
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.
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()