Pages: [1]   Go Down
Author Topic: Arduino Mega PWM pins  (Read 6954 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
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);
  }
}
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 20, 2009, 12:24:44 pm by mem » Logged

Bangalore, India
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

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

Logged

Bangalore, India
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
     } else if (digitalPinToTimer(pin) == TIMER5C) {
            // connect pwm to pin on timer 5, channel C
            sbi(TCCR5A, COM5C1);
            // set pwm duty
            OCR5C = val;
« Last Edit: September 13, 2009, 12:41:41 pm by mungbean » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 236
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? smiley

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 236
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
} 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
« Last Edit: January 19, 2010, 01:44:16 am by willich » Logged

Bangalore, India
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Pumbaa!

So what's changed with v18?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 236
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
(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 smiley



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 smiley




« Last Edit: January 19, 2010, 02:03:55 am by willich » Logged

0
Offline Offline
Shannon Member
****
Karma: 162
Posts: 10461
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
      } 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())
Logged

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: September 02, 2010, 03:18:11 am by mem » Logged

Milan
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: