Skip to content

analogWrite() Notes and Warnings in reference section #774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cattledogGH opened this issue Sep 1, 2020 · 1 comment
Open

analogWrite() Notes and Warnings in reference section #774

cattledogGH opened this issue Sep 1, 2020 · 1 comment
Labels

Comments

@cattledogGH
Copy link

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.

@per1234 per1234 added the bug label Sep 1, 2020
@edgar-bonet
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants