Operation Manual

result, the circuit will act as though the button is being pressed even when it isnt, and may fail to detect the button being pressed
even when it is.
Open a new Python file, either in a text editor or using one of the Python integrated development environments (IDEs) available
on the Raspberry Pi. To begin, you will need to import the same GPIO library as in the previous GPIO output example:
import RPi.GPIO as GPIO
You dont need to import the time library, because this example doesnt need any timing instructions. Instead, you can get right
to setting up Pin 12 as an input. This is done in the same way as setting a pin as an output, with just the final part of the instruction
changed accordingly:
GPIO.setup(12, GPIO.IN)
If youre not using Pin 12 for this, make sure you change the pin number in the preceding instruction.
As with the previous example, the next step is to create an infinite loop that constantly checks the input pin to see if its been
brought low (in other words, if its been pressed). Begin the loop with the following code line:
while True:
Reading the status of an input pin is very similar to setting the status of an output pin, with one exception: before you can do
anything useful with the resulting value, youll need to store it in a variable. The following instruction tells Python to create a new
variable called input_value (as described in Chapter 11,An Introduction to Python) and set it to the current value of Pin 12:
input_value = GPIO.input(12)
Although the program could be executed now and work, it doesnt do anything useful. To make sure you know what’s going on,
add the following print instruction to get feedback:
if input_value == False:
print(The button has been pressed.)
while input_value == False:
input_value = GPIO.input(12)
The last two linesthe second while and the second input_value, an embedded loop—are important. Even on the
Raspberry Pis processor, which is relatively underpowered when compared to high-performance desktop and laptop
processors, Python runs very quickly. This embedded loop tells Python to keep checking the status of Pin 12 until its no longer
low, at which point it knows the button has been released. Without this loop, the program will loop while the button is being
pressed—and no matter how quick your reflexes, youll see the message printed to the screen multiple times, which is
misleading.
The final program should look like this:
import RPi.GPIO as GPIO
GPIO.setup(12, GPIO.IN)
while True:
input_value = GPIO.input(12)
if input_value == False:
print(The button has been pressed.)
while input_value == False:
input_value = GPIO.input(12)
Save the file as gpioinput.py, and then execute it from the terminal with sudo python gpioinput.py. At first, nothing will
happenbut if you press the push-button switch, the program will print the message from line six to the terminal (see Figure 12-
6). Release the button and press it again, and the message will be repeated.
As with the previous input example, this is a deceptively simple program that can be used for many purposes. In addition to being
able to read when a switch is pressed, the same code can be used to read when the pins of a separate devicesuch as a sensor
or external microcontrollerhave been pulled high or low.
Figure 12-6: The output of the gpioinput.py program