Arduino mega PWM pins and frequency

Want to change the frequency on your your arduino mega?
Alot of this info is out there on the web but not much of it is all in one place and as easy to find as right here on Arduino's Forums.

Getting all this data together for my projects has taken me much effort since a lot of it was hard to completely understand when every place said refer to the datasheet.
Thats great! The french in the data sheet says yes there are timer,s and yes they can be changed, but there was no dumb dumb version in there to say to do this, type this here.

There is a lot of info on this for other boards like UNO etc.... but they don't work the same on the mega's

Lastly to all the OG's here that know this stuff cause it was feed to em in a bottle. Please be gentle. If there are mistakes or things to add. let me know so i can fix em or add em.

Mostly I'm making this cause it felt there wasn't greatly one place to go for this info.

Ok so

Arduino mega pins and hardware Timers

PWM Hardware
46 OC5?
45 OC5?
44 OC5?
13 OC1C
12 OC1B
11 OC1A
10 OC2A
9 OC2B
8 OC4C
7 OC4B
6 OC4A
5 OC3A
4 OC0B //never mess with this one at it directly effects major timing { i.e delay and millis}
3 OC3C
2 OC3B

All that is really important above is the numbers. They tell you what timers there on.
For example pin 2 is OC3B which is timer 3.

another way to look at this is:
timer 0 ----- pin 4
timer 1 ----- pin 11, 12, 13
timer 2 ----- pin 9, 10
timer 3 ----- pin 2, 3, 5
timer 4 ----- pin 6, 7, 8
timer 5 ----- pin 44, 45, 46

Unfortunate I do not have a oscilloscope cause I'm poor. So the rest of these frequencies are taken from other ref's. I can not guarantee the exact freq's but they should be close.
If someone knows these more accurately than i have listed please feel free to let me know so i can change em.

TIMER 0
Value Divisor Frequency
0x01 1 62500 hz
0x02 8 7812.5 hz
0x03 64 976.5625 hz // default
0x04 256 244.140625 hz
0x05 1024 61.03515625 hz
Code: TCCR0B = (TCCR0B & 0xF8) | value ;

TIMER 1
Value Divisor Frequency
0x01 1 31250 hz
0x02 8 3906.25 hz
0x03 64 488.28125 hz // default
0x04 256 122.0703125 hz
0x05 1024 30.517578125 hz
Code: TCCR1B = (TCCR1B & 0xF8) | value ;

TIMER 2
Value Divisor Frequency
0x01 1 31250 hz
0x02 8 3926.25 hz
0x03 32 976.5625 hz
0x04 64 488.28125 hz // default
0x05 128 244.140625 hz
0x06 256 122.0703125 hz
0x07 1024 30.517578125 hz
Code: TCCR2B = (TCCR2B & 0xF8) | value ;

the rest i do not know the frequencies: but I can tell you tell you the values available.

TIMER 3
Value Divisor Frequency
0x01 1 ? hz
0x02 8 ? hz
0x03 64 ? hz // default
0x04 128 ? hz
0x05 1024 ? hz
Code: TCCR3B = (TCCR3B & 0xF8) | value ;

TIMER 4
Value Divisor Frequency
0x01 1 ? hz
0x02 8 ? hz
0x03 64 ? hz // default
0x04 128 ? hz
0x05 1024 ? hz
Code: TCCR4B = (TCCR4B & 0xF8) | value ;

TIMER 5
Value Divisor Frequency
0x01 1 ? hz
0x02 8 ? hz
0x03 64 ? hz // default
0x04 128 ? hz
0x05 1024 ? hz
Code: TCCR5B = (TCCR5B & 0xF8) | value ;

To set your timer just add it to your sketch:
Here's and example;

void setup () {
TCCR2B = (TCCR2B & 0xF8) | 0x04;
}

I know there is more that can be done with these And there are other ways as well but this is what i know works thus far.
Hope this is helpful to someone.

More Info at SobiSource.com

1 Like

Ah, that's also something I've had to do - I've some (incomplete) notes on Arduino timers here Advanced Arduino: direct use of ATmega counter/timers which may be useful. Hopefully I'll fill more in in due course.

There is a conflict, at least in my eyes, when I look at these charts. Other says that timer0 on Mega is for Pins 4 and 13, other says Timer1 controls Pin 13? I am talking about Mega in both cases.

I don't understand this well yet, just trying to get the whole big picture before testing my applications in real life.
Thanks!

Cheers,
Kari

Yes, they both talk to pin 13 - you can't use them both at once in output mode. The Arduino code doesn't use timer1 channel C. Note that the timer peripherals have other modes than PWM (for instance they can count pulses) - there might be a way to use pin13 as input for one timer and output for the other?

Thank you MarkT for the input. I have been trying t figure out how to code the different waves. I understand the different waves (i.e square, Sawtooth, etc...
What i don't understand is what needs to be entered code wise to change the wave or pattern if you will.
For example I like to just be able to say set timer 3 to prescale 0x05.

Like this

void setup () {
TCCR2B = (TCCR2B & 0xF8) | 0x04;
}

but what would i have to add to say change it to fast pwm i believe it called.
where it's saw toothed /l/l/l
instead of ////
or square__------
Not really sure which one wold be the best for dc fans but I would be nice to experiment with the differnt waves if i knew how.

Im trying to set timer 5 to 25kHz. I used the following code based on the equation frequency=(system clock)/ [prescaler * (TOP +1)]:

void setup()
{
TCCR5A = B00100011; // Fast PWM
TCCR5B = B11001; // Prescaler = 1
OCR5A = 639; // TOP
pinMode(44, OUTPUT);
pinMode(45, OUTPUT);
pinMode(46, OUTPUT);
}

So you can easily see that 16MHz/[1*(639+1)] = 25kHz

The result? This sets pin 45 on the Mega to 25kHz, but there's no detectable frequency on pins 44 and 46. What can I do to set timer 5 (or any timer besides timer 0) so that all the pins on the timer are equal?

Can I use the code above?
TCCR5B = TCCR5B & 0xF8 |

When I enter 0x01 for the value, I get 31.37 kHz on my multimeter for channels 44, 45, & 46.
If so, then the "value" would equal 1.2549 for 25kHz, but how can I enter that in witout getting an error? I assumed it was HEX, and entered 1.4141205BC01A

This is so confusing. What am I missing?

The definitive guide are the Atmel datasheets for the relevant chips - in particular there are lots of different modes for the counters and you need to select the appropriate one for what you need. Some modes prevent all of the PWM pins from being used, for instance, some use other pins as clock inputs. As I remember it all the 16bit counters are identical in functionality, but timer0 and timer2 are slightly different (despite being both 8 bit).

Thanks for the tip Mark, I was able to find it: http://www.atmel.com/dyn/resources/prod_documents/doc2549.pdf Just a quick glance at it looks like what I need to solve this. It will take some time to go through the ~450 pages. In my last post, I guess I wasn't being too specific. I want to set Mega's timer 5 pins 45 and 46 to 25kHz to change the speed of some PWM fans (~25kHz so it's not so noisy). So most likely I will want to implement a Phase Correct PWM Mode. If anyone knows how to write a few lines of code to output 25kHz to pins 45 and 46, that would be great!