PWM LED Dimming on the Pico
In your very first Pico programs you turned an LED fully ON or fully OFF. That is like a light switch with only two positions. But a real dimmer lets you set the light to 10%, 50%, 90% — any brightness you like. How can a chip that only understands the numbers 0 and 1 produce a half-bright LED?
The answer is a beautiful trick called PWM — Pulse Width Modulation. The pin still only ever outputs 0 or 1, but it switches between them thousands of times per second. By controlling how much of the time the pin is ON, you control how bright the LED looks. In this lesson you will build a dimmer, make an LED smoothly fade up and down, and create the famous "breathing" LED effect.
What you will learn
- Why a plain digital pin cannot give you a "half" voltage on its own.
- The core PWM idea: fast on/off switching and the duty cycle.
- How to use MicroPython's
PWMclass:freq()andduty_u16(). - What "16-bit duty" means, with real worked numbers (why 50% = 32768).
- How to fade an LED smoothly with loops, and build a breathing effect.
- Why brightness looks non-linear (the gamma / perception idea).
- How to choose a PWM frequency and avoid visible flicker.
- That the very same PWM trick also drives servos, motors and buzzers.
1. Why a digital pin can't make a "half" voltage
Every general-purpose pin (GPIO) on the Pico is a digital pin. "Digital" means it has exactly two output states:
- HIGH — the pin is connected to roughly 3.3 volts (this is a logic "1").
- LOW — the pin is connected to 0 volts, i.e. ground (this is a logic "0").