Electronics
Arduino analogWrite values: from percent to 0–255
analogWrite(pin, value) sets a PWM duty cycle from a value between 0 and 255: 0 is always off, 255 is always on, and anything between switches the pin on for value ÷ 255 of each cycle. So a quarter is 64, a half is 128 and three quarters is 191. It's not a percentage.
Percent to value
| You want | Value | Duty you get |
|---|---|---|
| 10% | 26 | 10.2% |
| 25% | 64 | 25.1% |
| 50% | 128 | 50.2% |
| 75% | 191 | 74.9% |
| 100% | 255 | 100% |
Why 255? The value is a byte, 8 bits, and 2⁸ = 256 levels run from 0 to 255. Each step is about 0.4% of full power.
Live experiment
Dim to order
Pip's dimmer takes orders: a quarter, a half, three quarters, a tenth. Set analogWrite's value from 0 to 255 to match each one and watch four PWM cycles of the LED's current, full 12.5 mA pulses, with their average dashed. Try typing the percentage itself.
“Dimming with PWM” is lesson 5 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 53 before it first.
The percentage bug
analogWrite(9, 75) looks like 75%, but it's 75 ÷ 255, only 29%. analogWrite(9, 50) is about 20%, and analogWrite(9, 25) is a dim 10%. If an LED is mysteriously dim, check whether a percentage went in where a byte belongs.
Values outside 0 to 255 don't clamp neatly on a real board, so keep them in range with constrain() or map() first. Feeding analogRead's 0 to 1023 straight in is the classic way to get it wrong: divide by 4, or map it, first.
What the LED actually gets
The pin is only ever fully on or fully off, so the LED's current is full or nothing. What changes is how long each pulse lasts. Averaged over a cycle:
A red LED that draws 12.5 mA fully on gets about 0.75 × 12.5 ≈ 9.4 mA at a value of 191. The pin's average voltage follows the same rule, 75% of 5 V, about 3.75 V.
Which pins, and how fast
On an Uno, only pins 3, 5, 6, 9, 10 and 11 can do it, each marked with a ~. They run at about 490 Hz, or about 980 Hz on pins 5 and 6, far too fast for an eye to see flicker. On any other pin, analogWrite can only switch fully on (values of 128 and up) or fully off.
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.
“Dimming with PWM” is lesson 5 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 53 before it first.
Or start from the very first lesson, no account neededWork it out
Practice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: 100%. 255 is the top of the range, so duty = 255 over 255 = 100%. The pin stays HIGH.
- Why not “25%”? 25% duty is a value of about 64. At 255, the top of the range, the pin never goes LOW.
- Why not “50%”? 50% is a value of about 128, half of 255. At 255 the pin stays HIGH all the time.
Show the answer
Answer: About half brightness. The same 0–255 sets a motor's speed here instead of an LED's brightness.
- Why not “A blink”? The pin does switch on and off, but hundreds of times a second, far too fast to see, so it looks like a steady glow.
- Why not “Off”? Off is 0. At 128 the pin is HIGH about half the time, so the LED glows.
- Why not “Full brightness”? Full brightness is 255. At 128 the pin is HIGH for only about half of each cycle.
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 a hardware timer behind them can make PWM: 3, 5, 6, 9, 10 and 11 on an Uno.
- Why not “Only pin 13”? Pin 13 has no PWM, so analogWrite there can only switch it fully on or fully off.
- Why not “Only A0 to A5”? A0 to A5 are analog inputs for analogRead. They can't make PWM.
“Dimming with PWM” is lesson 5 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 53 before it first.
Questions people ask
What does analogWrite(9, 127) do?
It switches pin 9 on for 127 ÷ 255, about 50% of every cycle, roughly 490 times a second. An LED on the pin looks about half as bright, and its average current is about half the full current.
How do I convert a percentage to an analogWrite value?
Multiply by 255 and divide by 100, then round. 25% is 64, 50% is 128 and 75% is 191. In code, map(percent, 0, 100, 0, 255) does it, but it rounds down, so 25% comes out as 63.
Does analogWrite output a real analog voltage?
No. The pin is only ever 0 V or 5 V; it just changes how long it stays at 5 V. The average is duty × 5 V. For a steady voltage, smooth it with an RC low-pass filter whose cutoff is well below the PWM frequency.
Related topics
- How does PWM work?How a pin that's only on or off dims an LED: duty cycle and analogWrite's 0–255.
- How Arduino analogRead worksHow analogRead turns 0–5 V into 0–1023, and how to scale it for analogWrite.
- How the Arduino map() function worksRescale a reading from one range to another, and the traps that come with it.
- Low-pass filter explainedA resistor and a capacitor that pass the bass and fade the treble, and where the cutoff sits.