From my days helping out at the fabulous Cotswold Pi Jam, here is a python project I wrote for one of the tutorial sessions which serves as a great introduction to incorporating physical buttons and switches into your programming projects.
We build a simple control panel with three buttons, learn how to incorporate it into our python code, then we use that knowledge to modify an open source version of space invaders to work with our new controller!
Tutorial by Stuart Fox
Thanks to Scott Bowman & Lee Robinson
Public Domain
In this tutorial, we'll build a mini game controller, wire it to a Raspberry Pi's GPIO pins and write a program in Python to recognise the button presses. Finally we’ll examine and run a Space Invaders game, modified with the methods we’ve just learnt about to work with our controller.
What we'll need...

In your bag you will find the following components:
- ×6 M-F Jumper leads (pin to socket)
- ×1 Mini Breadboard
- ×3 Momentary Push Buttons
Part 1 – Let’s build the controller





Take a button by its connectors as shown and carefully bend the button so it is at a 90-degree angle to the pins.
Do this slowly and gently to avoid snapping the connectors.
Place the breadboard in front of you as in the image to the left. Use the numbers and letters shown in the image as a reference.
Now take your first button and push its connectors into holes E1 and E3 with the button facing away from you as shown on the right.
Repeat this process, placing your second button's connectors into holes E5 and E7 and the third button's connectors into holes E15 and E17.
Part 2 – Wiring your controller
|  | First, make sure your Pi is shut down and the power lead disconnected. | 

The colour of the jumper wires in your kit is not important – where you put the jumper wires is important!
Make sure you connect to the correct holes and pins. Using the diagram above for reference, follow these steps to wire your controller:

| Breadboard | Pi GPIO | 
|---|---|
| A1 Wire to | Pin 29 (GPIO 5) | 
| A3 Wire to | Pin 39 (GND) | 
| A5 Wire to | Pin 31 (GPIO 6) | 
| A7 Wire to | Pin 25 (GND) | 
| A15 Wire to | Pin 33 (GPIO 13) | 
| A17 Wire to | Pin 34 (GND) | 
Part 3 – The Code
Now you have a controller built and wired to your Raspberry Pi's GPIO Pins, it’s time to see if it works. Make sure your wires have been checked by a tutor before you power up your Pi.
Power up your Raspberry Pi. From the desktop menu, select Programming – Thonny Python IDE.
Let’s run the program. Use the Run button on the top of the window.
Now try pushing each of your buttons. If everything is working the program will show the text correponding to each button when it's pressed.
Type in the following code
from gpiozero import Button
from signal import pause
button_1=Button(5)
button_2=Button(6)
button_3=Button(13)
def buttonone ():
  print("Button 1")
def buttontwo ():
  print("Button 2")
def buttonthree ():
  print("Button 3")
  
while (True):
  button_1.when_pressed = buttonone
  button_2.when_pressed = buttontwo
  button_3.when_pressed = buttonthree
  pause ()
Now use File, Save As to save your program as the name of your choice (don’t forget to put .py on the end) in the ~/python/buttoninvaders folder.
This program shows us how to use the GPIOZero library to trigger functions in Python and will also serve to test your newly built controller.
Let's look at how this program works…
from gpiozero import Button from signal import pause
The from lines tell the computer to learn about new things. Computers can learn from programs that other people have written; we call these other programs "libraries". Our program needs the function called button from the gpiozero library which it will use to detect button presses on your controller. The function pause from the signal library so that we can insert pauses.
button_1=Button(5) button_2=Button(6) button_3=Button(13)
These lines set the names of our buttons and tell the program which GPIO pin they are connected to. So now the program will see our three buttons connected to these three GPIO pins: pin 5 (I2C1 SCL), pin 6 (GND) and pin 7 (GPIO 4)
def buttonone ():
  print("Button 1")
def buttontwo ():
  print("Button 2")
def buttonthree ():
  print("Button 3")
Here we are setting up functions called buttonone, buttontwo and buttonthree. Functions contain multiple instructions, ready to be triggered by an event or input. In this case, each function only performs one task, and that is to print some text on the screen. When we look at the Space Invaders game code, we will see functions like this performing multiple tasks at once when triggered.
while True:
This while True: tells the program to run forever in a loop.
button_1.when_pressed = buttonone button_2.when_pressed = buttontwo button_3.when_pressed = buttonthree pause ()
Now we are telling the program to trigger a function when a button is pressed.
Part 4 – Let's Play Space Invaders

Now our control panel is built and tested, it's time to defend earth against an alien attack! In Thonny Python IDE, Use File, Open to open buttoninvaders.py program in the ~/python/buttoninvaders folder.
Now enable line numbers: from the Tools menu select options, choose the editor tab, then tick the box by 'Show Line Numbers'.
Look at the following lines of code and see if you recognise the way this game has been modified to work with our controller.
Lines 12 to 20, lines 55 to 63, and lines 451 to 484. These lines are commented to explain the modifications.
Now, click the Run button and our modified Space Invaders game will launch.
- Button one is left
- Button two is right
- Button three is fire
You can press fire or the space bar to start the game.
