How to change PWM frequency of Arduino Mega

How To Change PWM Frequency Of Arduino Mega

Arduino Mega is a beast when it comes to multitasking automation projects. Because of its fairly large number of digital and analog pins, Mega is the first priority in many complex projects including multiple output control. But the default frequency on PWM pins of Arduino Mega can be a limitation when it comes to High-frequency control circuits. Yes, the frequency on PWM pins is set to default and you can easily change the frequency by using a simple one-line code! Read below to know How To Change PWM Frequency Of Arduino Mega.

There are some cool facts about Arduino with which many students and makers are not familiar. And one of the facts is this: ” There is a certain default frequency for each PWM pin, which is called when the analogWrite command is used on that pin. And this default frequency can be changed to a value as high as 65Khz and as low as 30Hz by using just one line code without altering the shape of the PWM wave or attenuation.”.

Looking for Arduino UNO PWM frequency change?

How To Change Frequency On PWM Pins Of Arduino UNO

 

The default frequency on respective PWM pin of Arduino Mega:

Arduino Mega has a total of 15 PWM pins. 12 of them are from pin 2 to pin 13 whereas remaining 3 are D44, D45, and D46. The default PWM frequency for all pins is 490 Hz, except pin 4 and 13 whose default frequency is 980Hz.

PWM frequency from D2 to D13:

490.20 Hz (The DEFAULT)

 

PWM frequency for D4 & D13:

976.56 Hz (The DEFAULT)

Now, these frequencies are optimum for low-frequency applications like fading an LED. But these default frequencies are not suitable for High-frequency circuits. For example, 1Khz is nothing when it comes to an S.M.P.S.

There are many projects in which we require high-frequency pulses, One such project is a Buck-Converter. So to achieve frequency lower or higher than the default frequency on PWM pins, One line code we can use before initializing PWM pin as output is given below:

 

FOR ARDUINO MEGA:

Code for Available PWM frequency for D4 & D13:

TCCR0B = TCCR0B & B11111000 | B00000001; // for PWM frequency of 62500 Hz

TCCR0B = TCCR0B & B11111000 | B00000010; // for PWM frequency of 7812.50 Hz

TCCR0B = TCCR0B & B11111000 | B00000011; // for PWM frequency of 976.56 Hz (The DEFAULT)

TCCR0B = TCCR0B & B11111000 | B00000100; // for PWM frequency of 244.14 Hz

TCCR0B = TCCR0B & B11111000 | B00000101; // for PWM frequency of 61.04 Hz

TCCR5B = TCCR5B & B11111000 | B00000101; // for PWM frequency of 30.64 Hz

 

   Code for Available PWM frequency for D11 & D12:

TCCR1B = TCCR1B & B11111000 | B00000001;  // for PWM frequency of 31372.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000010;  // for PWM frequency of 3921.16 Hz

TCCR1B = TCCR1B & B11111000 | B00000011;  // for PWM frequency of 490.20 Hz

TCCR1B = TCCR1B & B11111000 | B00000100;  // for for PWM frequency of 122.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000101;   // for  PWM frequency of 30.64 Hz

 

 

Code for Available PWM frequency for D9 & D10:

TCCR2B = TCCR2B & B11111000 | B00000001;  // for PWM frequency of 31372.55 Hz

TCCR2B = TCCR2B & B11111000 | B00000010;  // for  PWM frequency of 3921.16 Hz

TCCR2B = TCCR2B & B11111000 | B00000011;  // for  PWM frequency of 980.39 Hz

  TCCR2B = TCCR2B & B11111000 | B00000100;  //  for PWM frequency of 490.20 Hz

TCCR2B = TCCR2B & B11111000 | B00000101;  // for PWM frequency of 245.10 Hz

TCCR2B = TCCR2B & B11111000 | B00000110;  // for PWM frequency of 122.55 Hz

TCCR2B = TCCR2B & B11111000 | B00000111;  // for PWM frequency of 30.64 Hz

 

 Code for Available PWM frequency for D2, D3 & D5:

TCCR4B = TCCR4B & B11111000 | B00000001;   // for PWM frequency of 31372.55 Hz

TCCR4B = TCCR4B & B11111000 | B00000010;  // for PWM frequency of 3921.16 Hz

TCCR4B = TCCR4B & B11111000 | B00000011;  // for PWM frequency of 490.20 Hz

TCCR4B = TCCR4B & B11111000 | B00000100;   // for PWM frequency of 122.55 Hz

TCCR4B = TCCR4B & B11111000 | B00000101;    // for PWM frequency of 30.64 Hz

 

 

  Code for Available PWM frequency for D6, D7 & D8:

TCCR4B = TCCR4B & B11111000 | B00000001;   // for PWM frequency of 31372.55 Hz

TCCR4B = TCCR4B & B11111000 | B00000010;  // for PWM frequency of 3921.16 Hz

TCCR4B = TCCR4B & B11111000 | B00000011;  // for PWM frequency of 490.20 Hz

TCCR4B = TCCR4B & B11111000 | B00000100;   // for PWM frequency of 122.55 Hz

TCCR4B = TCCR4B & B11111000 | B00000101;    // for PWM frequency of 30.64 Hz

 

 

Code for Available PWM frequency for D44, D45 & D46:

TCCR5B = TCCR5B & B11111000 | B00000001;  // for PWM frequency of 31372.55 Hz

TCCR5B = TCCR5B & B11111000 | B00000010;  // for PWM frequency of  3921.16 Hz

TCCR5B = TCCR5B & B11111000 | B00000011;  // for PWM frequency of   490.20 Hz

TCCR5B = TCCR5B & B11111000 | B00000100;  // for PWM frequency of   122.55 Hz

 

 

 

 

EXAMPLE:

To show you how frequency changes on applying the above code, Arduino circuit is simulated in Proteus(for Arduino UNO):

Check out: How to add Arduino Library to Proteus and Simulate Arduino Projects | 2018 Edition

1.Two Arduino are selected and placed on Front-Panel

2. Digital Pin 3 ( PWM pin) of each Arduino is connected to oscilloscope

3. Two separate programs are written for each Arduino:

Program A – Default frequency on Pin 3

void setup() {

pinMode(3,OUTPUT);
// put your setup code here, to run once:

}

void loop() {
analogWrite(3,155);
// put your main code here, to run repeatedly:

}

 

Program B – Changed frequency on Pin 3

void setup() {

TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
pinMode(3,OUTPUT);
// put your setup code here, to run once:

}

void loop() {
analogWrite(3,155);
// put your main code here, to run repeatedly:

}

 

 

4 .Hex file of above programs are given to Arduino

5. Run Simulation

 

6. It can be clearly seen in oscilloscope that frequency is increased to a very high value when this piece of code is used(for Arduino UNO) :
TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz

 

Read Similar Articles:

How to add Arduino Library to Proteus and Simulate Arduino Projects

How to Add Microphone library to Proteus and generate audio waveforms

How to Add And Simulate Ultrasonic Sensor Library in Proteus 

Leave a Reply

Your email address will not be published. Required fields are marked *