Build a push button for Raspberry Pi

The following diagram shows how to construct a push button for a Raspberry Pi which is connected to a GPIO pin.


Use Python to detect for a push button being pressed. The following code will print Button Pressed when the button is pressed and Not Pressed when it is not pressed.

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

while True:
circuit = GPIO.input(4)
if circuit==False:
        print "Not Pressed"
elif circuit==True:
        print "Button Pressed"


Comments