Skip to content
Snippets Groups Projects
Commit fa282a98 authored by s84716's avatar s84716
Browse files

led switch

parent fe77e80d
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time, sys
# use pin numbers
GPIO.setmode(GPIO.BOARD)
# use pin 12 for output (=GPIO18)
GPIO.setup(12, GPIO.OUT)
ledOn = False
GPIO.output(12, ledOn)
# use pin 22 for input (=GPIO25)
GPIO.setup(22, GPIO.IN)
# callback function to switch LED output
def switch_led(pin):
global ledOn
ledOn = not ledOn # switch
# write to pin 12
GPIO.output(12, ledOn)
return
# detect the event of rising signal level at pin 22
GPIO.add_event_detect(22, GPIO.RISING, bouncetime=200)
# in case of an event switch LED output using
#the callback function switch_led(pin)
GPIO.add_event_callback(22, switch_led)
try:
while True:
time.sleep(1)
# interrupt using <Ctrl>+<c>
except KeyboardInterrupt:
# release used pins and return
GPIO.cleanup()
sys.exit()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment