You want to connect a switch to your Raspberry Pi so that when you press it, some Python code is run.
Connect a switch to a GPIO pin and use the RPi.GPIO library in your Python program to detect the button press.
To make this recipe, you will need:
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• Tactile push switch (see “Miscellaneous” on page 382)
Figure 11-1 shows how to connect a tactile push switch, using a breadboard and jumper wires.
Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called switch.py.
This example code displays a message when the button is pressed:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) while True: input_state = GPIO.input(18) if input_state == False: print(‘Button Pressed’) time.sleep(0.2) |
You will need to run the program as superuser:
pi@raspberrypi ~ $ sudo python switch.py Button Pressed Button Pressed Button Pressed Button Pressed |
Discussion
You will notice that the switch is wired so that when it is pressed, it will connect pin 18 configured as an input to GND. The input pin is normally pulled up to 3.3V by the optional argument pull_up_down=GPIO.PUD_UP in GPIO.setup. This means that when you read the input value using GPIO.input, False will be returned if the button is pressed. This is a little counterintuitive.
Each GPIO pin has software configurable pull-up and pull-down resistors. When using a GPIO pin as an input, you can configure these resistors so that one or either or neither of the resistors is enabled, using the optional pull_up_down parameter to GPIO.setup. If this parameter is omitted, then neither resistor will be enabled. This leaves the input floating, which means that its value cannot be relied upon and it will drift between high and low depending on what it picks up in the way of electrical noise.
If it is set to GPIO.PUD_UP, the pull-up resistor is enabled; if it is set to GPIO.PUD_DOWN, the pull-down resistor is enabled.
You might expect the push switch to have just two connections, which are either open or closed. While some of these tactile push switches do have just two connections, most have four. Figure 11-2 shows how these connections are arranged.
Actually, there are only really two electrical connections, because inside the switch package pins B and C are connected together, as are A and D.
See Also
For more information on using a breadboard and jumper wires with the Raspberry Pi, see Recipe 8.10.
To use a switch to trigger an interrupt, see Recipe 9.12.
To debounce a switch, see Recipe 11.5.
To use external pull-up or pull-down resistors, see Recipe 11.6.
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-9. Miscellaneous | |
1200mAh LiPo battery | Adafruit: 258 |
5V relay | SparkFun: COM-00100 |
5V panel meter | SparkFun: TOL-10285 |
Servo motor | SparkFun: ROB-09065, Adafruit: 1449 |
5V 1A power supply | Adafruit: 276 |
Low power 6V DC motor | Adafruit: 711 |
0.1 inch header pins | SparkFun: PRT-00116, Adafruit: 392 |
5 V 5-pin unipolar stepper motor | Adafruit: 858 |
12 V, 4-pin bipolar stepper motor | Adafruit: 324 |
Magician chassis with gearmotors | SparkFun: ROB-10825 |
Tactile push switch | SparkFun: COM-00097, Adafruit: 504 |
Miniature slide switch | SparkFun: COM-09609, Adafruit: 805 |
Rotary encoder | Adafruit: 377 |
4×3 keypad | SparkFun: COM-08653 |
Piezo buzzer | SparkFun: COM-07950, Adafruit: 160 |