Operation Manual
with one or more inputs. In the following example, you’ll see how to connect a push-button switch to another pin on the GPIO
port and read its status in Python.
As with the earlier LED output example, this input example makes use of the Python GPIO library. Assuming that you have this
library installed, you can begin to build the circuit. (If you haven’t installed the Python GPIO library yet, skip back a few pages
and follow the installation instructions.)
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 breadboard for the new components or you’ll find things don’t work quite as planned.
Build the circuit as follows:
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 12-5.
Figure 12-5: The example breadboard layout for a simple push-button input
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 connected 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 connect 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