User manual
59
GPIO.setmode(GPIO.BCM); LED = [18,25]
GPIO.setup(LED[0], GPIO.OUT); GPIO.setup(LED[1], GPIO.OUT)
print (“Ctrl+C exits the program")
p = GPIO.PWM(LED[0], 50); q = GPIO.PWM(LED[1], 50)
p.start(0)
p.start(0)
try:
while True:
for c in range(0, 101, 2):
p.ChangeDutyCycle(c); q.ChangeDutyCycle(c)
time.sleep(0.1)
q.ChangeFrequency(10)
for c in range(0, 101, 2):
p.ChangeDutyCycle(100-c); q.ChangeDutyCycle(c)
time.sleep(0.1)
q.ChangeFrequency(50)
except KeyboardInterrupt:
p.stop(); GPIO.cleanup()
6.1.3 How does it work?
The basic structure of the program corresponds to the previous experiment with a few small extensions.
LED = [18,25]; GPIO.setup(LED[0], GPIO.OUT); GPIO.setup(LED[1], GPIO.OUT)
Instead of a variable for the GPIO port we will define a list of two variables is now defined, and by that two
GPIO ports,
18 and 25, are initialized as the outputs for LEDs.
p = GPIO.PWM(LED[0], 50); q = GPIO.PWM(LED[1], 50); p.start(0); q.start(0)
Then the two objects p and q are created which generate the PWM signals for the two LEDs, each with 50
Hertz.
for c in range(0, 101, 2):
p.ChangeDutyCycle(c); q.ChangeDutyCycle(c)
time.sleep(0.1)
The duty cycles of both PWM objects are simultaneously increased step by step in the first loop. The two
LEDs have the same behavior in this phase.
q.Change Frequency(10) At the end of this loop, when both LEDs have achieved full brightness, the
frequency of the PWM signal of the second LED is reduced to 10 Hertz by the method
Change Frequency().
This frequency is still detected by the human eye as blinking.
for c in range(0, 101, 2):
p.ChangeDutyCycle(100-c); q.ChangeDutyCycle(c)
time.sleep(0.1)