
C H A P T E R 1 4
T H E G P I O P O R T
229
If you’ve already built the GPIO output example, you can either disconnect that from your Pi
or leave it connected—this example uses different pins, so both can co-exist quite happily. If
you do leave the previous example connected, make sure to use different rows on the bread-
board for the new components or you’ll find things don’t work quite as planned.
Build the circuit as follows, matching Figure 14-5:
1.
Insert the push-button switch into the breadboard. Most switches have either two or
four legs. You only need worry about two of the legs in the circuit. If the button has
four legs, they’ll be set up in in pairs: check the push-button’s data sheet to find out
which legs are paired together.
2.
Connect a 10 K
Ω
resistor to the same row as one of the push-button’s legs and an
unused row. This is a pull-up resistor, and will provide the Pi with a reference voltage
so it knows when the button has been pressed.
3.
Connect the unused leg of the pull-up resistor to Pin 1 of the Raspberry Pi’s GPIO port.
This provides the 3.3 V reference voltage.
4.
Connect the unused leg of the push-button switch to Pin 6 of the Raspberry Pi’s GPIO
port. This provides the ground connection.
5.
Finally, connect Pin 12 of the Raspberry Pi’s GPIO port to the other leg of the push-
button switch in the same row as the 10 K
Ω
resistor. Your breadboard should now
look like Figure 14-5.
The circuit you just built creates a situation whereby the input pin, which in this instance is
Pin 12 of the Raspberry Pi’s GPIO port, is constantly
high
thanks to the pull-up resistor con-
nected to a 3.3 V supply. When the push-button is pressed, the circuit is grounded and becomes
low
, providing the cue for your Python program to know that the button has been activated.
You may wonder why the resistor is required at all, and why the switch does not simply con-
nect Pin 12 to Pin 6 or Pin 1 directly. While this is possible, it creates what is known as a
floating
pin, which is a pin that doesn’t know whether it’s high or low. As a result, the circuit
will act as though the button is being pressed even when it isn’t, 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 develop-
ment 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
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...