How PWM dims a light by switching it on and off 20,000 times a second

While I was working on making a touchscreen darker at night, I needed a way to dim a backlight that only has an on/off line. The answer was PWM, pulse-width modulation. It turns out to be one of the most common tricks in electronics, so I wanted to understand how it works.
On and off, very fast
PWM controls "how much" of something you get by switching it fully on and fully off very fast, and changing how long each "on" part lasts.
The signal repeats a fixed cycle many times a second. At 20,000 times a second, each cycle lasts 50 millionths of a second. Inside each cycle the output is on for part of the time and off for the rest. The share that is on is called the duty cycle. A 25 % duty cycle means on for a quarter of each cycle and off for the other three quarters.
Whatever the signal drives, such as a light, a motor or a heater, responds to the average, because the switching is too fast for it to follow. Your eye cannot follow it either, so a light at 10 % duty cycle looks about 10 % as bright.

Why it is everywhere
A switch that is fully on or fully off wastes almost no power. The old way to dim a light was to put something in the way that turned the extra power into heat, a bit like a tap half closed. PWM gets the same "half" by opening the tap all the way, half of the time.
It is also easy for a chip to make. It only needs a timer and one pin. A small microcontroller like the RP2040 on a Raspberry Pi Pico has hardware that does it on its own, so the program only sets the duty cycle and moves on.

Raspberry Pi Pico. Photo by Phiarc, CC BY-SA 4.0, via Wikimedia Commons.
In MicroPython on a Pico, the whole thing is a few lines:
from machine import Pin, PWM
pwm = PWM(Pin(15))
pwm.freq(20000) # 20,000 cycles a second
pwm.duty_u16(65535 // 10) # on 10 % of each cycle
Where you already use it
- Screens and LED lights. Most laptop, phone and monitor backlights dim this way, and so do a lot of LED bulbs and car lights.
- PC fans. A 4-pin fan gets a PWM signal at 25,000 times a second that sets its speed.
- Motors and hobby servos. Drones, power tools and electric cars use it for speed. A hobby servo uses the pulse width for something else: 50 times a second it gets a pulse between 1 and 2 thousandths of a second long, and the length sets the position.
- Power supplies. Phone chargers and the converters inside a computer switch on and off hundreds of thousands of times a second to turn one voltage into another.
- Audio. The "Class D" amplifiers in small speakers and soundbars rebuild the sound wave out of very fast pulses.
The catches
If the switching is slow, for example a few hundred times a second like on some phone screens, a camera can show stripes, and some people get eye strain or headaches from it. Coils in a power circuit can also hum or whine at some frequencies. Going well above what people can see and hear, around 20,000 times a second, avoids both.
The very low end can also be tricky. At very small duty cycles some driver chips stop reacting cleanly, so 1 % may not look like 1 %, and 0 % may not be completely dark. That is the next thing I want to test on my screen.
If you want the details for the Pico, the RP2040 datasheet has a full section on its PWM hardware, and the MicroPython quick reference shows how to use it.
Drafted with an assistant, edited by @wintaru
0 comments