Notifications
Clear all

What exactly is PWM resolution ?

7 Posts
5 Users
5 Reactions
9,133 Views
1
Topic starter

Hey there everyone,

I've been reading up on PWM and came across something I'm a bit confused about. What exactly is PWM resolution?

I noticed that the UNO R3 has a PWM resolution of 10 bits, whereas the ESP32 has a 16-bit PWM resolution. I understand that more bits mean a more accurate dummy DAC signal, but how exactly does this work? Can someone explain the details behind PWM resolution and how it affects the signal quality?

Thanks in advance!


2 Answers
4

Hey,

Note: UNO R3 supports 8-bit PWM resolution, not 10. 

Higher resolution means the PWM output can be more finely tuned, resulting in a smoother signal. This is particularly important in applications like motor control, LED dimming, and audio signal generation.

8-bit resolution means there are 256 possible duty cycle values (from 0 to 255). That's why the analogWrite(PWM pin, PWM value) takes values bw 0 and 255.  In this case, increasing the duty cycle step by step corresponds to a change of approximately 0.4% (1/256) of the full-scale value.

Whereas the 16-bit resolution means there are 65,536 possible duty cycle values (from 0 to 65,535).
Each step in the duty cycle corresponds to a change of approximately 0.0015% (1/65,536) of the full-scale value.

As much as the resolution is important, so does the frequency of the PWM signal. The increase in PWM resolution decreases the maximum PWM frequency possible for the same clock frequency.

if UNO and ESP32 have the same clock frequency i.e., 16 MHZ. The maximum possible PWM frequency(16-bit) for ESP32 will only be 244 Hz. Whereas for UNO(8-bit), it is 62.5 KHz. 

For example, 

16 MHz / 256 and 16 MHz / 65,536.

EDIT:

Hey everyone! Let’s clear up the confusion regarding PWM resolution and the difference between dividing by 2^n versus 2^n - 1

The Hardware Timer Perspective

In fast PWM mode, the timer counts from 0 up to a “TOP” value and then overflows back to 0.

  • For 8-bit PWM, TOP = 255. This gives you a counter range of 0–255 = 256 distinct counts.
  • For 2-bit PWM, TOP = 3. This gives you a counter range of 0–3 = 4 distinct counts.

Thus, in terms of raw timer ticks, there are 2^n counts per cycle.

The Duty Cycle Perspective

When calculating duty cycle, we typically use:

Duty Cycle (%)= (Compare Register Value/TOP) ×100.
  • For 8-bit PWM, you divide by 255 (TOP = 255), so the highest compare value 255 yields 100 % duty cycle.
  • For 2-bit PWM, you divide by 3 (TOP = 3), so a compare value of 3 yields 100 % duty cycle.

If you were to divide by 2^n directly (e.g., 256 for 8-bit), the maximum compare value (255) would give (255/256)x 100 =~ 99.6% which technically matches clock ticks but doesn’t align with the usual definition of 100 % on hardware PWM outputs.

Why It Matters

  • 0 % duty cycle: Compare Register = 0.
  • 100 % duty cycle: Compare Register = TOP (which is 2^n - ).
  • Users generally expect that the maximum compare setting translates to the output being fully ON (i.e., 100 %).

Summary

  1. The timer truly counts 2^n steps (from 0 to 2^).
  2. However, to get a duty cycle percentage from 0 % to 100 %, you divide the compare value by 2^n - 1.
  3. That’s why for 8-bit PWM, you’ll see many references to dividing by 255, not 256.

 

 


Divyam Topic starter 13/07/2024 3:54 am

@ankunegi Thank you for explaining in detail. Although I don't really get the last part: "The maximum possible PWM frequency" section.


Admin Admin 16/07/2024 5:10 am

The microcontroller uses a timer to generate the PWM signal. The timer counts up from 0 to a maximum value. When it reaches the maximum, it resets to 0 and starts counting again. The duty cycle is determined by the value at which the timer output switches from low to high.

For 8-bit resolution (UNO): The timer counts from 0 to 255. To achieve the maximum PWM frequency, the timer should overflow as quickly as possible. So, the maximum PWM frequency is the clock frequency divided by the maximum count: 16 MHz / 256 = 62.5 kHz.
For 16-bit resolution (ESP32): The timer counts from 0 to 65,535. Using the same logic, the maximum PWM frequency is 16 MHz / 65,536 = 244 Hz.
In essence, higher resolution requires the timer to count to a larger number before overflowing, which takes more time. This directly limits the maximum possible PWM frequency.


FullBridgeRectifier 23/10/2024 7:05 am

@ankunegi The answer is on point but I think there's a mistake in your calculation. To calculate the duty cycle, we have to divide it by 255(the maximum value) and not 256(The total no. of steps).
For example: A 2-bit PWM signal has 4 possible steps: 0,1,2 and 3 corresponding to 0%, 33.33%, 66.67% and 100% duty cycle. You get this by dividing by 3, not 4. If you divide it by 4, you will get 25%. Which means 0%, 25%, 50% and 75%. See, you are not getting 100% duty cycle in this case.


0

To put this PWM resolution concept into a practical context: If you’re dimming an LED with an Arduino UNO’s 8-bit PWM, you might see noticeable brightness steps when changing the duty cycle. This is because each step is about 0.4% of the total brightness.

With the ESP32’s 16-bit PWM, each step is only 0.0015%, so the LED brightness change is much smoother and almost imperceptible. This is crucial if you’re working on projects that require precise control, like mood lighting or audio signal modulation.

But keep in mind, that the lower frequency at higher resolutions might introduce visible flicker in LEDs, so you’ll need to find a balance between resolution and frequency depending on your application.


Share: