How to Fade LED Using Raspberry Pi with PWM Control
To fade an LED using a Raspberry Pi, connect the LED to a PWM-capable GPIO pin and use Python's
RPi.GPIO library to gradually change the PWM duty cycle. This smoothly adjusts the LED brightness by increasing and decreasing the power over time.Syntax
Use the RPi.GPIO library to set up PWM on a GPIO pin. The main parts are:
GPIO.setmode(GPIO.BCM): Sets pin numbering mode.GPIO.setup(pin, GPIO.OUT): Sets the pin as output.pwm = GPIO.PWM(pin, frequency): Creates a PWM instance on the pin with a frequency in Hz.pwm.start(duty_cycle): Starts PWM with initial brightness (0-100%).pwm.ChangeDutyCycle(value): Changes brightness smoothly.pwm.stop(): Stops PWM.GPIO.cleanup(): Resets GPIO pins.
python
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) pin = 18 GPIO.setup(pin, GPIO.OUT) pwm = GPIO.PWM(pin, 1000) # 1000 Hz frequency pwm.start(0) # Start with LED off try: for duty_cycle in range(0, 101, 5): pwm.ChangeDutyCycle(duty_cycle) # Increase brightness time.sleep(0.1) for duty_cycle in range(100, -1, -5): pwm.ChangeDutyCycle(duty_cycle) # Decrease brightness time.sleep(0.1) finally: pwm.stop() GPIO.cleanup()
Example
This example fades an LED connected to GPIO pin 18 smoothly from off to fully bright and back to off repeatedly. It uses PWM to change the LED brightness by adjusting the duty cycle in steps.
python
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) pin = 18 GPIO.setup(pin, GPIO.OUT) pwm = GPIO.PWM(pin, 500) # 500 Hz frequency pwm.start(0) # LED off initially try: while True: for brightness in range(0, 101, 2): pwm.ChangeDutyCycle(brightness) time.sleep(0.02) for brightness in range(100, -1, -2): pwm.ChangeDutyCycle(brightness) time.sleep(0.02) except KeyboardInterrupt: pass finally: pwm.stop() GPIO.cleanup()
Common Pitfalls
- Not using a resistor with the LED can damage the LED or Raspberry Pi.
- Using the wrong GPIO pin numbering mode (BCM vs BOARD) causes pin confusion.
- Forgetting to call
GPIO.cleanup()can leave pins in an unstable state. - Setting PWM frequency too low can cause visible flicker.
- Not handling exceptions can leave the LED stuck on.
python
import RPi.GPIO as GPIO import time # Wrong: No resistor and no cleanup GPIO.setmode(GPIO.BCM) pin = 18 GPIO.setup(pin, GPIO.OUT) pwm = GPIO.PWM(pin, 1000) pwm.start(50) time.sleep(2) # LED might stay on and no cleanup called # Right way: GPIO.cleanup() # Always clean up after use
Quick Reference
Here is a quick summary for fading an LED on Raspberry Pi:
| Step | Action |
|---|---|
| 1 | Connect LED with resistor to GPIO pin (e.g., GPIO18) |
| 2 | Import RPi.GPIO and set mode to BCM |
| 3 | Set pin as output |
| 4 | Create PWM instance with frequency (e.g., 500 Hz) |
| 5 | Start PWM with 0% duty cycle |
| 6 | Change duty cycle gradually to fade LED |
| 7 | Stop PWM and cleanup GPIO |
| Step | Action |
|---|---|
| 1 | Connect LED with resistor to GPIO pin (e.g., GPIO18) |
| 2 | Import RPi.GPIO and set mode to BCM |
| 3 | Set pin as output |
| 4 | Create PWM instance with frequency (e.g., 500 Hz) |
| 5 | Start PWM with 0% duty cycle |
| 6 | Change duty cycle gradually to fade LED |
| 7 | Stop PWM and cleanup GPIO |
Key Takeaways
Use PWM on a GPIO pin to control LED brightness smoothly.
Always include a resistor to protect the LED and Raspberry Pi.
Set GPIO mode to BCM and clean up pins after use.
Adjust PWM duty cycle gradually to create a fade effect.
Handle exceptions to ensure GPIO pins reset properly.