-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.Blinking_LED.py
34 lines (34 loc) · 1.04 KB
/
1.Blinking_LED.py
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
#!/usr/bin/env python3
import RPi.GPIO as GPIO #the pin libaray of raspberry pi4
import time #the time libaray of system time
LedPin = 17 #the BCM pin connect to LED
def setup():
# Set the GPIO modes to BCM Numbering
GPIO.setmode(GPIO.BCM)
# Set LedPin's mode to output,and initial level to High(3.3v)
GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
# Define a main function for main process
def main():
while True:
print ('...LED ON')
# Turn on LED
GPIO.output(LedPin, GPIO.LOW)
time.sleep(1)
print ('LED OFF...')
# Turn off LED
GPIO.output(LedPin, GPIO.HIGH)
time.sleep(1)
# Define a destroy function for clean up everything after the script finished
def destroy():
# Turn off LED
GPIO.output(LedPin, GPIO.HIGH)
# Release resource
GPIO.cleanup()
# If run this script directly, do:
if __name__ == '__main__':
setup()
try:
main()
# When 'Ctrl+C' is pressed, the program destroy() will be executed.
except KeyboardInterrupt:
destroy()