22

According to the Arduino reference for analogWrite(), the PWM frequency on most pins is ~490 Hz. However, it's ~980 Hz for pins 5 and 6 on the Uno, and for pins 3 and 11 on the Leonardo.

Why are these different? Is it a deliberate design feature, or is it somehow dictated by the hardware?

2 Answers 2

24

Those aren't the only frequencies available for the PWM signals. However, they are the frequencies as determined by the applied prescaler (which you can readily change as detailed below).

Each of the 3 pairs of PWM pins is tied to one timer, each of which has its own base frequency, as follows:

  • Pins 5 and 6 are paired on timer0, with base frequency of 62500Hz
  • Pins 9 and 10 are paired on timer1, with base frequency of 31250Hz
  • Pins 3 and 11 are paired on timer2, with base frequency of 31250Hz

Then each set of pins have a number of prescaler values that can be chosen, that will divide the base frequency of that pair of pins. The prescaler values available are:

  • Pins 5 and 6 have prescaler values of 1, 8, 64, 256, and 1024
  • Pins 9 and 10 have prescaler values of 1, 8, 64, 256, and 1024
  • Pins 3 and 11 have prescaler values of 1, 8, 32, 64, 128, 256, and 1024

The different combinations yield different frequencies in a given PWM pin. Notice that timer 2 (tied to pins 3 and 11) have more prescaler values available, resulting in more frequencies available.

Now, why timer 2 is different, that's a separate question.

Edit: Here's a list of possible PWM frequencies per pin (from this article):

For pins 6 and 5 (OC0A and OC0B):

  • If TCCR0B = xxxxx001, frequency is 64kHz
  • If TCCR0B = xxxxx010, frequency is 8 kHz
  • If TCCR0B = xxxxx011, frequency is 1kHz (this is the default from the Diecimila bootloader)
  • If TCCR0B = xxxxx100, frequency is 250Hz
  • If TCCR0B = xxxxx101, frequency is 62.5 Hz

For pins 9, 10, 11 and 3 (OC1A, OC1B, OC2A, OC2B):

  • If TCCRnB = xxxxx001, frequency is 32kHz
  • If TCCRnB = xxxxx010, frequency is 4 kHz
  • If TCCRnB = xxxxx011, frequency is 500Hz (this is the default from the Diecimila bootloader)
  • If TCCRnB = xxxxx100, frequency is 125Hz
  • If TCCRnB = xxxxx101, frequency is 31.25 Hz

TCCRnB is where you set the prescaler bits for timer n, replacing n by 0, 1 or 2, depending on the timer you want to set. If you are still unsure about bitwise operations, read this bit math tutorial.

My sources:

Note that there seems to be divergence in those sources about whether pins 9 and 10 have the same behavior as 5 and 6 or 3 and 11, but you get the idea anyway. I'm reading the datashet to try and figure out which is correct, or whether this is a difference between boards.

6
  • 1
    What ATmega does this answer refer to? I didn't check, but I bet it slightly differs per controller.
    – jippie
    Feb 14, 2014 at 18:16
  • @jippie Good point! The sources refer to ATmega168 and 328.
    – Ricardo
    Feb 14, 2014 at 18:22
  • @jippie Just to clarify, in my answer, the pin numbers refer to how they are assigner on the Uno board (pin 1 means Digital Pin 1, or D1, for example), not the IC (ATmega328 pin 1 is reset).
    – Ricardo
    Feb 14, 2014 at 19:05
  • 1
    I believe those details change too with boards, Uno, Duemilanove, Mega, ...
    – jippie
    Feb 14, 2014 at 19:41
  • @jippie Definitely, yes.
    – Ricardo
    Feb 14, 2014 at 22:31
8

I am not aware of the design considerations, but if you check the datasheet for the microcontroller on your Arduino, you will notice that PWM pins are grouped together and per group connected to a timer. The speed at which this timer is increased varies by the configured prescaler. If you change the prescaler for a certain timer, you change the PWM frequency for the related PWM pins. I believe some timers double for other purposes like the millis(); function. If you change the prescaler for that timer, values returned by millis() will be off by the same factor.

You can calculate the setting for the prescaler as follows:

$$\text{prescaler} = \dfrac{f_{CPU}}{PWMresolution × f_{PWM}} = \dfrac{16\text{MHz}}{256×490} \approx 128$$

prescaler = f[CPU]/(PWMresolution×f[PWM]) = 16000000/(256×490) = approx 128.

Check the datasheet and you'll find that 128 is indeed one of the prescaler values you can select.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.