reference-en icon indicating copy to clipboard operation
reference-en copied to clipboard

analogWrite() Notes and Warnings in reference section

Open cattledogGH opened this issue 4 years ago • 1 comments

The reference section for analogWrite() https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/ includes this material in "notes and warning"

Notes and Warnings The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g. 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

Since analogWrite(pin,0) will be executed as digitalWrite(pin,LOW) I do not think that this statement is correct and it should be removed.

may result in a value of 0 not fully turning off the output on pins 5 and 6.

I would like to see some explanation of the "higher than expected duty cycles on pins 5 and 6". How big an effect is this, and is it worth a special warning? If indeed it is a significant issue, then the statement should be broadened to deal with the output pins on Timer 0 which are different than 5 and 6 on platforms other than the AT328.

cattledogGH avatar Sep 01 '20 00:09 cattledogGH

If you analogWrite(pin, n) to one of the pins connected to Timer 0, you get the duty cycle (n+1)/256. The only exception is, as you noted, that n = 0 turns the pin LOW:

n duty
0 0/256
1 2/256
2 3/256
... ...
254 255/256
255 256/256

From n = 0 to n = 1, the step in duty cycle is twice as large as the other steps.

edgar-bonet avatar Jun 18 '21 09:06 edgar-bonet