Skip to content
Snippets Groups Projects
led_switch2.py 871 B
Newer Older
s84716's avatar
s84716 committed
#!/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()