How to Fade LED Raspberry Pi

 1. Introduction

So far, we have only turned LEDs fully ON or fully OFF. This is called Digital control. Today, we will learn how to vary the brightness of an LED, creating a "fading" or "breathing" effect. To do this, we use a technique called PWM (Pulse Width Modulation). This allows the Raspberry Pi to simulate an analog voltage using a digital pin.

2. Components

  • Raspberry Pi (Any model)

  • Breadboard

  • LED (Any color)

  • 220 ohms Resistor

  • Jumper Wires (M-F)

3. Circuit and connections

Raspbery Pi
Raspberry Pi Pinout Diagram

The connections are exactly the same as a standard LED blink project.

  • Anode (+): Connect the long leg of the LED to GPIO 17 (Pin 11).

  • Cathode (-): Connect the short leg through a 220 ohms resistor to GND (Pin 9).

4. Detailed Step By Step Circuit working

  1. The Raspberry Pi cannot output a true analog voltage (like 1.5V). It can only output 0V or 3.3V.

  2. PWM works by switching the LED ON and OFF very rapidly (hundreds of times per second).

  3. If the LED is ON for 50% of the time and OFF for 50%, our eyes perceive it as 50% brightness.

  4. By gradually increasing the "ON" time (Duty Cycle), the LED appears to fade in smoothly.

5. Libraries to be included

In GPIO Zero, we use the PWMLED class instead of the standard LED class.

  • from gpiozero import PWMLED

  • from time import sleep

6. Code

Python
from gpiozero import PWMLED
from time import sleep

# Define PWM LED on GPIO 17
led = PWMLED(17)

print("LED Fading Started... Press Ctrl+C to stop")

try:
    while True:
        # Fade In
        for brightness in range(0, 101):
            led.value = brightness / 100.0
            sleep(0.02)
            
        # Fade Out
        for brightness in range(100, -1, -1):
            led.value = brightness / 100.0
            sleep(0.02)

except KeyboardInterrupt:
    led.off()
    print("\nProgram Stopped")

7. Detailed Step By Step Code working

  1. PWMLED(17): This tells the Pi to enable PWM hardware on GPIO 17.

  2. led.value: This accepts a number between 0.0 (OFF) and 1.0 (Full Brightness).

  3. For Loop: We use a loop to count from 0 to 100. By dividing the count by 100 (brightness / 100.0), we get the decimal values (0.1, 0.2, etc.) that the Pi needs.

  4. sleep(0.02): This controls the speed of the fade. A smaller number makes the fade faster.

8. Tips

  • Pin Selection: While most GPIO pins support software PWM, GPIO 18 is a hardware PWM pin on the Pi and is often more stable for complex projects.

  • Math Matter: Remember that Python division should result in a float. Using 100.0 ensures the result is a decimal like 0.5 instead of just 0.

9. Uses

  • Mood Lighting: Creating soft transition lights for home decor.

  • Status Indicators: A "breathing" LED often indicates a system is in "Sleep Mode" or "Processing."

  • Motor Control: The same PWM logic is used to control the speed of DC motors.

10. Conclusion

Mastering PWM is a huge milestone! You have moved from simple switches to controlling intensity. This logic is the foundation for controlling fan speeds, screen brightness, and even the colors of an RGB LED. 

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.