Using a breadboard and some simple components, make a super-fun reaction game while learning how to use a breadboard to make an electronic circuit, as well as programming in Python to make your game even better.
The game involves a race against an opponent, but there are a handful of trick signals thrown in for good measure, so don’t get too complacent. The project is designed as an extension to the projects found in the Monk Makes Raspberry Pi Electronics Starter Kit.
Reaction game: Connect the LEDs
Gather the components as listed on the left. Following the breadboard diagram, attach the two red LEDs to the breadboard, at either end. You must ensure that the long leg of the LED is facing down, on both LEDs.
This is the positive leg, which will be connected to a positive output on the Raspberry Pi. The big RGB LED can be connected in the middle of the breadboard, on the right-hand side, as shown. There is one leg longer than the rest, which will be connected to the second hole from the top (shown by a red circle on the diagram). This is the negative leg.
Add the resistors
In order for the LEDs to work correctly, a resistor needs to be added in series to each one, limiting the flow of current so they don’t melt! You will need five resistors, as the RGB LED counts as three separate LEDs that share the same cathode (negative leg). Connect three resistors to the LED’s positive legs, bridging the gap across the centre of the breadboard, as shown in the diagram. All of the pins in each row are connected to each other inside the breadboard, with a gap in the centre.
Add the buttons
This project needs two buttons: one for each player. Some small buttons have four legs, as shown in the diagram, and a connection is made between the diagonal pins when it is pressed. If you have a button with two legs, simply connect it to the breadboard with one leg on either side of the central gap, ensuring that the jumper wires are connected to the same row as the button. As you can see from the diagram, the button will be connecting a Raspberry Pi input pin to ground when pressed.
Finish up the Reaction Game circuit
Using the five male-to-male jumper leads, follow the diagram to connect all the negative legs on the LEDs and the buttons together, which can then be connected to a ground pin on the Raspberry Pi. The eight male-to-female jumper leads can then be used to connect the positive sides of the LEDs to the Pi’s output pins, as well as connecting the two buttons and the common ground connection. Ensure that each jumper lead is connected to the correct pin on the Raspberry Pi, as this is crucial for the code to run correctly with the hardware.
Install the code
Using the latest version of Raspbian (PIXEL) on your Raspberry Pi, you can now boot up. If you are not using this version of Raspbian, you will need to install the GPIO Zero library if you haven’t already. You’ll need to make sure that your Pi is connected to the internet, via WiFi or Ethernet. When booted, open the terminal window and type:
git clone https://github.com/henrybudden/rpesk-advanced/. This
will download the Python files from GitHub, onto your Raspberry Pi. Once this is completed, you can navigate to the folder using:
And then run the game by typing:
sudo python 02_reaction_game.py.
How to play the Reaction Game
When you run the program, you will first need to type the names of the two players. Player one’s button is located at the bottom of the breadboard and player two’s at the top (as shown on the diagram). Whenever the RGB LED in the centre of the breadboard lights up in any colour other than red, press your button as fast as you can. The first player to hit their button will gain a point. However, beware! If the RGB LED lights up in red, do not press your button. If you do, one point will be deducted from your score! After each round, the score for each player is displayed on the screen, and one of the small red LEDs on the breadboard will light up on the side of the player in the lead. If both LEDs are lit up, you are tied. Each round will begin automatically, so keep going until you get too tired.