mega 2560 PWM frequency

Hi,
does anybody knows how to change the PWM frequency on Mega 2560?
I found some related topics but they all concern Arduino diecimila or Arduino mega 1280; No one of them seems to explain what to do when
arduino is 2560, and as far as I understand, the registers to change are different (the code working on 10000 does not work on 2560).

Unfortunately I'm not an expert and is very difficult for me to understand the code acting on registers.
Till now I just understood that arduino 2560 uses 5 different timers:
timer 0 for pin 13 and 4
timer 1 for pin 12 and 11
timer 2 for pin 10 and 9
timer 3 for pin 5 and 3 and 2
timer 4 for pin 8 and 7 and 6

All timers generate a PWM frequency on 500 hertz, while timer 0 generates a pwm frequency of 1000 hetrz.
I need on all pins a frequency > 1000 hetrz!!!!

thanks in advance!
Valerio

Hi guys! After some day, I finally managed to change the PWM frequency of all PWM pins of my Arduino Mega 2560.
All the explanations are foundamentally here:

In this brief article the author explains very clearly how to change some values in Arduino Diecimila registers, in order to enhance the PWM frequency.
Here I simply modified a little bit the author'code to make it match with Arduino 2560.
Here follows my brief instructions and descriptions!

  1. Arduino 2560 has 12 pins supporting PWM. They are from 2 to 13 included.

  2. the PWM default frequency is 490 Hz for all pins, with the exception of pin 13 and 4,
    whose frequency is 980 Hz (I checked with an oscilloscope).

  3. In order to change frequency on pin 'A', we have to change some value in the timer
    (or register), controlling pin 'A'. This is the list of timers in Arduino Mega 2560:

timer 0 (controls pin 13, 4);
timer 1 (controls pin 12, 11);
timer 2 (controls pin 10, 9);
timer 3 (controls pin 5, 3, 2);
timer 4 (controls pin 8, 7, 6);

As you can see, a given timer controls more than one pin (every change about a timer will affect all pins depending on it!).

  1. You can access a timer simply changing in your code (tipically in the setup()), the value of variable TCCRnB, where 'n' is the number of register. So, if we want to change the PWM frequency of pins 10 and 9, we will have to act on TCCR2B .

  2. The TCCRnB is a 8 bit number. The first three bits (from right to left!) are called CS02, CS01, CS00, and they are the bits we have to change.
    Those bits in fact represent an integer number (from 0 to 7) called 'prescaler' , that Arduino uses to generate the frequency for PWM.

  3. First of all, we have to clear these three bits, i.e they must be all set to 0:

int myEraser = 7; // this is 111 in binary and is used as an eraser
TCCR2B &= ~myEraser; // this operation (AND plus NOT), set the three bits in TCCR2B to 0

  1. now that CS02, CS01, CS00 are clear, we write on them a new value:

int myPrescaler = 3; // this could be a number in [1 , 6]. In this case, 3 corresponds in binary to 011.
TCCR2B |= myPrescaler; //this operation (OR), replaces the last three bits in TCCR2B with our new value 011

  1. now we have a new PWM frequency on pin 9 and 10!

I registered those values on all PWM pins, changing the value of prescaler (the only exception are pins 13 and 14, see later):

prescaler = 1 ---> PWM frequency is 31000 Hz
prescaler = 2 ---> PWM frequency is 4000 Hz
prescaler = 3 ---> PWM frequency is 490 Hz (default value)
prescaler = 4 ---> PWM frequency is 120 Hz
prescaler = 5 ---> PWM frequency is 30 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

(prescalers equal t 0 or 7 are useless).

Those prescaler values are good for all timers (TCCR1B, TCCR2B, TCCR3B, TCCR4B) except for timer 0 (TCCR0B). In this case the values are:

prescaler = 1 ---> PWM frequency is 62000 Hz
prescaler = 2 ---> PWM frequency is 7800 Hz
prescaler = 3 ---> PWM frequency is 980 Hz (default value)
prescaler = 4 ---> PWM frequency is 250 Hz
prescaler = 5 ---> PWM frequency is 60 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

Note that timer 0 is the one on which rely all time functions in Arduino: i.e., if you change this timer, function like delay() or millis() will continue to work but at a different timescale (quicker or slower!!!)

Ok, I hope it will help!
Cheers, Valerio

3 Likes

What should I do if i want a frequency of 250 or 300Hz on pins 3 and 5??

@valerio_sperati
Wow! Thanks!
http://playground.arduino.cc/Main/TimerPWMCheatsheet
is very similar buy that articles seems to be only for ATmega328p or so. Definitely not for Arduino Mega. Mega's timer selections are different to the UNO.

What is with pins 44, 45, and 46, in specs for MEGA 2560 it is said that they can be used for PWM?

Thanks Valerio!!! I can confirm your post.
Thanks you so much,
Corrado :slight_smile:

good explanations ... thanks! :wink:

thanks! :slight_smile:

thank you so much.. :slight_smile:

Hi guys,
Defalut Frequecy of timer 1,2,3 and 4 is 490 HZ. what do i have to do for getting 1KHZ frequecy on every timer.

Thanks..

Hi,

this is great, it works! I would like to run 1 MHz piezo transducer to produce fog nebulizer.
My piezo resonant frequency is 1MHz. How to drive this piezo in correct way? I have mosfet P75NF75, what else do I need and which parameters/values?

Check attached image to see how I was trying to do this. I can hear buzzer at 4000-8000 kHz, but in contact with water it does not produce any mist or vibration.

Thanks.

Ok, my question is about Arduino Uno, but I believe it is relevant to the topic:
changing the PWM frequency on Timer 2 can I make the Virtual Wire library working on timer 2?

ı want to change arduino pwm freqaucny in MATLAB SIMULINK, how to do this,

Hi,

I Am new to mega2560. We are using TMC429 motor controller and i need to give 16Mhz clock. Kindly advice how to or which pin i can use to provide clock from Mega2560 to TMC429.

Thanks in advance

Naveen

Just wanted to say thank you to valerio_sperati for doing all the hard work and laying out the info on what timers control which pins and what prescalers relate to which frequency. I plagiarized(stole) your info and used it in my magnetic stirrer project. I gave you credit in my project. I was able to change my PWM frequency to be slower when my DC pm motor is very slow and this showed a great improvement in motor response.

prescaler = 2 ---> PWM frequency is 4000 Hz

Is it possible to get 1.7 Mhz ?

4000*425=1.7 Mhz !! how to get it?
I need to run piezo element to get fog from the water!

The steps to change PWM frequencies of different timers were explained clearly and awesome manner.

Thank you so much........

I want to set 60hz frequency for all timers except timer0. Is it possible or not.

If it is possible how it can be done.

plz, help me......

Hi,
what's the timer and pwm pin list for PRO MICRO?
Thanks

valerio_sperati:
Hi guys! After some day, I finally managed to change the PWM frequency of all PWM pins of my Arduino Mega 2560.
All the explanations are foundamentally here:
Arduino Diecimila and the Atmega168: Changing PWM Frequency on the Arduino Diecimila and the Atmega168
In this brief article the author explains very clearly how to change some values in Arduino Diecimila registers, in order to enhance the PWM frequency.
Here I simply modified a little bit the author'code to make it match with Arduino 2560.
Here follows my brief instructions and descriptions!

  1. Arduino 2560 has 12 pins supporting PWM. They are from 2 to 13 included.

  2. the PWM default frequency is 490 Hz for all pins, with the exception of pin 13 and 4,
    whose frequency is 980 Hz (I checked with an oscilloscope).

  3. In order to change frequency on pin 'A', we have to change some value in the timer
    (or register), controlling pin 'A'. This is the list of timers in Arduino Mega 2560:

timer 0 (controls pin 13, 4);
timer 1 (controls pin 12, 11);
timer 2 (controls pin 10, 9);
timer 3 (controls pin 5, 3, 2);
timer 4 (controls pin 8, 7, 6);

As you can see, a given timer controls more than one pin (every change about a timer will affect all pins depending on it!).

  1. You can access a timer simply changing in your code (tipically in the setup()), the value of variable TCCRnB, where 'n' is the number of register. So, if we want to change the PWM frequency of pins 10 and 9, we will have to act on TCCR2B .

  2. The TCCRnB is a 8 bit number. The first three bits (from right to left!) are called CS02, CS01, CS00, and they are the bits we have to change.
    Those bits in fact represent an integer number (from 0 to 7) called 'prescaler' , that Arduino uses to generate the frequency for PWM.

  3. First of all, we have to clear these three bits, i.e they must be all set to 0:

int myEraser = 7; // this is 111 in binary and is used as an eraser
TCCR2B &= ~myEraser; // this operation (AND plus NOT), set the three bits in TCCR2B to 0

  1. now that CS02, CS01, CS00 are clear, we write on them a new value:

int myPrescaler = 3; // this could be a number in [1 , 6]. In this case, 3 corresponds in binary to 011.
TCCR2B |= myPrescaler; //this operation (OR), replaces the last three bits in TCCR2B with our new value 011

  1. now we have a new PWM frequency on pin 9 and 10!

I registered those values on all PWM pins, changing the value of prescaler (the only exception are pins 13 and 14, see later):

prescaler = 1 ---> PWM frequency is 31000 Hz
prescaler = 2 ---> PWM frequency is 4000 Hz
prescaler = 3 ---> PWM frequency is 490 Hz (default value)
prescaler = 4 ---> PWM frequency is 120 Hz
prescaler = 5 ---> PWM frequency is 30 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

(prescalers equal t 0 or 7 are useless).

Those prescaler values are good for all timers (TCCR1B, TCCR2B, TCCR3B, TCCR4B) except for timer 0 (TCCR0B). In this case the values are:

prescaler = 1 ---> PWM frequency is 62000 Hz
prescaler = 2 ---> PWM frequency is 7800 Hz
prescaler = 3 ---> PWM frequency is 980 Hz (default value)
prescaler = 4 ---> PWM frequency is 250 Hz
prescaler = 5 ---> PWM frequency is 60 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

Note that timer 0 is the one on which rely all time functions in Arduino: i.e., if you change this timer, function like delay() or millis() will continue to work but at a different timescale (quicker or slower!!!)

Ok, I hope it will help!
Cheers, Valerio

valerio_sperati:
Hi guys! After some day, I finally managed to change the PWM frequency of all PWM pins of my Arduino Mega 2560.
All the explanations are foundamentally here:
Arduino Diecimila and the Atmega168: Changing PWM Frequency on the Arduino Diecimila and the Atmega168
In this brief article the author explains very clearly how to change some values in Arduino Diecimila registers, in order to enhance the PWM frequency.
Here I simply modified a little bit the author'code to make it match with Arduino 2560.
Here follows my brief instructions and descriptions!

  1. Arduino 2560 has 12 pins supporting PWM. They are from 2 to 13 included.

  2. the PWM default frequency is 490 Hz for all pins, with the exception of pin 13 and 4,
    whose frequency is 980 Hz (I checked with an oscilloscope).

  3. In order to change frequency on pin 'A', we have to change some value in the timer
    (or register), controlling pin 'A'. This is the list of timers in Arduino Mega 2560:

timer 0 (controls pin 13, 4);
timer 1 (controls pin 12, 11);
timer 2 (controls pin 10, 9);
timer 3 (controls pin 5, 3, 2);
timer 4 (controls pin 8, 7, 6);

As you can see, a given timer controls more than one pin (every change about a timer will affect all pins depending on it!).

  1. You can access a timer simply changing in your code (tipically in the setup()), the value of variable TCCRnB, where 'n' is the number of register. So, if we want to change the PWM frequency of pins 10 and 9, we will have to act on TCCR2B .

  2. The TCCRnB is a 8 bit number. The first three bits (from right to left!) are called CS02, CS01, CS00, and they are the bits we have to change.
    Those bits in fact represent an integer number (from 0 to 7) called 'prescaler' , that Arduino uses to generate the frequency for PWM.

  3. First of all, we have to clear these three bits, i.e they must be all set to 0:

int myEraser = 7; // this is 111 in binary and is used as an eraser
TCCR2B &= ~myEraser; // this operation (AND plus NOT), set the three bits in TCCR2B to 0

  1. now that CS02, CS01, CS00 are clear, we write on them a new value:

int myPrescaler = 3; // this could be a number in [1 , 6]. In this case, 3 corresponds in binary to 011.
TCCR2B |= myPrescaler; //this operation (OR), replaces the last three bits in TCCR2B with our new value 011

  1. now we have a new PWM frequency on pin 9 and 10!

I registered those values on all PWM pins, changing the value of prescaler (the only exception are pins 13 and 14, see later):

prescaler = 1 ---> PWM frequency is 31000 Hz
prescaler = 2 ---> PWM frequency is 4000 Hz
prescaler = 3 ---> PWM frequency is 490 Hz (default value)
prescaler = 4 ---> PWM frequency is 120 Hz
prescaler = 5 ---> PWM frequency is 30 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

(prescalers equal t 0 or 7 are useless).

Those prescaler values are good for all timers (TCCR1B, TCCR2B, TCCR3B, TCCR4B) except for timer 0 (TCCR0B). In this case the values are:

prescaler = 1 ---> PWM frequency is 62000 Hz
prescaler = 2 ---> PWM frequency is 7800 Hz
prescaler = 3 ---> PWM frequency is 980 Hz (default value)
prescaler = 4 ---> PWM frequency is 250 Hz
prescaler = 5 ---> PWM frequency is 60 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

Note that timer 0 is the one on which rely all time functions in Arduino: i.e., if you change this timer, function like delay() or millis() will continue to work but at a different timescale (quicker or slower!!!)

Ok, I hope it will help!
Cheers, Valerio

Nice

1 Like

It is possible to have 120khz in output?