This tutorial will help you to receive input to your Pi from physical switches and buttons and to debounce if necessary. It will show you how to automatically run a program when a button is pressed (a GO button for a robot, for instance), or use it as part of your program.
Circuit with current-limiting resistors in place. Diagram from eLinux.
When a GPIO pin is set as an input it is “floating” and has no defined voltage level. For us to be able to reliably detect whether the input is high or low we need to tie it so that it is always connected and either reads high or low.
To tie the pin we connect either a Pull Up or Pull Down resistor. A Pull down resistor connects the pin to ground through a large resistor, this means that when the switch is open there is a path to ground and so it will read low. When the switch is pressed (with the other side connected to 3.3V) there is a lower resistance path to high and so the pin will read high. The large (10kΩ) resistor ensures that only a little current is drawn when the switch is pressed.
Setting up a circuit like this means that we will be able to take reliable readings from a switch, however we could still damage the pins if they are accidentally set to an output. If we drive it low the output is connected directly to ground. Pushing the button will then create a short circuit between 3.3V and ground! To make this safer we put in a current limiting resistor (1kΩ will do) to make sure the Pi can handle the current drawn.
It is recommended to use one of our SD cards or images, if you are not you will need to install the RPi.GPIO library.
Now that we know what we need to do, we can get things wired up. First you should try to identify how your switch works, i.e. how the pins are connected. A simple switch may only have 2 pins and so be very easy to use, however if the one you wish to use is more complicated you can either look for a schematic (sometimes even on the switch itself) or test it with a multimeter. Look to see how the resistance changes between different pairs of pins when the switch is in its different positions.
With your switch connected, let’s now look at how to read its state from Python. Start Python (as root so you can access the GPIO pins) with
In your Python console:
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM)
This will import the necessary libraries in the GPIO namespace and set the pin numbering to correspond to your breakout board.
Now we need to set the pin as input, pin 17 is used in this example.
GPIO.setup(17,GPIO.IN)Reading the pin is now as easy as:
input = GPIO.input(17)
If we want to print “Button Pressed” each time a button is pressed (and assuming we’ve set up the switch so the pin goes high when pressed):
while True: if (GPIO.input(17)): print("Button Pressed")
But wait… if you tried that, you probably noticed it printed many times for just a single press. This may sometimes be what you want if you’re monitoring something which changes state continuously, but for a button we’re probably only interested in seeing each press as one event.
For more detail: Physical computing with Raspberry Pi