Analog I/O

Lesson 72: Using analogWriteResolution()

Learn how analogWriteResolution() changes PWM value range and how resolution affects output control precision.

Progress indicator

Lesson 72 of 72

Learning Objectives

  • Understand what analogWriteResolution() changes in PWM output range.
  • Learn PWM resolution math using (2^bits - 1) max value.
  • Compare resolution and duty-cycle behavior without confusion.
  • Configure resolution safely and scale PWM values correctly.
  • Avoid board-support and range mismatch mistakes.

Concept Explanation

What is analogWriteResolution()

analogWriteResolution(bits) defines how many bits are used for PWM output values.

More bits mean more selectable PWM levels, giving finer control.

analogWriteResolution() Syntax

analogWriteResolution(bits);

How analogWriteResolution() Works

  1. Set resolution bit count (example: 8, 10, 12).
  2. Maximum PWM value becomes (2^bits - 1).
  3. analogWrite() then expects values in this new range.
  4. Duty-cycle precision improves with higher resolution.

PWM Resolution Concept

Resolution = number of available PWM steps.

BitsRangeTotal levels
80-255256
100-10231024
120-40954096

Resolution vs Duty Cycle

Duty cycle is still ON-time ratio. Resolution only changes how finely you can set that ratio. With higher resolution, each step changes duty by a smaller amount.

Setting PWM Resolution

Set once in setup(), then keep PWM values scaled to selected resolution.

Supported Boards Note

This API is board/core dependent. Some boards ignore it or support limited bit ranges. Always verify board documentation.

When to Use analogWriteResolution()

  • Need finer dimming or motor-speed control steps.
  • Need wider control range for tuning output behavior.
  • Using boards that expose configurable PWM resolution.

Example Code

This sketch uses 12-bit resolution and prints both PWM value and duty percentage.

const int LED_PIN = 2;
int pwmValue = 0;
int stepValue = 256;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);

  // 12-bit PWM resolution -> range 0..4095
  analogWriteResolution(12);
}

void loop() {
  analogWrite(LED_PIN, pwmValue);

  float dutyPercent = (pwmValue / 4095.0) * 100.0;

  Serial.print("pwmValue=");
  Serial.print(pwmValue);
  Serial.print(", dutyPercent=");
  Serial.println(dutyPercent, 2);

  pwmValue = pwmValue + stepValue;

  if (pwmValue >= 4095 || pwmValue <= 0) {
    stepValue = -stepValue;
  }

  delay(120);
}

Example Code Explanation

  1. analogWriteResolution(12) sets PWM range to 0-4095.
  2. analogWrite(LED_PIN, pwmValue) applies current duty level.
  3. dutyPercent converts raw PWM value into human-readable percent.
  4. pwmValue changes by stepValue each cycle.
  5. Edge check flips direction for fade up/down pattern.
  6. Serial output validates both numeric range and percent mapping.

What Happens Inside

  1. Resolution config sets PWM compare register scale.
  2. analogWrite value is interpreted in that configured scale.
  3. PWM hardware updates HIGH pulse width per cycle.
  4. Output device reacts to average duty-cycle energy.

Common Mistakes with analogWriteResolution()

  • Keeping old 0-255 values after switching to higher resolution.
  • Using unsupported bit depth for the current board.
  • Assuming all Arduino-family boards implement this API equally.
  • Forgetting to rescale threshold logic and fade steps.

Best Practices for analogWriteResolution()

  • Set resolution in setup and document chosen bit depth clearly.
  • Scale all PWM values to configured max range.
  • Use percent logging to compare behavior across resolutions.
  • Confirm board/core support before production use.

Try it now

Open the simulator workspace and observe 12-bit PWM value and duty-percent changes.

Run in Simulator