The has been with us for a few months now and there are a growing number of projects that make use of the extra GPIO pins.
For this project we will look back to our childhood and bring a much loved game from our past into the future. The humble board game.
Board games have been a traditional family pastime for many generations but with the rise of computer games their novelty has started to dwindle. These card and paper based games have little to offer the children of today who have been brought up on a diet of downloadable content packs and gamer scores.
But what if we could take a game from yesteryear and adapt it using the Raspberry Pi?
Meet the latest interactive board game...Pythons and Resistors
This project is split into three parts
- Assembling the board game
- Assembling and testing the electronic components
- Writing and testing the Python code
To build this project you will need the following equipment.
- 30x Female to male jumper cables
- 20x Male to male jumper cables
- Snakes and Ladders board game file
- Thick card or cardboard
- Sugru for player pieces
- Magnets to be inserted in to the player pieces. Neodymium are the strongest
The board game is based on a simple snakes and ladders setup, with 100 squares in total via a grid of 10 x 10 squares. The object of the game is for 2 or more players to roll a dice and move their game piece to match the number given on the dice. If the player lands on a python's head, then they will slither down the game board to the tail of the Python. If the player lands on the bottom of a resistor then they will climb up the game board. The winner is the first player to reach square 100, which is at the top left of the board.
Board game assembly
The board game components consists of
- The game board, printed on glossy photo paper
- Sugru rubber game pieces
- Neodymium magnets to trigger the reed switches
- Spare components to decorate the game pieces.
Start by printing at least two copies of the game board as it is easy to make a mistake when assembling the game. While they are drying open a pack or two of Sugru. Sugru is a form of air curing rubber that starts life as a sticky ball with a similar consistency to blutack. Form the Sugru into the shape that you wish. When you are happy with the shape add about three neodymium magnets to the base of the piece ensuring that they are gripped firmly by the sugru. Also if you wish decorate the game pieces with spare components such as LED or resistors.
Assemble and test the hardware
With the game board and player pieces completed our focus turn to the wiring and components that are under the board.
At the head of every python and the base of every resistor we are going to place a reed switch on the underside of the game board. These reed switches will be triggered by the magnets inside of our game pieces. A reed switch is a tiny switch contained inside of a glass vial. The contacts that form the switch mechanism come normally open so a current does not flow through the switch. By placing a magnet near to the switch we can trigger the contacts to quickly make contact, with a small audible click / pop being heard when this is the case. When contact is made the current will flow through the switch and in this case the current will flow to a GPIO pin on the Raspberry Pi which has been configured to be an input and react when this happens.
The best place to start is by fixing the reed switches into place on the underside of the board. To easily locate the correct spot I used a magnet on the printed side to trigger the reed switch on the underside. Once found I simply used cellotape over the glass vial of the switch to fix it into place. Repeat this action for all of the reed switches for all of the pythons and resistors.
With the switches in place we must now attach jumper cables to each end of the reed switch component. In one end of the switch we will supply 3v3 power from the Raspberry Pi 3v3 pin and the other will be connected to the corresponding pin. It doesn't matter which side that you choose, just make a note of which side you will be using as the 3v3 supply as we will go into more detail on the wiring for this later in the guide. For these connections use a female to female jumper lead and slide the connections over each end of the reed switch then use cellotape to secure them firmly in place. Do this for all of the reed switches.
Wiring the 3v3
The Raspberry Pi can supply 3 volts, commonly referred to as 3v3 from multiple pins on the GPIO but because for this project there are more switches than there are 3v3 pins on the Raspberry Pi we must break out the supply using a breadboard. A breadboard is a great prototyping tool that enables components and circuits to be tested before soldering. Typically a breadboard has two sets of two rails which are on the outer most reaches of the board. These rails are marked + and – and refer to a positive voltage, in this case our 3v3 supply from the Pi and a negative voltage which is commonly called Ground, often shortened to GND. Any connection made on a rail is available to all of the holes in the same rail as the supply. So a 3v3 supply from the Pi connected to the + rail will be available to all other holes in the + rail. For this project we need to connect the 3v3 supply from our Raspberry Pi to the + rail of our breadboard. 3V3 is supplied via pin 1 of the Raspberry Pi, which is the top left pin nearest the micro SD card slot. Connect pin 1 of your Raspberry Pi to the breadboard, and then using Male to Male jumper leads connect the reed switch 3v3 connections to the + rail of the breadboard.
Connecting the switches to their respective GPIO pins
In this project we have used a number of reed switches which are triggered by magnets and supplied 3v3 from the Raspberry Pi. When a magnet is placed over the reed switch, the switch will close and supply 3v3 from the Pi, through the switch and back to the Pi via another wire connected to the other side of the reed switch. On the Raspberry Pi we will configure the receiving pins for each reed switch attached to each Python and resistor square to wait for a signal, in this case the voltage change and when it receives it will trigger an audio file to play. To connect our switches to their corresponding GPIO pin, please refer to the table below.
Connecting our push button
With the reed switches complete, we now need to install the push button that controls the roll of our dice. Our push button is connected from the 3v rail of our breadboard and to the GPIO 13 pin, which is pin 33 using the logical numbering layout. The best way to ensure a good connection is to solder the wires directly to the push button. If you can't solder it is easy to learn but should be attempted with an experienced maker and with safety equipment present. There are many great videos on Youtube to help your learning including one from Carrie Anne at the Raspberry Pi Foundation
You can wind the wire around the terminals of the button for a temporary "no solder" test
|Board Square||Broadcom GPIO Numbering|
|Board Square||Broadcom GPIO Numbering|
Wiring up our dice
Rather than use a traditional dice, we will build our own dice using 6 LED connected to the Raspberry Pi GPIO. The LED will light up to signify the roll of our dice, so if I roll a 3 then 3 of the LED will light up. The Raspberry Pi outputs 3.3v per GPIO pin which flows to the LED and will light it up rather brightly, but just like Roy Batty from Blade Runner "The light that burns twice as bright, lasts half as long" so we need to drop the voltage down using a resistor. For this project I chose to use 82 ohm resistors which run in line from the GPIO pin to the LED, dropping the voltage before it reaches the LED.
For each of the LED that make up our dice, we need to complete the above connection, using a breadboard.
To connect each of the dice LED, use the above diagram as a guide, your wiring may vary depending on your breadboard.
The pins for each of the dice LED are as follows.
|Dice||Broadcom GPIO Numbering|
Configure the software
For this project we are using Python a robust and easy to learn language that is well suited to the Raspberry Pi. Python enables our project to use the GPIO pins which connect to the components on the game board and this is achieved via an external library, one of a few that will be used in this project.
A library is a collection of code that can be included inside of our project. Typically a library is one of the many thousands that have been written, tested and approved by the Python community, but you can easily make your own library for personal use. Using a library is achieved by importing the contents of the library into the application, and this happens as so.
Typically you will use libraries that are already installed with your version of Python, but in this project we will be using a library that is not installed as standard. Which is pygame.
Pygame is a library of functions that can be used to build games in Python. It includes functions to handle sprites, sounds, images and keyboard events. In this project we will use it to play sounds for the pythons and resistors.
In order to use this library we need to install it on our Raspberry Pi, and for that we need to install the Python package manager, called PIP. PIP will have the latest versions of the libraries but it does not come as standard on a fresh Raspbian install. To install PIP, open a new terminal by double clicking on the LXTerminal desktop icon, or navigating to the LXMenu > Accessories > LXTerminal.
In the terminal issue the following command and press enter
When prompted for a password type it in and press enter. It will take a few moments for pip to be installed on your system. When completed keep the terminal window open and enter the following command.
Pip will now install the pygame library for this project and once complete you are now free to close the terminal window. With all of the software dependencies met we can now focus on the Python code that controls our game.
When working in Python I like to use a set format for my code and that is as follows
Functions (if any)
Main body of the code
And for this project I have followed that format.
Importing libraries is a vital skill to learn. It enables code written by others to be easily used in our projects. Giving us the convenience of growing our code with out the need to build concepts such as random numbers, GPIO functionality and GUI interfaces.
In the code we firstly import the libraries that are needed, and in the code snippet below I have added comments to explain what each import achieves.
With these imports completed we now need to create the variables that will contain the position of the resistors and Pythons on the board and their GPIO pin reference, the sounds used in the game , the GPIO pins used for each of the LED for our dice and also the GPIO pin used for the push button that triggers our dice.
Next we configure both Pygame and the GPIO pins that are used in the game. Pygame requires us to initialise both the actual library and the mixer functions and it looks like this
The GPIO configuration needs us to set the board layout, we can use the physical pin layout, where pin 1 is at the top left of the board, next to the micro SD slot. This is an easy layout to use and is great for beginners. For this article we will use the official Broadcom pin mapping, which is not a logical numbering system but is easy to use and is my preferred choice. To do this code.
Using the variables for the resistors and pythons that we created earlier we will now configure each of the pins to be an input, by which I mean that they will receive current when the switch is triggered. In order to protect the Raspberry Pi rather delicate GPIO we will be using an internal pull down resistor which buffers the pins to prevent any accidental damage.
With the inputs configured, we now do the same to the pins that make our digital dice, of which they are all output devices that receive power from the GPIO pins to make them light up.
Lastly on the setup we tell the pin used for d2 to turn off, as by default it is active and sending power out to any devices attached, we do this by doing the following.
Next we create a function which will handle the playback of the audio files used in the game. Our function takes an argument, which is the filename of the audio file. Once triggered the function loads the audio file into memory, and then plays the audio once per trigger. While the audio is playing the function checks to see if the audio is still playing and if so it waits until complete, before triggering again if needed.
Next we have a function which handles the roll of our digital dice. Once triggered it uses the random int function from the random library to pick a number between 1 and 6. Then using a series of conditional logic the number rolled is compared to each number 1 to 6 and when a match is found the corresponding LED are lit up, so if I roll a 5, 5 of the LED will light up.
Now we move to the final part of our code, this is the main body that is a series of if and else if conditional statements that look to see if any squares have been triggered or that the dice has been rolled. Once the trigger occurs, either the dice roll or audio file playback function is called and output via the Raspberry Pi.
And now we reach the end of this project. I hope that you have enjoyed yourself and learnt a little more about what the new B+ is capable of with just a few easy and cheap to purchase components.
The full code for this project, along with the image file for the game board is available via Github.