Go Down

Topic: Arduino Mega PWM pins (Read 8679 times) previous topic - next topic

giantmolecules

Hey Folks,

I just got an arduino mega, and I'm trying to use all of the available PWM pins. I gather from the documentation that pins 0-13 are reserved for PWM, but I notice that pins 0 and 1 are also RX TX pins as well. PWM works well on pins 2-13, but 0 and 1 just turn on and off (no analog output?). Do I need to disable serial on pins 0 and 1 to use them for PWM? If so, how do I go about doing that? Sample code below (I read that it is not necessary to explicitly define the pins as outputs...I tried both ways...)

Code: [Select]

/*
ARDUINO MEGA PWM TEST
*/

void setup(){
 for(int i=0; i<14; i++){
   pinMode(i, OUTPUT);
 }
}

void loop(){
 for(int j=0; j<255; j++){
   for(int i=0; i<14; i++){
     analogWrite(i, j);
   }
   delay(10);
 }
}

mem

#1
May 20, 2009, 07:24 pm Last Edit: May 20, 2009, 07:24 pm by mem Reason: 1
from looking at the arduino core source code I don't think pins 0 and 1 are enabled for pwm.  try using pins  44, 45 and 46.

mungbean

I'm finding that 2..13 plus 45 and 46 work fine for PWM, but 44 behaves like a digital-only pin, turning only on or off when using analogWrite()

The Mega schematic suggests PL3 (46/OC5A), PL4 (45/OC5B) and PL5 (44/OC5C) should all be PWM... is this an error in the schematic, or in the analogWrite() code?

Or maybe there's some extra kludge required to get 44 working?

retrolefty

Quote
2..13 plus 45 and 46 work fine for PWM


The Arduino Mega's spec for number of PWM outputs is 14, so isn't 2..13 + 45 & 46 = 14?

http://arduino.cc/en/Main/ArduinoBoardMega

Lefty


mungbean

#4
Sep 13, 2009, 07:00 pm Last Edit: Sep 13, 2009, 07:41 pm by mungbean Reason: 1
OK, looking at the source (wiring_analog.c lines 132-174), TIMER5C isn't catered for, whereas TIMER5A and TIMER5B, corresponding to 45 and 46 are included.

Is there a reason why this is so, or is it just an oversight?

Update: I tried adding this extra code to wiring_analog.c just before the #endif on line 174 but it made no difference.

Code: [Select]

     } else if (digitalPinToTimer(pin) == TIMER5C) {
           // connect pwm to pin on timer 5, channel C
           sbi(TCCR5A, COM5C1);
           // set pwm duty
           OCR5C = val;

Pumbaa

Sorry for putting this thread up. But i got a similar Issue:

Pin 44 seems to be prepared for PWM-Output. On the list of timerpins (in pins.c) pin 44 is listed as a pwm-pin,connected to Timer5c
Does somebody know, why the PWM on pin 44 is just "half-implemented"? (from my POV)
And what to do exactly, to make it more analog? :)


Pumbaa

#6
Jan 18, 2010, 03:28 pm Last Edit: Jan 19, 2010, 07:44 am by willich Reason: 1
OK, i reply to myself:
It works!
I think it was forgotten to implement.
At least, if you make that change to the coming Version18 (didn't try with 17), whose Releasecandidate can be downloaded.
Just add
Code: [Select]
} else if (digitalPinToTimer(pin) == TIMER5C) {
           // connect pwm to pin on timer 5, channel B
           sbi(TCCR5A, COM5C1);
           // set pwm duty
           OCR5C = val;

to the wiring_analog.c

mungbean

Thanks Pumbaa!

So what's changed with v18?

Pumbaa

#8
Jan 19, 2010, 07:43 am Last Edit: Jan 19, 2010, 08:03 am by willich Reason: 1
I just added it to my RC of V18 cause I'm messing around with it these days.

And it worked. But me looking at V17's wiring_analog.c and pins_arduino.c doesn't show me a reason why it shouldn't work with V17.

I just didn't test if it works with the V17 too.
Just try it out an let us know :)
(maybe some: "restart the IDE to recompile the libs"-issue.)

I wanted to do the same like you, then i asked myself: just google, if smby else has the same issue.
Found you..., with what i wanted to do. And was frustrated, cause you said it didn't work. Tried it nevertheless, and it works :)



But if you asking for general changes in V18:
http://code.google.com/p/arduino/
"delaysmicros is changed to don't be so accurate, so it doesn't turn off interrupts" is my favorit :)





MarkT

Tried it with 0017, pin 44 works fine with the missing code added.

Is there a process for ensuring such fixes get into the next release?

The fix with the corrected comment:

Code: [Select]

      } else if (digitalPinToTimer(pin) == TIMER5C) {
           // connect pwm to pin on timer 5, channel C
           sbi(TCCR5A, COM5C1);
           // set pwm duty
           OCR5C = val;


Also is there a reason the Mega uses pin13 as OC0A rather than the more flexible OC1C ?  Its the only pin on the mega1280 that is connected to two timers, and the timer0 PWM is less flexible (only 8 bit and shared with millis())
[ I won't respond to messages, use the forum please ]

Erac


Hello Guys,

I'n having trouble coding the 1-2ms pulse in my arduino, my application is for the speed and directional control of my DC motor.

My motor controller needs: 1.5ms pulse to stop the motor. 2.0ms will be full speed forward.
1.0ms full speed reverse.

Any idea how to start coding this in the arduino environment?

Help

Regards

mem

#11
Sep 02, 2010, 10:17 am Last Edit: Sep 02, 2010, 10:18 am by mem Reason: 1
Hi Erac,

The easiest way to produce 1-2ms radio control pulses is with the servo library. The Servo library does not use PWM so this thread is probably not the best place for your questions. Best if you create new thread that describes your project and the help you want.

claudio

Could you explain me how to use pwm on pin 44 with rel 21.
I modified the wiring_analog.c file without success

Thank you

Go Up