DEV Community

Cover image for Raspberry Pi Pico - Fade LED
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Raspberry Pi Pico - Fade LED

This build fades an LED in and out on a Raspberry Pi Pico, using PWM instead of a plain on/off digitalWrite()-style signal. It is a nice first project for seeing how software can control an analog-ish quantity, brightness, through a digital pin, and it sets you up for dimming other things like motors or other LEDs later.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi Pico - Fade LED

What you need

Wiring

Raspberry Pi Pico - Fade LED wiring diagram

The LED has two legs: cathode (-) and anode (+). Connect the cathode to GND. Connect the anode, through a 220Ω resistor if you're not using the LED module, to a PWM-capable pin on the Pico, GP13 in the code below. If you use the LED module instead of a bare LED, wiring is simpler since the resistor is already built in.

How it works

An LED only really has two states, off when its anode is tied to GND and on when its anode gets power. PWM fakes everything in between by switching the pin on and off very fast, with a duty value from 0 to 255. A PWM value of 0 behaves like GND, 255 behaves like full power, and values in between make the LED look dimmer or brighter to your eye because it's flickering faster than you can see.

Code

The script sets up a PWM output on the LED pin at 1kHz using machine.PWM, then loops forever, nudging a brightness variable up or down by a fixed step and writing it out with duty_u16() after converting the 0-255 range to the Pico's 0-65535 PWM range. When brightness hits either end, 0 or 255, the step direction flips, which is what produces the fade-in, fade-out effect. A small sleep(0.03) between updates controls how fast the fade looks.

raspberry-pi-pico-fade-led.py

"""
This Raspberry Pi Pico MicroPython code was developed by newbiely.com
This Raspberry Pi Pico code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-fade-led
"""

from machine import Pin, PWM
from time import sleep

# Define pin
LED_PIN = 13  # The Raspberry Pi Pico pin GP13 connected to the LED

# Initialize PWM on the LED pin
led = PWM(Pin(LED_PIN))
led.freq(1000)  # Set the PWM frequency to 1kHz

# Variables to control the LED brightness
brightness = 0  # How bright the LED is
fade_step = 5  # How many points to fade the LED by

# Main loop
while True:
    # Set the brightness of the LED
    led.duty_u16(int(brightness * 65535 / 255))  # Convert 0-255 range to 0-65535

    # Change the brightness for next time through the loop
    brightness = brightness + fade_step

    # Reverse the direction of the fading at the ends of the fade
    if brightness <= 0 or brightness >= 255:
        fade_step = -fade_step

    # Wait for 30 milliseconds to see the dimming effect
    sleep(0.03)
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If nothing lights up, double check the cathode is on GND and the anode is on the pin you set in code, it's easy to swap the two legs. If the LED stays at one brightness or blinks instead of fading, make sure you're writing with duty_u16() and not just toggling the pin on or off. Also confirm Thonny is connected to the Pico with MicroPython installed, otherwise the script won't run at all.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-fade-led

Top comments (0)