Pulse Width Modulation (PWM)
How a digital pin that can only be fully on or fully off still manages to dim an LED or control a motor's speed.
A microcontroller's output pin is strictly digital — fully HIGH or fully LOW, nothing in between. So how does the same pin dim an LED smoothly, rather than just switching it fully on or off? The answer is pulse width modulation (PWM): instead of holding the pin at a constant analog level, the microcontroller switches it on and off very rapidly — commonly hundreds to thousands of times per second — and varies how much of each cycle it spends HIGH versus LOW.
The proportion of time spent HIGH during each cycle is called the duty cycle, usually expressed as a percentage. A 100% duty cycle is indistinguishable from fully on; 0% is fully off; 50% spends exactly half of each cycle HIGH. Because the switching happens faster than an LED, a motor, or your eye can react to, the effect is perceived as a steady in-between brightness or speed rather than a rapid flicker.
PWM controls brightness or speed by controlling average power delivered, not by lowering the voltage itself — the pin is always either at full voltage or zero, just for different fractions of the time. This is why PWM is efficient: unlike a resistor that would waste power as heat to dim an LED, a PWM signal wastes almost none, since the pin is never sitting at a partial voltage.
Beyond dimming and speed control, PWM is also the basic building block used inside the buck converters from Lesson 25 — the switching duty cycle there is exactly this same technique, just applied to control an inductor instead of an LED.