Raspberry Pi – Push Button + LED

Read Time:20 Second

Console Screen

 

Breadboard GPIO Breadboard for LED and Push Button

import RPi.GPIO as GPIO
from time import sleep

PIN18 = 12
PIN25 = 22

GPIO.setmode(GPIO.BOARD)

GPIO.setup(PIN25, GPIO.IN)
GPIO.setup(PIN18, GPIO.OUT)

LEDON = False

while 1:
        try:
                if GPIO.input(PIN25):
                        if LEDON == True:
                                print "-- PIN25 ON"
                                LEDON = False
                                GPIO.output(PIN18, False)
                        else:
                                print "-- PIN25 OFF"
                                LEDON = True
                                GPIO.output(PIN18, True)
                sleep(1)
        except KeyboardInterrupt:
                GPIO.cleanup()

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.