How To Change PWM Frequency Of Arduino Mega - ETechnophiles
Maybe your like
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 HzCommand 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 HzCommand 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 HzCommand 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 HzCommand 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 HzCommand 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 HzDemonstration
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).
Tag » Arduino Mega 2560 Pwm Timers
-
Mega 2560 PWM Frequency - Project Guidance - Arduino Forum
-
Mega Timers - Programming Questions - Arduino Forum
-
Arduino Mega Timer/Counter 3 Fast PWM Mode 15 Duty Cycle
-
Arduino Mega PWM Pins And Frequency
-
Arduino PWM - Mega 2560 Pins, Registers And Changing The ...
-
Arduino - PWM Frequency And Timers - DomoticX Knowledge Center
-
Arduino Mega 2560 Pwm Timer Controls - Stack Overflow
-
Arduino-PWM-Frequency - ArduinoInfo - MyWikis
-
Need To Setup TIMER3 On Mega To Higher Frenquency PWM
-
Functions To Change PWM Frequency In Arduino UNO & MEGA.
-
Secrets Of Arduino PWM - Ken Shirriff's Blog
-
Giới Thiệu Arduino Mega 2560 - ĐIỆN TỬ TƯƠNG LAI
-
Changing PWM On Arduino Mega, Pins 9 & 10 To 20 To 25 KHz
-
PWM Adjustment Of Arduino Mega 2560 - Fear Cat