Skip to content

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.

duty = value over 255 and value = percent × 255 over 100

Percent to value

You wantValueDuty you get
10%2610.2%
25%6425.1%
50%12850.2%
75%19174.9%
100%255100%

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.

Try it yourself: free lesson

“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:

Iavg ≈ duty × Ion

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.

Try it yourself: free lesson

“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 needed

Work it out

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3analogWrite(9, 255) gives a duty cycle of…
    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.
  2. Question 2 of 3The same analogWrite drives an LED. analogWrite(9, 128) on an LED gives…
    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.
  3. Question 3 of 3Which pins can analogWrite dim?
    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.
Keep practising: free lesson

“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.

Every topic