User manual
55
Fig. 6.3: An LED at GPIO port 18.
The programme leddimmen01.py dims the LED using cycles of light and dark phases and utilizes the PWM
function from the GPIO library. The PWM signal is generated as a separate thread. This allows the use of a
dimmed LED (almost) like any normally lit LED in a programmr.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM); LED = 18
GPIO.setup(i, GPIO.OUT, initial=0)
print (“Ctrl+C exits the program")
p = GPIO.PWM(LED, 50); p.start(0)
try:
while True:
for c in range(0, 101, 2):
p.ChangeDutyCycle(c); time.sleep(0.1)
for c in range(100, -1, -2):
p.ChangeDutyCycle(c); time.sleep(0.1)
except KeyboardInterrupt:
p.stop(); GPIO.cleanup()