Hardware Reference
In-Depth Information
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
Search WWH ::




Custom Search