PI-14Raspberry Pi PicoBeginner

Raspberry Pi Pico PWM LED Fade (Breathing Effect)

The LED doesn't just blink anymore, it breathes! With the Raspberry Pi Pico's PWM (Pulse Width Modulation), the LED's brightness slowly rises and then slowly fades, a calm "breathing" effect like a sleeping laptop's light. You'll learn the machine.PWM class, the 16-bit brightness value from 0–65535 with duty_u16() and the PWM frequency.

0.0(0)
4 completed
Raspberry Pi Pico PWM LED Fade (Breathing Effect)
An LED that breathes calmly, like the light on a device in standby. The Raspberry Pi Pico slowly turns the LED's brightness up, then slowly back down, in a loop that never ends. You'll see with your own eyes that a pin can do more than just on or off: it can glow at every brightness in between.

Video

Circuit Diagram

Raspberry Pi Pico PWM LED Fade (Breathing Effect) - Circuit Diagram

Source Code

MicroPython
1# ─────────────────────────────────────────
2#   BlueGrays · bluegrays.com
3#   Raspberry Pi Pico PWM LED Fade (Breathing Effect, GP14)
4# ─────────────────────────────────────────
5# With PWM the LED brightness rises and falls smoothly (breathing effect).
6# STEP sets the brightness change per step and STEP_DELAY_MS the wait
7# between steps; one breath takes about 1.3 s.
8#
9# Pins:
10#   GP14 -> Green LED (220 ohm resistor, cathode to GND)
11
12from machine import Pin, PWM
13from time import sleep_ms
14
15# ============ PWM SETUP ============
16led = PWM(Pin(14))
17led.freq(1000)          # no visible flicker
18
19STEP          = 512     # Brightness step (0-65535)
20STEP_DELAY_MS = 5       # Delay per step (ms)
21
22# ============ MAIN LOOP ============
23while True:
24    # Fade in (0 -> 65535)
25    for duty in range(0, 65536, STEP):
26        led.duty_u16(duty)
27        sleep_ms(STEP_DELAY_MS)
28
29    # Fade out (65535 -> 0)
30    for duty in range(65535, -1, -STEP):
31        led.duty_u16(duty)
32        sleep_ms(STEP_DELAY_MS)
33

FAQ

What values does duty_u16() take?

It takes a 16-bit value from 0 to 65535: 0 is fully off, 65535 is full brightness and 32768 is about half brightness.

Why is the PWM frequency 1000 Hz?

An LED switching on and off 1000 times a second looks like steady light to the eye, with no flicker. At very low frequencies (e.g. 50 Hz) the flicker can be noticeable.

How can the breathing effect look more natural?

Increasing the brightness quadratically or exponentially instead of linearly matches how the eye perceives light. Adding a short pause at the dimmest point also feels more like a real breathing rhythm.

Can every Pico pin output PWM?

Yes, all GPIO pins on the Pico can generate PWM. The Pico has 8 PWM slices (16 channels); two pins that share the same slice must use the same frequency.