FIGURE 24.4 The Pi Cobbler input circuit diagram.
Remember to keep the LED and resistor plugged into the GPIO 18 pin because you’ll use that in this
project as well.
Setting Up the Gertboard for Input
For the doorbells, you’ll use two of the three built-in push button switches on the Gertboard.
Try It Yourself: Set Up the Gertboard for Input
To set up the Gertboard for input, keep the B12 output buffer set to the B12 LED and
plugged into the GP18 pin that you used for the output test. Also make sure you have
the 3V3 jumper on the J7 block for power. Then follow these steps to set up the
switches for input:
- Connect a wire between GP24 in the J2 block and B2 in the J3 block.
- Connect a wire between GP25 in the J2 block and B1 in the 3 block.
That’s it! Your setup is complete, and you’re all set to start coding!
Working with Input Signals
On the surface, working with input signals is a breeze in the RPi.GPIO library. You just set up the
GPIO pin for input and then read the pin status using the input() method, like this:
Click here to view code image
pi@raspberrypi ~ $ sudo python3
Python 3.2.3 (default, Mar 1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(24, GPIO.IN)
>>> print(GPIO.input(24))
0
>>> print(GPIO.input(24))
1
>>> print(GPIO.input(24))
0
>>>
After you enter this code, try holding down the push button you connected to the GPIO 24 pin as you
run the print() methods. You should get different values of 0 or 1 , depending on whether the
button is pushed in.
However, there’s a hidden problem with this setup, and you may have already run into it with your
testing. Pushing the button connects the GPIO 24 pin to ground, forcing the LOW value (which is
displayed as a 0 ). However, when the button isn’t pressed, the GPIO 18 pin isn’t connected to
anything. That means the pin could be in either a HIGH or LOW state, and it may even switch back and
forth without your doing anything. This is called flapping.
To avoid flapping, you need to set the default value of the pin for when the button isn’t pressed. This
is called a pull-up (when you set the default to a HIGH signal) or pull-down (when you set the default