Built-in project · Raspberry Pi Pico W

Pico W fading LED with PWM on GP15

MicroPython on a Pico W: machine.PWM at 1 kHz on GP15 raises the duty cycle step by step, so the LED brightens slowly, then goes dark again. The whole build, a Raspberry Pi Pico W and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $6.15. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • PWM brightness control in MicroPython
  • Duty cycle as a 16-bit number
  • Why a resistor goes in series with an LED
  • Stepping a value in a loop with a delay
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 3

How it works

This is MicroPython on a Pico W. GP15 is set up as a PWM output at 1 kHz, which means it switches on and off a thousand times a second. The share of each cycle it is on, the duty cycle, sets how bright the LED looks.

The program raises the duty from 0 up to nearly full in steps of 1024 out of 65535, waiting 20 ms between steps, so the LED brightens slowly. Then it drops to 0, prints faded up, waits half a second and starts again.

Parts list

3 parts, plus the jumper wires. They retail for about $6.15; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω pin 2Raspberry Pi Pico W pin GP15
LED, red pin AResistor, 220 Ω pin 1
LED, red pin CGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Fading LED: PWM on GP15 at 1 kHz. The duty cycle climbs from 0 to
# full in small steps so the LED brightens, then the LED goes dark and
# it starts again.
from machine import Pin, PWM
import time

led = PWM(Pin(15))
led.freq(1000)

while True:
    for duty in range(0, 65536, 1024):
        led.duty_u16(duty)
        time.sleep_ms(20)
    led.duty_u16(0)
    print("faded up")
    time.sleep_ms(500)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED does not light This project
    LEDs only work one way: the anode goes to the resistor from GP15, the cathode to ground.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.