import RPi.GPIO as GPIO #external module
import time
ledPin = 23 # Broadcom pin number
buttonPin = 20
GPIO.setmode(GPIO.BCM) # pin numbering scheme
GPIO.setwarnings(False)
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.output(ledPin, GPIO.LOW) # initial state
print("Ctrl +C to exit")
try:
while 1:
buttonValue = GPIO.input(buttonPin)
if buttonValue == False : # when button pressed buttonrValue == 0, LED ON
GPIO.output(ledPin, GPIO.HIGH)
print("Button pressed /LED ON")
time.sleep(0.5)
else:
GPIO.output(ledPin, GPIO.LOW) # when button not pressed LED OFF
except KeyboardInterrupt:
GPIO.cleanup()
Ещё видео!