Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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()