Wednesday, September 10, 2014

Raspberry Pi Python exercise: Change GPIO LED brightness with GPIO.PWM

Example of Python on Raspberry Pi, to change GPIO LED brightness with GPIO.PWM.



led.py
import RPi.GPIO as GPIO
import time

led_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)

#frequency 500 Hz
led_pwm = GPIO.PWM(led_pin, 500)
#duty cycle = 100
led_pwm.start(100)

while(True):
    led_pwm.ChangeDutyCycle(100)
    time.sleep(1)
    led_pwm.ChangeDutyCycle(80)
    time.sleep(1)
    led_pwm.ChangeDutyCycle(60)
    time.sleep(1)
    led_pwm.ChangeDutyCycle(40)
    time.sleep(1)
    led_pwm.ChangeDutyCycle(20)
    time.sleep(1)
    led_pwm.ChangeDutyCycle(0)
    time.sleep(1)


Connection, check lst post of "Toggle LED on GPIO".

No comments: