-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path04_pwmLed.py
More file actions
executable file
·36 lines (30 loc) · 904 Bytes
/
04_pwmLed.py
File metadata and controls
executable file
·36 lines (30 loc) · 904 Bytes
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
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
LedPin = 18
def setup():
global p
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by BCM
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
GPIO.output(LedPin, GPIO.LOW) # Set LedPin to low(0V)
p = GPIO.PWM(LedPin, 1000) # set Frequece to 1KHz
p.start(0) # Duty Cycle = 0
def loop():
while True:
for dc in range(0, 101, 4): # Increase duty cycle: 0~100
p.ChangeDutyCycle(dc) # Change duty cycle
time.sleep(0.05)
time.sleep(1)
for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
p.ChangeDutyCycle(dc)
time.sleep(0.05)
time.sleep(1)
def destroy():
p.stop()
GPIO.cleanup()
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()