How To Change the PWM Frequency Of Arduino

How To Change the PWM Frequency Of Arduino

The microcontroller has several timers that can perform different functions, such as generating a PWM signal. In order for the timer to generate a PWM signal, it has to be pre-configured by editing the timer register. When we work in the Arduino IDE, the timers are configured without our knowledge in the Arduino.h library, and actually get the settings the developers wanted. And these settings are not very good: the default PWM frequency is low, and the timers are not used to their full potential. Let’s look at the standard PWM of the ATmega328 (Arduino UNO/Nano/Pro Mini):

Timer Pins Frequency Resolution
Timer 0 D5 and D6 976 Hz 8 bits (0- 255)
Timer 1 D9 and D10 488 Hz 8 bits (0-255)
Timer 2 D3 and D11 488 Hz 8 bits (0-255)

In fact, all timers can easily give out 64 kHz PWM signal, and timer 1 – it is even 16 bits, and at the frequency that was given to him Arduino, could work with a resolution of 15 bits instead of 8, and that, by the way, 32768 gradations of filling instead of 256! So why this injustice? Timer 0 is in charge of timing and is set so that the milliseconds are ticking precisely. The other timers are combed to zero to prevent the Arduino-enthusiast from having unnecessary problems. This approach is generally understandable but would have made at least a couple of standard functions for a higher frequency, well, seriously! Okay, if they didn’t, we will.

PWM Frequency Setting Via Registers

The PWM generation is tuned through the timer registers. Then you will find some ready-made “pieces” of code, which you need to insert into setup(), and the PWM frequency will be reconfigured (the pre-delimiter and the timer mode will change). You can still work with the PWM signal with the analogWrite() function, controlling the filling of the PWM on the standard pins.

Changing the PWM Frequency on the ATmega328 (Arduino UNO/Nano/Pro Mini)

Pins D5 and D6 (Timer 0) – 8 bits

// Pins D5 and D6 are 62.5kHz
TCCR0B = 0b00000001; // x1
TCCR0A = 0b00000011; // fast pwm
// Pins D5 and D6 - 31.4 kHz
TCCR0B = 0b00000001; // x1
TCCR0A = 0b00000001; // phase correct
// Pins D5 and D6 - 7.8 kHz
TCCR0B = 0b00000010; // x8
TCCR0A = 0b00000011; // fast pwm
// Pins D5 and D6 - 4 kHz
TCCR0B = 0b00000010; // x8
TCCR0A = 0b00000001; // phase correct
// Pins D5 and D6 - 976 Hz - default
TCCR0B = 0b00000011; // x64
TCCR0A = 0b00000011; // fast pwm
// Pins D5 and D6 - 490 Hz
TCCR0B = 0b00000011; // x64
TCCR0A = 0b00000001; // phase correct
// Pins D5 and D6 - 244 Hz
TCCR0B = 0b00000100; // x256
TCCR0A = 0b00000011; // fast pwm
// Pins D5 and D6 - 122 Hz
TCCR0B = 0b00000100; // x256
TCCR0A = 0b00000001; // phase correct
// Pins D5 and D6 - 61 Hz
TCCR0B = 0b00000101; // x1024
TCCR0A = 0b00000011; // fast pwm
// Pins D5 and D6 - 30 Hz
TCCR0B = 0b00000101; // x1024
TCCR0A = 0b00000001; // phase correct

Pins D9 and D10 (Timer 1) – 8 bits

// Pins D9 and D10 - 62.5 kHz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00001001; // x1 fast pwm
// Pins D9 and D10 - 31.4 kHz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000001; // x1 phase correct
// Pins D9 and D10 - 7.8 kHz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00001010; // x8 fast pwm
// Pins D9 and D10 - 4 kHz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000010; // x8 phase correct
// Pins D9 and D10 - 976 Hz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00001011; // x64 fast pwm
// Pins D9 and D10 - 490 Hz - default
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000011; // x64 phase correct
// Pins D9 and D10 - 244 Hz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00001100; // x256 fast pwm
// Pins D9 and D10 - 122 Hz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000100; // x256 phase correct
// Pins D9 and D10 - 61 Hz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00001101; // x1024 fast pwm
// Pins D9 and D10 - 30 Hz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000101; // x1024 phase correct

Pins D9 and D10 (Timer 1) – 10 bit

// Pins D9 and D10 - 15.6 kHz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00001001; // x1 fast pwm
// Pins D9 and D10 - 7.8 kHz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00000001; // x1 phase correct
// Pins D9 and D10 - 2 kHz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00001010; // x8 fast pwm
// Pins D9 and D10 - 977 Hz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00000010; // x8 phase correct
// Pins D9 and D10 - 244 Hz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00001011; // x64 fast pwm
// Pins D9 and D10 - 122 Hz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00000011; // x64 phase correct
// Pins D9 and D10 - 61 Hz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00001100; // x256 fast pwm
// Pins D9 and D10 - 30 Hz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00000100; // x256 phase correct
// Pins D9 and D10 - 15 Hz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00001101; // x1024 fast pwm
// Pins D9 and D10 - 7.5 Hz 10bit
TCCR1A = 0b00000011; // 10bit
TCCR1B = 0b00000101; // x1024 phase correct

Pins D3 and D11 (Timer 2) – 8 bits

// Pins D3 and D11 - 62.5 kHz
TCCR2B = 0b00000001; // x1
TCCR2A = 0b00000011; // fast pwm
// Pins D3 and D11 - 31.4 kHz
TCCR2B = 0b00000001; // x1
TCCR2A = 0b00000001; // phase correct
// Pins D3 and D11 - 8 kHz
TCCR2B = 0b00000010; // x8
TCCR2A = 0b00000011; // fast pwm
// Pins D3 and D11 - 4 kHz
TCCR2B = 0b00000010; // x8
TCCR2A = 0b00000001; // phase correct
// Pins D3 and D11 - 2 kHz
TCCR2B = 0b00000011; // x32
TCCR2A = 0b00000011; // fast pwm
// Pins D3 and D11 - 980 Hz
TCCR2B = 0b00000011; // x32
TCCR2A = 0b00000001; // phase correct
// Pins D3 and D11 - 980 Hz
TCCR2B = 0b00000100; // x64
TCCR2A = 0b00000011; // fast pwm
// Pins D3 and D11 - 490 Hz - default
TCCR2B = 0b00000100; // x64
TCCR2A = 0b00000001; // phase correct
// Pins D3 and D11 - 490 Hz
TCCR2B = 0b00000101; // x128
TCCR2A = 0b00000011; // fast pwm
// Pins D3 and D11 - 245 Hz
TCCR2B = 0b00000101; // x128
TCCR2A = 0b00000001; // phase correct
// Pins D3 and D11 - 245 Hz
TCCR2B = 0b00000110; // x256
TCCR2A = 0b00000011; // fast pwm
// Pins D3 and D11 - 122 Hz
TCCR2B = 0b00000110; // x256
TCCR2A = 0b00000001; // phase correct
// Pins D3 and D11 - 60 Hz
TCCR2B = 0b00000111; // x1024
TCCR2A = 0b00000011; // fast pwm
// Pins D3 and D11 - 30 Hz
TCCR2B = 0b00000111; // x1024
TCCR2A = 0b00000001; // phase correct

Example of Usage

void setup() {
  // Pins D5 and D6 - 7.8 kHz
  TCCR0B = 0b00000010; // x8
  TCCR0A = 0b00000011; // fast pwm
  // Pins D3 and D11 - 62.5 kHz
  TCCR2B = 0b00000001; // x1
  TCCR2A = 0b00000011; // fast pwm
  // Pins D9 and D10 - 7.8 kHz 10bit
  TCCR1A = 0b00000011; // 10bit
  TCCR1B = 0b00000001; // x1 phase correct
  analogWrite(3, 15);
  analogWrite(5, 167);
  analogWrite(6, 241);
  analogWrite(9, 745); // yes, range 0-1023
  analogWrite(10, 345); // yes, range 0-1023
  analogWrite(11, 78);
}
void loop() {
}
Important!
If you change the frequency on pins D5 and D6, you will lose the time functions (millis(), delay(), pulseIn(), setTimeout(), etc.), they will not work correctly. Also, the libraries that use them will stop working!

If you really want or need an overclocked PWM on the system (zero) timer without loss of time functions, you can correct them as follows:

#define micros() (micros() >> CORRECT_CLOCK)
#define millis() (millis() >> CORRECT_CLOCK)

void fixDelay(uint32_t ms) {
  delay(ms << CORRECT_CLOCK);
}

Defines should be placed before plugging in the libraries so that they get into the code and substitute functions. The only thing is that you can not correct the delay in another library this way. You can use fixDelay() for yourself as written above.

The most important thing is CORRECT_CLOCK. This is an integer equal to the ratio of the default timer divider and the new one set (for PWM acceleration). For example, we set the PWM to 8 kHz. From the list above, we see that the default divider is 64, and 7.8 kHz will be 8, which is eight times smaller. CORRECT_CLOCK is set accordingly.

#define CORRECT_CLOCK 8
void fixDelay(uint32_t ms) {
  delay(ms << CORRECT_CLOCK);
}
void setup() {
  pinMode(13, 1);
  // Pins D5 and D6 - 4 kHz
  TCCR0B = 0b00000010; // x8
  TCCR0A = 0b00000001; // phase correct
}
void loop() {
  digitalWrite(13, !digitalRead(13));
  fixDelay(1000);
}

Related Video: Arduino PWM Tutorial #1 – How To Change PWM Frequency

Libraries for Working with PWM

In addition to fiddling with the registers manually, there are ready-made libraries that allow you to change the PWM frequency of the Arduino. Let’s take a look at some of them:

PWM library (GitHub) – a powerful library that allows you to change the PWM frequency on ATmega48 / 88 / 168 / 328 / 640 / 1280 / 1281 / 2560 / 2561 microcontrollers, of which 328 is on UNO/Nano/Mini and 2560 is an Arduino Mega.

  • Allows you to set any PWM frequency, pre-delay, TOP
  • Only one channel is available when working with 8-bit timers (for example, on the ATmega328, only D3, D5, D9, and D10)
  • Allows to work with 16-bit timers at a higher resolution (16 bits instead of the standard 8)
  • The library is very complicated, so it can’t be shredded into pieces.
  • See examples in the folder with the library!

GyverPWM library (GitHub) – the library, which we wrote together with my friend. The library allows very flexible work with PWM on microcontroller ATmega328 (we will add Mega later):

  • Allows you to set any PWM frequency in the range of 250 Hz – 200 kHz
  • Bit selection: 4-8 bits for 8-bit timers, 4-16 bits for 16-bit timers (at 4 bits, the PWM frequency is 1 MHz)
  • PWM mode selection: Fast PWM or Phase-correct PWM (favorable for motors)
  • Generation of meander frequencies from 2 Hz to 8 MHz on pin D9 with maximum accuracy
  • Only one channel is available when working with 8-bit timers (for example, on an ATmega328, only D3, D5, D9, and D10)
  • There are functions to reconfigure the standard PWM outputs without losing the PWM
  • The library is written in a very simple way, and you can take pieces of code from it
  • See examples in the folder with the library!