Interfacing Push button with Raspberry Pi is very simple. In this project i will show you how to interface 4 pin push button switch with Raspberry Pi 2 and also connect one LED so that when button is pressed, LED will turn ON and on button release it will turns OFF.
Internal connections of pins:
There is direct connection between A and B, whether button is pressed of not. And same for C and D.
When button is pressed, A and C are getting shorted (actually all pins are getting shorted) and same way B and D are getting shorted.
So to use this push button as a switch, use either A-C pair or B-D pair or A-D or B-C.
The Button is wired in such a way that when it is pressed, it will connect GPIO 23 to the Ground(GND).
In the code (check below in code section),
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
means GPIO 23 is normally pulled up to 3.3V. So that when you read the input value using
, it will return False if the button is pressed.
Each GPIO pin in Raspberry Pi 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
If it is set to
, the pull-up resistor is enabled; if it is set to
, the pull-down resistor is enabled.
After running the code, when you push the Button LED should turn ON and in terminal window you will see the text "Button Pressed...". If not then check you code and connections and try again.
Summery of the project: