Embedded Systems
How does PWM work?
PWM, or pulse-width modulation, makes an in-between output from a pin that can only be on or off. The pin switches between HIGH and LOW hundreds of times a second, and the fraction of each cycle it spends HIGH, the duty cycle, sets the average. An LED driven that way looks dimmer; a motor turns slower.
On, off, and the average
A digital pin has two states: 5 V or 0 V. Flick it on and off faster than your eye can follow, and the eye sees the average: a dimmer light. On an Arduino Uno the switching runs at about 490 cycles a second on most pins.
At 25% duty the pin is HIGH a quarter of the time, so its average is a quarter of 5 V, 1.25 V.
Live experiment
Slide the duty, then slow it down
The square wave on pin 9, with Pip as the LED. Slide analogWrite's number from 0 to 255 and watch how long the pin stays HIGH in each cycle. Then tap Slow motion to see that Pip is only ever fully on or fully off.
“Fade” is lesson 5 of 6 in Blink Bay, Embedded Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
analogWrite and its 0–255
analogWrite(pin, value) makes PWM. The value is a byte, from 0 (always LOW) to 255 (always HIGH):
So analogWrite(9, 191) gives 191 over 255 ≈ 75% duty. If the LED takes 12.5 mA while the pin is HIGH, its average current is about 0.75 × 12.5 mA ≈ 9.4 mA.
Only some pins have the fast switching built in. On an Uno they're 3, 5, 6, 9, 10 and 11, marked with a little ~.
A fade, step by step
Change the duty a little at a time and an LED fades smoothly. This loop steps it from 0 to 255, five at a time, waiting 20 ms between steps:
void loop() {
for (int b = 0; b <= 255; b = b + 5) {
analogWrite(9, b);
delay(20);
}
}
That's 52 steps, about a second from dark to full brightness.
Where the average isn't the whole story
Your eye blurs the flicker, but a fast circuit doesn't. It still sees full 5 V pulses, so PWM is not the same as a steady 1.25 V. That's fine for LEDs, heaters and motors, which respond to the average. A circuit that needs a true steady voltage needs a filter after the pin to smooth the pulses out.
Eyes also judge brightness roughly logarithmically, so 25% duty looks brighter than a quarter. Smooth dimmers step the duty along a curve rather than in equal steps.
PWM is how a board sets a motor's speed too: a motor driver takes the PWM signal and feeds the motor the bigger current it needs.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Fade” is lesson 5 of 6 in Blink Bay, Embedded Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: switching it fully on and off, fast. The pin is only ever fully HIGH or fully LOW. Switched faster than your eye can follow, the LED looks as bright as its average, so less time on looks dimmer.
- Why not “adding resistance to the pin”? Nothing about the pin's resistance changes. PWM only changes the share of each cycle the pin spends on, and your eye sees the average.
- Why not “lowering the pin's voltage a little”? A pin can only be fully HIGH or fully LOW, never a little lower. PWM changes how long it stays HIGH in each cycle, not the voltage it switches to.
Show the answer
Answer: The six marked with a ~. Only pins 3, 5, 6, 9, 10 and 11 have the fast switching built in. They're marked with a ~ on the board.
- Why not “Any pin”? Only pins with PWM hardware behind them can dim: on an Uno, 3, 5, 6, 9, 10 and 11. On any other pin, analogWrite gives only fully on or fully off.
- Why not “Only pin 13”? Pin 13 has the built-in LED, but on an Uno it has no PWM. analogWrite there can only turn it fully on or fully off.
- Why not “Only A0 to A5”? A0 to A5 are the analog inputs, read with analogRead. analogWrite isn't a true analog output: it's fast switching on the ~ pins.
Show the answer
Answer: A steady glow, about half bright. 50 blinks a second is too fast for your eye to follow, so it sees the average. On half the time looks like half bright.
- Why not “Nothing: L stays dark”? The LED is on for 10 ms of every 20 ms, so it still gets half the power. Switching fast doesn't stop it lighting.
- Why not “A fast blink you can easily count”? Your eye can't follow 50 flashes a second. It blends them into a steady glow at the average brightness, which is the whole idea behind PWM.
- Why not “L blinks once and stops”? loop() runs again and again, so the on-off pattern keeps repeating for as long as the board has power.
“Fade” is lesson 5 of 6 in Blink Bay, Embedded Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Questions people ask
What is PWM in simple terms?
Pulse-width modulation switches a pin fully on and fully off, hundreds of times a second, and varies how long it stays on in each cycle. An LED or motor responds to the average, so it looks dimmer or turns slower, even though the pin itself is only ever on or off.
What is duty cycle?
The fraction of each cycle the pin spends HIGH. A 25% duty cycle is on a quarter of the time; on a 5 V pin its average voltage is a quarter of 5 V, 1.25 V.
Why does analogWrite go to 255 and not 100?
The value is a byte, 0 to 255, not a percentage. 0 is always off and 255 always on, so the duty is value ÷ 255. analogWrite(9, 50) gives only about 20%; for p percent, use p × 255 ÷ 100.
What frequency is Arduino PWM?
About 490 Hz on most pins of an Arduino Uno, and about 980 Hz on pins 5 and 6. Only the pins marked with a ~ can do PWM: 3, 5, 6, 9, 10 and 11.
Related topics
- How Arduino analogRead worksHow analogRead turns 0–5 V into 0–1023, and how to scale it for analogWrite.
- Transistor as a switch, explainedA trickle into the base switches a big current: cutoff, saturation and the base resistor.
- How a two-wheeled robot steers and sets its speedHow a two-wheeled robot turns with no steering wheel, and how PWM sets each motor's speed.