Hardik Rathod
Published © GPL3+

Push Button with Raspberry Pi

Quick Project to Interface Push Button with Raspberry Pi

BeginnerFull instructions provided30 minutes88,042
Push Button with Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi
×1
Push Button
×1
LED
×1
100 ohm Resistor
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper Wires
×1

Story

Read more

Schematics

Schematic

Code

button.py

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
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Button to GPIO23
GPIO.setup(24, GPIO.OUT)  #LED to GPIO24

try:
    while True:
         button_state = GPIO.input(23)
         if button_state == False:
             GPIO.output(24, True)
             print('Button Pressed...')
             time.sleep(0.2)
         else:
             GPIO.output(24, False)
except:
    GPIO.cleanup()

Credits

Hardik Rathod

Hardik Rathod

1 project • 33 followers

Comments