# How To Change PWM Frequency Of Arduino Mega

Last updated on March 27th, 2024 at 04:56 pm

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.

However, the default frequency on PWM pins can be a limitation in some high-frequency control circuits. Yes, the frequency on these pins has some default values. But the good news is that you can easily change this frequency using a simple one-line code!

PWM frequency change on other Arduino boards

Change PWM frequency of UNO

Change PWM frequency change of Nano

Here is the default frequency of each PWM pin of Arduino Mega:

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

1) PWM frequency for D4 & D13:

490.20 Hz (The default)

2) PWM frequency for D2, D3, D5 to D12, D44, D45 and D46:

976.56 Hz (The default)

These frequencies work best for low-frequency tasks such as dimming an LED, but they are not suitable for high-frequency circuits. For instance, 1 kHz is insufficient for an S.M.P.S.

In many projects, especially those involving high-frequency pulses like a Buck Converter, adjusting the frequency is necessary. Here are simple one-line commands to modify the PWM frequency on Arduino Mega:

One line code for changing PWM frequency on D4 & D13 pins:

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

TCCR0B = TCCR0B & B11111000 | B00000010; // 7812.50 Hz

TCCR0B = TCCR0B & B11111000 | B00000011; //  976.56 Hz

TCCR0B = TCCR0B & B11111000 | B00000100; // 244.14 Hz

TCCR0B = TCCR0B & B11111000 | B00000101; // 61.04 Hz

TCCR5B = TCCR5B & B11111000 | B00000101; // 30.64 Hz``````

Command for D11 & D12 pins:

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

TCCR1B = TCCR1B & B11111000 | B00000010;  // 3921.16 Hz

TCCR1B = TCCR1B & B11111000 | B00000011;  // 490.20 Hz

TCCR1B = TCCR1B & B11111000 | B00000100;  // 122.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000101;   // 30.64 Hz``````

Command for D9 & D10 pins:

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

TCCR2B = TCCR2B & B11111000 | B00000010;  // 3921.16 Hz

TCCR2B = TCCR2B & B11111000 | B00000011;  // 980.39 Hz

TCCR2B = TCCR2B & B11111000 | B00000100;  //  490.20 Hz

TCCR2B = TCCR2B & B11111000 | B00000101;  //  245.10 Hz

TCCR2B = TCCR2B & B11111000 | B00000110;  //  122.55 Hz

TCCR2B = TCCR2B & B11111000 | B00000111;  //   30.64 Hz``````

Command for D2 & D3 and D5:

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

TCCR3B = TCCR3B & B11111000 | B00000010;  //  3921.16 Hz

TCCR3B = TCCR3B & B11111000 | B00000011;  //  490.20 Hz

TCCR3B = TCCR3B & B11111000 | B00000100;   //  122.55 Hz

TCCR3B = TCCR3B & B11111000 | B00000101;    //  30.64 Hz``````

Command for D6 & D7 and D8:

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

TCCR4B = TCCR4B & B11111000 | B00000010;  //  3921.16 Hz

TCCR4B = TCCR4B & B11111000 | B00000011;  //  490.20 Hz

TCCR4B = TCCR4B & B11111000 | B00000100;   //  122.55 Hz

TCCR4B = TCCR4B & B11111000 | B00000101;    //  30.64 Hz

``````

Command for D44 & D45 and D46:

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

TCCR5B = TCCR5B & B11111000 | B00000010;  // 3921.16 Hz

TCCR5B = TCCR5B & B11111000 | B00000011;  //    490.20 Hz

TCCR5B = TCCR5B & B11111000 | B00000100;  //  122.55 Hz``````

## Demonstration

Let’s use the above code to change the frequency on pin D3. The circuit is simulated in proteus software.

Check out: How to simulate Arduino in Proteus?

Step 1: Place the following components on the worksheet.

• Two Arduino Mega
• Oscilloscope

Connect them as shown below.

Step 2: Connect the D3 pins of each Mega to the Oscilloscope.

Step 3: The program for the first Mega board outputs the PWM signal at a default frequency as no additional command is used.

``````// Default PWM frequency

void setup() {
pinMode(3, OUTPUT);
}

void loop() {
analogWrite(3, 155);
}``````

The program for the Mega2 board has an additional command that sets the frequency to 31372.55 Hz.

``````// Set the PWM frequency to 31 KHz at pin D3

void setup() {
TCCR3B = TCCR3B & B11111000 | B00000001;  // for PWM frequency of 31372.55 Hz
pinMode(3, OUTPUT);
}

void loop() {
analogWrite(3, 155);
}``````

Step 4: Run the simulation

As you can see in the image above, the frequency of the signal on channel 2(connected to Mega2) is more than that on channel 1(connected to Mega2).

Ankit Negi

I am an electrical engineer by profession who loves to tinker with electronic devices and gadgets and have been doing so for over six years now. During this period, I have made many projects and helped thousands of students through my blog and YouTube videos. I am active on Linkedin.

### 6 thoughts on “How To Change PWM Frequency Of Arduino Mega”

1. Thanks for your clear explanation.
Regards
Franz

2. You have a mistake!
“Code for Available PWM frequency for D2, D3 & D5:
TCCR4B = TCCR4B & B11111000 | B00000001; ”

It needs to be TCCR3B NOT TCCR4B!!!

• Hi Peter,
Thankyou for pointing out the mistake. The frequency code for the control pins 5,3 and 2 is now changed from timer 4 to timer 3.

3. Good Tutorial but the timer codes listed for the pins are incorrect.
The correct ones are:
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);