Pages: [1]   Go Down
Author Topic: Good PWM Pins  (Read 703 times)
0 Members and 1 Guest are viewing this topic.
Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

Just want to know what pins on the Arduino Mega 2560 have good PWM output. I know that pins 2 to 13 have PWM. So here is what I am asking:
If I set the duty cycle to 0, which pins are truly fully off? and if I set it to 255, which pins are truly fully on?

Thanks
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I set the duty cycle to 0, which pins are truly fully off? and if I set it to 255, which pins are truly fully on?
All of them. The analogWrite() function sees to that.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

Just want to know what pins on the Arduino Mega 2560 have good PWM output. I know that pins 2 to 13 have PWM. So here is what I am asking:
If I set the duty cycle to 0, which pins are truly fully off? and if I set it to 255, which pins are truly fully on?

Thanks


The analog write function takes the form of analogWrite(pin#,dutyvalue);
So your question as to which pins will be effected by the command, the answer is only the pin number(s) you use in your specific analogWrite() functions used in your specific sketch code.

Lefty

Logged

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand what you are saying about the analogWrite. But I came across this on the analogWrite page:

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.

I want to know if there is any other pins like 5 and 6 that behave differently?

Thanks
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I set the duty cycle to 0, which pins are truly fully off? and if I set it to 255, which pins are truly fully on?

I guess you're thinking of this note in the documentation for analogWrite():

Quote
Notes and Known Issues

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.

I think the answer to your question is given in bold above.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But I thought analogWrite(pin#,0) and analogWrite(pin#,255) are two 'special cases' in that the analogWrite library does a true digitalWrite(pin#,LOW) and digitalWrite(pin#,HIGH) for those two specific values respectably. Therefore the warning about possible variations in actual duty for small values used on pins 5 & 6 should probably be changed to:

Quote
This will be noticed mostly on low duty-cycle settings (e.g 1 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

Or am I all wet?

Lefty
Logged

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yessss... you are right about the note and that is the thing that bothers me. I wanna know if there is any other pins like 5 and 6 that have weird outputs?

Has anybody tested this with an oscilloscope?

Thanks
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yessss... you are right about the note and that is the thing that bothers me. I wanna know if there is any other pins like 5 and 6 that have weird outputs?

Has anybody tested this with an oscilloscope?

Thanks

Well there is always a possibility of different software libraries you try to include in your sketch trying to utilize the same timers used by analogWrite commands, thereby setting up a conflict with the analogWrite commands. The default arduino sketch startup code only uses timer0 to support the millis() functions so I wouldn't expect any other 'weird outputs' on other pwm pins driven by the other timers, but hey I didn't even know about the possible 'weirdness' for pins 4 and 5.

Anyway here is a code fragment from the analogWrite core function showing that any and all pwm output pins will be forced to a true LOW or HIGH when used with values 0 and 255 respectively:

Code:
void analogWrite(uint8_t pin, int val)
{
// We need to make sure the PWM output is enabled for those pins
// that support it, as we turn it off when digitally reading or
// writing with them.  Also, make sure the pin is in output mode
// for consistenty with Wiring, which doesn't require a pinMode
// call for the analog output pins.
pinMode(pin, OUTPUT);
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}
else
{
switch(digitalPinToTimer(pin))
{


Lefty
Logged

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the research.

Is there a page that shows what timers are used for each PWM pin on the Arduino Mega 2560?

I am using timer 1 in my code as a software interrupt.

Thanks
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the research.

Is there a page that shows what timers are used for each PWM pin on the Arduino Mega 2560?

I am using timer 1 in my code as a software interrupt.

Thanks


The below might be useful. The far left column is the 'abstracted' arduino pin number and the other columns show the AVR port and bit number of the corresponding pin and descriptions of secondary functions for some of the pins for both the 328p and mega1280/2560 boards.

https://spreadsheets.google.com/pub?key=rtHw_R6eVL140KS9_G8GPkA&gid=0

Lefty
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12161
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the research.

Is there a page that shows what timers are used for each PWM pin on the Arduino Mega 2560?

I am using timer 1 in my code as a software interrupt.

Thanks


Its all in the code - read the code rather than the documentation if you want to be sure what's happening(!)

The mappings for pins and ports is in pins_arduino.h for the specific variant (so in <installdir>/hardware/arduino/variants/mega/pins_arduino.h

Timer mappings can also be worked out from a knowledge of which chip pins have which Arduino pin numbers and
then reading the datasheet, but frankly that's a lot more hassle!
for the mega, checkout the other subdirectories of variants for other chips)
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: