Pushbuttons or switches connect two points in a circuit when you press them.
Interfacing Push button with Raspberry Pi is very simple. In this project i will show you how to interface push button switch with Raspberry Pi 3 and also connect one LED so that when button is pressed, LED will turn ON and on button release it will turns OFF.
- Raspberry Pi
- Momentary button or Switch
- 220 Ohm Resistor
- hook-up wires
The Button is wired in such a way that when it is pressed, it will connect GPIO 26 to the Ground(GND).
GPIO 26 is normally pulled up to 3.3V. So that when you read the input value, it will return False if the button is pressed.
Code - Blocks
After running the code, when you push the Button LED should turn ON. If not then check you code and connections and try again.
Code - Python
Create new python file button.py and enter following code. To run the code, open the terminal and go to directory where you code is located and enter the command " sudo python button.py " and hit enter.
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(19, GPIO.OUT) GPIO.setup(26, GPIO.IN) while True: if GPIO.input(26): GPIO.output(19,True) else: GPIO.output(19,False)