PWM Resolution

Hi there.

One thing I don't understand is why the pwm resolution is locked to 12bit. The Output comparator is 16bit, why can't we have 16bit pwm resolution?

Ok, next time I'll test first.

The documentation is confusing.

If you read the analogwriteresolution() function documentation, you get the idea that only 12 bit is suported.

Well, in fact, no!

I've tested with the fade test sketch changing to 16 bit resolution and while verifying with a multimeter, I can say, it works!! ]:D

This is a very nice improvement!!! :D

Sketch used:


 This example shows how to fade an LED on pin 13
 using the analogWrite() function.

 This example code is in the public domain.

int led = 13;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare LED pin to be an output:
  pinMode(led, OUTPUT);

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of LED pin :
  analogWriteResolution(16);  // Set pwm to 16bit resolution
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 65535) { //65535 is the maximum value for a 16bit number
    fadeAmount = -fadeAmount ; 
  // wait for 5 milliseconds to see the dimming effect    

Awesome. I can't wait to get a board and use the DAC! Although I wish there were also a 16-bit ADC on the uC.


I'm speaking about PWM ports, not DAC!

DAC is 12bit only I think.

Thanks for this, it will be interesting to disassemble the sketch and see what is being configured and how.

Reading another post in this section I see that there are timers and separate PWM Channels, I am guessing that the PWM Channels address specific pins like on the AVR's and that withe the timers you can use software PWM on any pin.

Duane B

The PWM resolution depends on the pin you select for analogWrite(). See analogWrite() in hardware/arduino/sam/cores/arduino/wiring_analog.c It does analog out using either hardware DAC, PWM, or Timer/Counter pins else does high/low on digital pins

timer pins: 2-5, 10-13, 58,60,61 PWM pins: 6-9

mapResolution() limits timer and PWM pins to 8 bit resolution, DAC 12-bit.

pin attributes are set in hardware/arduino/sam/variants/arduino_due_x/variant.cpp variant.h has




The chip supports 16-bit PWM on many pins, but the DUE seems to "recognize" only PWM pins 6-9

alvesjc: Ok, next time I'll test first.

The documentation is confusing.

If you read the analogwriteresolution() function documentation, you get the idea that only 12 bit is suported. [....]

Hi Alvesjc,

I'm sorry to ask you this so newbie question, but when I try to compile your code on my arduino 1.0.2 : I have this Orange message at the bottom : :sweat_smile: sketch_dec13a.ino: In function 'void loop()': sketch_dec13a:18: error: 'analogWriteResolution' was not declared in this scope

Do I need to add some Libraries ? how can I declare it ?

I use a arduino mega 2560 Mega ADK

Thanks :grin:

Hay All,

I went round and round a while ago with this, Haven’t messed with it lately, It would seem that the docks and the ide for due are still somewhat green and have not ripened yet. If pwm is your thing and speed is a must, put the due in the drawer for later and get a leaf maple. No a leaf maple is not the fix all i2c on maple will make you crazy. But pwm is a cinch, when the new ide comes out that really supports the due I am sure that it will be bad to the bone and then some.

The analogWriteResolution will adjust the resolution per pin, however if a value is given bigger than the capability it will just count by 2s 4s 8s instead of 1s.

In maple you can adjust the timer overflow and clock divisor and set it to just about any frequency and resolution you could want.

Hi all,

On the Arduino Due also you can configure clock divisor, frequency, resolution as you want. You have to use the real 8-channel PWM, which are available on many pins...

You can do it by directly setting the SAM3X registers, see the raw example I put here :,142103.msg1070358.html#msg1070358

Or you could use/create a nice librairy which makes some checking and provide nice function name, though I don't know if one already exists at the present time.