How To Change PWM Frequency Of Arduino Mega - ETechnophiles

Last updated on February 5th, 2025 at 12:46 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:

976.56 Hz (The default)

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

490.20 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.

Two Arduino Mega connected to the Oscilloscope
Two Arduino Mega connected to the oscilloscope

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

Each board outputs the PWM signal at different frequencies
Each board outputs the PWM signal at different frequencies

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).

Tag » Arduino Mega 2560 Pwm Timers