Built-in project · Raspberry Pi Pico

MicroPython breathing LED on a Pico, PWM on GP15

MicroPython on a Raspberry Pi Pico: machine.PWM on GP15 sweeps the duty cycle up and down, so the LED fades in and out like breathing. The whole build, a Raspberry Pi Pico 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 $4.15. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • PWM and duty cycle
  • Dimming an LED with machine.PWM
  • Sweeping a value up and down with range
  • The 16 bit duty range, 0 to 65535
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico
  • Parts: 3

How it works

PWM switches a pin on and off very fast. Here it runs at 10 kHz on GP15. The share of time the pin is on is the duty, and a higher duty makes the LED look brighter.

The loop raises the duty from 0 to 65535 in steps of 1024, waiting 10 milliseconds per step. Then it steps back down. The LED fades in and out, and the script prints breath after each cycle.

Parts list

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

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 1Raspberry Pi Pico pin GP15
LED, red pin AResistor, 220 Ω pin 2
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
# A breathing LED: PWM on GP15 at 10 kHz, the duty cycle swept up from
# 0 to 65535 and back down, so the LED fades in and out.
from machine import Pin, PWM
import time

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

while True:
    for duty in range(0, 65536, 1024):
        led.duty_u16(duty)
        time.sleep_ms(10)
    for duty in range(65535, -1, -1024):
        led.duty_u16(duty)
        time.sleep_ms(10)
    print("breath")

Troubleshooting

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

  • The LED does not fade This project
    Check the wiring from GP15 through the 220 ohm resistor to the LED anode, with the cathode on ground.
  • The fade is too fast This project
    Raise the time.sleep_ms(10) value, or lower the step size of 1024.
  • 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.
  • The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
    Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it.
  • A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
    Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.

Change it and keep it

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