Cjam Reaction Tutorial
Cjam Reaction Tutorial
GPIO Zero
Reaction Timer
Tutorial by Andrew Oakley
Public Domain 1 Feb 2016
www.cotswoldjam.org
Introduction
This Python programming tutorial, shows you how simple it is to use an LED light and a
button, with the new GPIO Zero library.
Getting started
Conventions
Words you will see on the screen, or that you need to type in, are
highlighted like this
At the end of each line, you will usually have to press the Enter key.
Your tutor should have prepared your Raspberry Pi for you and given you a bag of
components. If not, see the section “Preparation” at the end.
The electronics
Components
An Light Emitting Diode (LED) is a light bulb with a short leg and a long
leg. If you feel around the rim, you'll also find a flat edge. The short leg
and flat edge always connect to negative (ground).
The resistor can be connected any way around. Lower value (lower Ohm)
resistors will allow more current through and will make the LED brighter;
higher values will let less current through and make it dimmer. We're
using a 270 Ohm resistor but anything between 220-470 Ohms will work
fine.
A switch can be connected any way around. When pressed, it allows
electricity to flow; when released, the electricity stops. This kind of push-
button is known as a “momentary press-to-make switch”.
You should also have a breadboard and some jumper wires. You should have three long
male-to-female wires (with a sticky-out bit at one end, and a sticky-in bit at the other end),
and one short male-to-male wire (with sticky-out bits at both ends).
The Raspberry Pi uses two sets of numbers to refer to the General Purpose Input Output
pins (GPIO pins).
"Board numbering" just starts from the bottom-left at 1, and works its way up and right,
through to 40.
From the menu, select Programming - Python 3. Then use File, New Window to create a
new program.
Type in the following program, or alternatively you can use File, Open to open the led.py
program in the python/reaction folder.
led=LED(23)
led.on()
sleep(2)
led.off()
Use File, Save to save this program as led.py and the run it by selecting Run menu, Run
Module.
You should see the LED light up for two seconds, and then turn off.
led=LED(23)
We then create an LED object. We say that the LED is connected to GPIO 23.
led.on()
sleep(2)
led.off()
Turn the LED on, wait for 2 seconds, then turn it off.
Detecting the button
The circuit
Now add the button and
connecting wires.
The program
In Python 3, use File, New Window to create a new program. Type in the following program,
or alternatively you can use File, Open to open the button.py program in the python/reaction
folder.
def press():
print ( "Button PRESSED!" )
def unpress():
print ( "Button unpressed!" )
button = Button(24)
button.when_released = unpress
button.when_pressed = press
pause()
Indentation matters in Python. For example, you must type the two spaces before the word
“print”. Indentations like these define a block of code– hence the word “def”.
Use File, Save to save this program as button.py and the run it by selecting Run menu, Run
Module.
Hold down the button. It should say "Button PRESSED!" in the Python 3 shell window.
Let go of the button. It should say "Button unpressed!".You can press and release the button
many times.
def unpress():
print ( "Button unpressed!" )
These are called functions. They define (def) something that should happen, and they give
that action a name. The code is indented to show it belongs to the function. The press()
function says "Button PRESSED!" and the unpress() function says "Button unpressed!".
button = Button(24)
button.when_released = unpress
button.when_pressed = press
Here we set up a button object and tell the computer that it is connected to GPIO 24.
We also tell the computer what to do, when the button is pressed or released.
pause()
This tells the computer to wait for events to happen, such as button presses.
Type in the following program, or alternatively you can use File, Open to open the led-
button.py program in the python/reaction folder.
def press():
print ( "Button PRESSED!" )
led.on()
def unpress():
print ( "Button unpressed!" )
led.off()
led=LED(23)
led.off()
button=Button(24)
button.when_released=unpress
button.when_pressed=press
pause()
Use File, Save to save this program as led-button.py and the run it by selecting Run menu,
Run Module.
Press the button and the LED should light up; release the button and it should turn off.
Reaction timer
In Python 3, use File, New Window to create a new program.
Type in the following program, or alternatively you can use File, Open to open the reaction.py
program in the python/reaction folder.
def press():
global timestarted,led,button
print ( "Reaction time: %.3f" % (time()-timestarted) )
led.off()
sleep(2)
reset()
def reset():
global timestarted,led
sleep(uniform(0.5,3))
led.blink(0.1,0.4,4,False)
led.on()
timestarted=time()
led=LED(23)
led.off()
button=Button(24)
button.when_pressed=press
reset()
pause()
Use File, Save to save this program as reaction.py and the run it by selecting Run menu, Run
Module.
The LED will flash four times, then on the fifth time it will stay on until you press the button.
Try to press the button as quickly as possible when it comes on for the fifth time.
You can have as many goes as you like - try challenging a friend to see who has the fastest
reactions. When you've had enough, halt the program by selecting Shell menu, Restart shell.
What is this program doing?
We've introduced several new concepts in the reaction program.
We're using a value which we call timestarted to record when the fifth flash starts. We
can then compare it to the time when the user presses the button, and that will tell us the
reaction time.
Values which are given a name, like timestarted , are called variables, because the
actual value they contain can vary.
Some variables are global. This means they can be used in any part of the program.
%.3f tells the computer to print a decimal fraction with 3 decimal places after the decimal
point.
The number we're going to print is the value of time now, minus the value of time when we
started the final flash. This is measured in seconds.
sleep(uniform(0.5,3))
The uniform command is used here to pick a random number between 0.5 and 3. It will
probably pick a decimal fraction rather than a whole number. The command is called
uniform because it you call it enough times, you will get uniform random distributon of
numbers - you'll eventually get every possible number in that range.
led.blink(0.1,0.4,4,False)
This command tells the LED to blink for 0.1 of a second, then wait for 0.4 of a second, then
repeat that 4 times.
The last value, False, tells it to wait until it has finished before moving on to the next
command. If you put True then it would start executing the next commands whilst carrying on
blinking! We don't want that here.
http://pythonhosted.org/gpiozero
Preparation
You will need the following electronic components for this tutorial:
http://www.cotswoldjam.org/downloads/2016-01/
You will need Raspbian Jessie with Python 3, IDLE and GPIO Zero. Once you have
Raspbian Jessie and an internet connection working, you can ensure all the other files are
installed by going to Menu, Accessories, Terminal and typing:
The dist-upgrade may take a while – maybe half an hour if your system hasn’t been
updated recently. Don’t worry if apt-get tells you that you already have packages installed.