
C H A P T E R 1 4
T H E G P I O P O R T
231
As with the previous example, the next step is to create an infinite loop that constantly
checks the input pin to see if it’s been brought low (in other words, if it’s 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, you’ll need to
store it in a variable. The following instruction tells Python to create a new variable called
input_value
(as described in Chapter 12, “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 doesn’t do anything useful. To
make sure you know what’s going on, add the following
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 lines—the second
while
and the second
input_value
, an
embedded loop
—are
important. Even on the Raspberry Pi’s 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 it’s 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, you’ll
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.setmode(GPIO.BOARD)
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)
Summary of Contents for A
Page 1: ......
Page 2: ......
Page 3: ...Raspberry Pi User Guide 2nd Edition...
Page 4: ......
Page 5: ...Raspberry Pi User Guide 2nd Edition Eben Upton and Gareth Halfacree...
Page 10: ......
Page 26: ...R A S P B E R R Y P I U S E R G U I D E S E C O N D E D I T I O N 10...
Page 28: ......
Page 29: ...Chapter 1 Meet the Raspberry Pi...
Page 37: ...Chapter 2 Getting Started with the Raspberry Pi...
Page 56: ......
Page 57: ...Chapter 3 Linux System Administration...
Page 79: ...Chapter 4 Troubleshooting...
Page 89: ...Chapter 5 Network Configuration...
Page 109: ...Chapter 6 The Raspberry Pi Software Configuration Tool...
Page 122: ......
Page 123: ...Chapter 7 Advanced Raspberry Pi Configuration...
Page 140: ......
Page 141: ...Chapter 8 The Pi as a Home Theatre PC...
Page 151: ...Chapter 9 The Pi as a Productivity Machine...
Page 160: ......
Page 161: ...Chapter 10 The Pi as a Web Server...
Page 172: ......
Page 173: ...Chapter 11 An Introduction to Scratch...
Page 189: ...Chapter 12 An Introduction to Python...
Page 216: ......
Page 218: ......
Page 219: ...Chapter 13 Learning to Hack Hardware...
Page 234: ......
Page 235: ...Chapter 14 The GPIO Port...
Page 249: ...Chapter 15 The Raspberry Pi Camera Module...
Page 265: ...Chapter 16 Add on Boards...
Page 280: ......
Page 281: ...Appendix A Python Recipes...
Page 287: ...Appendix B Raspberry Pi Camera Module Quick Reference...
Page 293: ...Appendix C HDMI Display Modes...