This Instructable shows you how to connect switch circuits to your Raspberry Pi's GPIO pins and how to create a script that will respond to a switch being pressed.
These script below will enable you to switch an LED on by pressing a button and then switch the LED off by pressing another button. Our previous instructable Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins demonstrates how to switch LEDs on and off by using the GPIO.output command. This Instructable introduces you to the GPIO.input command and shows you how it can be used to add input to your Raspberry Pi.
It will also introduce you to using Loops in Python.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: What You Will Need
- A RaspberryPi with Raspbian already installed. You will also need to be able to access the Pi using a Monitor, Mouse and Keyboard or via Remote Desktop. You can use any model of Raspberry Pi. If you have one of the Pi Zero models, you may want to solder some header pins to the GPIO port.
- Some Male to Female jumper wires
Step 2: Build Your Circuit
Build the above circuit on your breadboard making sure that none of the components leads are touching and that the LED is connected the correct way round.
If you look at the LED closely, you will see that it has two small pieces of metal inside the coloured casing. These are called the Anode and Cathode. The Cathode is the largest of the two and is also connected to the LED's negative lead.
Ensure that the switches are connected the correct way round. If you look at a switch closely you will notice that two legs are mounted either side of the switch. When the switch is in the open position (i.e. not pressed) the circuit between each side will be broken. When the switch is in the closed position (i.e. pressed) the circuit between the two sides will be completed.
Once you have checked your circuit, connect the jumper cables your Raspberry Pi's GPIO pins by following the above diagram.
Step 3: Create a Python Script to Respond to Button Presses
Open IDLE on your Raspberry Pi (Menu > Programming > Python 2 (IDLE)).
Now open a new project (File > New File) and type the following:
import RPi.GPIO as GPIO
SwitchA=17 SwitchB=18 SwitchC=27 LED=22
GPIO.setmode(GPIO.BCM) GPIO.setup(SwitchA, GPIO.IN) GPIO.setup(SwitchB, GPIO.IN) GPIO.setup(SwitchC, GPIO.IN) GPIO.setup(LED, GPIO.OUT)
while True: if GPIO.input(SwitchA)==True:
print("You are pressing button A") if GPIO.input(SwitchB)==True: print("You are pressing button B") GPIO.output(LED, True) if GPIO.input(SwitchC)==True: print("You are pressing button C") GPIO.output(LED, False)
Save your project as buttons.py (File > Save As) in your Raspberry Pi's Documents folder.
On your Raspberry Pi open Terminal (Menu > Accessories > Terminal) and navigate to your Documents folder by typing the following:
You can now run your new script by typing the following:
When you press the first button, the message "You are pressing button A" will be repeated on your screen until you stop pressing the button.
When you press the second button, a similar message will appear on your screen and the LED will also light up.
When you press the third button, a similar message will appear and the LED will switch back off.
You can stop this program from running by pressing CTRL+Z.
How the Script Works
Lines 11-13 set up the GPIO pins as input pins by using the GPIO.setup command:
The script then uses the GPIO.input command to detect whether the pin is True (i.e. when a +3.3v supply is connected) or False (i.e. when the pin is connected to Ground):
To ensure that the Raspberry Pi is constantly checking the state of the pins, the script also uses a While loop to repeatedly run the same section of script:
The while command is normally followed by a condition (e.g. x==1) and a colon (":"). Any indented code that follows this command will be repeated until the condition is no longer met. However, in the above script we have simply written True instead of a condition. This will ensure that the scrip will run continuously until either CTRL+Z is pressed or the Raspberry Pi is powered down.
By changing line 17 to include the following conditional statement, the script will stop looping when the third button is pressed: