Circuit Bytes · Episode 45 · Electronics · PWM
"PWM: fake analog"
A digital pin is only fully on or fully off, but switching it hundreds of times a second fakes an in-between level. The share of time it's on is the duty cycle, and the average voltage is the duty cycle times the supply, so 25% of 5 V is 1.25 V. That's PWM, pulse-width modulation.
Only on or off
A microcontroller's digital output pin has two states: fully on (the supply, for example 5 V) or fully off (0 V). There is nothing in between. So how does an Arduino make an LED glow at half brightness, or spin a motor at half speed?
Too fast to see
The trick is to switch the pin on and off very quickly at a fixed rate. SparkFun notes that switching once a second (1 Hz) is a visible blink, but at 100 Hz or more your eye blurs the flicker into what looks like steady dimming. An Arduino Uno's analogWrite() switches its PWM pins at about 490 Hz (about 980 Hz on two pins), roughly 500 times a second. The LED really is flickering; your eye just can't keep up.
Duty cycle
Each on-and-off cycle is one period. The duty cycle is the percentage of each period the signal is on. On half the time is a 50% duty cycle. On a quarter of the time and off the other three quarters is a 25% duty cycle. The rate stays the same; only the width of the "on" pulse changes, which is where the name pulse-width modulation comes from.
The average voltage
Averaged over time, the output acts like a lower voltage:
average voltage = duty cycle × supply
So a 25% duty cycle on a 5 V pin gives 0.25 × 5 V = 1.25 V on average. A motor, or a simple filter, smooths the fast pulses into something close to that steady average.
Why it wastes so little heat
PWM is how LEDs get dimmed and how motor speed gets controlled. The switch doing the work, often a transistor like the one in episode 26, is always either fully on (very little voltage across it) or fully off (no current through it). Either way it turns very little power into heat. That's the same idea that makes switching regulators efficient (episode 41).
Try it yourself
Use average voltage = duty cycle × supply. A 5 V pin at a 40% duty cycle gives 0.40 × 5 V = 2 V on average. Now try a 3.3 V pin at 50% and check that you get 1.65 V.
Good to know
- Brightness doesn't look linear with duty cycle: an LED at 25% looks brighter than "a quarter" to your eye.
- Some people notice PWM flicker at low frequencies, and it can show up as stripes on phone cameras.
- Many household dimmers for old incandescent bulbs chop the AC wave instead (phase control), a related idea.
- Battery or low-voltage supplies only; leave wall wiring to a licensed electrician.
Answer to the comment question
A twelve-volt supply with a seventy-five percent duty cycle. What's the average voltage? 9 volts. The average is the duty cycle times the supply: 0.75 × 12 V = 9 V.
Test your knowledge
Quick quiz: "PWM: fake analog"
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- SparkFun Learn, "Pulse Width Modulation": duty cycle; 1 Hz flickers, 100 Hz or more looks like dimming
- Arduino language reference, "analogWrite()": PWM frequency by board, about 490 Hz on most Uno pins
- SparkFun Learn, "First FPGA Project: Getting Fancy with PWM": digital systems approximating analog values
Video transcript
How can flicker make a light look dimmer?
A chip's digital pin is only fully on or fully off. Five volts, or zero.
But switch it fast, hundreds of times a second, and your eye blurs the flicker into a steady glow.
The share of time it's on is the duty cycle. On a quarter of the time is a twenty-five percent duty cycle.
The average voltage is the duty cycle times the supply. Twenty-five percent of five volts is one point two five volts.
That's PWM, pulse-width modulation. It dims LEDs and controls motor speed, wasting very little heat.
A twelve-volt supply with a seventy-five percent duty cycle. What's the average voltage? Comment your answer below!
That's your byte.