27

What frequency do Arduinos use for normal PWM when you use analogWrite()?

And is it different for different Arduino model? I'm interested specifically in the Mega 2560, but also want to know if it's consistent between models.

I've seen a passing references to the Arduino using 500 Hz, which seems really slow.

1
  • That also depends on the pin you are using.
    – Gerben
    Commented Jul 5, 2014 at 17:33

1 Answer 1

37

The PWM signal is generated by timers on the AVR chips. Each timer can generate a PWM signal on two or three different pins. Each pin can have it's own duty cycle, but they share the PWM frequency. You can change the frequency of the PWM by changing the clock source for the timers. By default they use the CPU clock divided by 64, ie. they have their prescaler set to 64 by Arduino initialization code. To confuse things more there are two different PWM modes: fast PWM and phase correct PWM. In fast PWM the timer counts to 255, then overflows and starts over from 0 (256 different states). In phase correct PWM the timer counts to 255, then changes direction and counts downwards to zero, changes direction and so on (510 different states).

The Arduino Mega has 5 timers, timer0 - timer4. Because timer0 is also used for the millis and micros functions it uses fast PWM, whereas the other timers are configured for phase correct PWM. This results in different frequencies on different pins:

  • Pin 4 and 13, controlled by timer0:
    16 Mhz / 64 / 256 = 976.56 Hz

  • Other PWM pins, controlled by timer1-4:
    16 Mhz / 64 / 510 = 490.20 Hz

The calculation is: Clock / Prescaler / PWM mode number of states

The situation is the same for all other Arduino boards I know of, except they have less timers that connect to different pins.

You can change the PWM frequency by changing the timer prescaler. See this page: http://playground.arduino.cc/Main/TimerPWMCheatsheet

It is also possible to make the timers count to a different value than 255. On the 8 bit timers you will loose an output pin, but on the 16 bit timers you can use the Input Capture Register to define a TOP value. The input capture functionality is a feature rarely used in the Arduino community so you likely won't miss it.

The Arduino libraries only allow you to use 8 bit resolution, even on the 16 bit timers. If you want the higher resolution, you will have to write your own analogWrite, or use a library made for the purpose. On the Arduino Mega timer0 and timer2 are 8 bit, while the rest are 16 bit.

By changing the mode of the 16 bit timers to make use of the full resolution, in combination with changing the prescaler and the TOP value, you can achieve a very wide range of PWM frequencies.

The maximum frequency you can achieve is clock / 4, by setting the prescaler to 1 and TOP to 3 in fast PWM mode - a lower value isn't allowed. This will give you a 4 MHz PWM with 2 bit resolution. It can be on 0%, 25%, 50%, or 75% of the time. A higher TOP value will give you a higher resolution at a lower frequency.

For a longer explanation read this article or refer to the datasheet.

Changing the prescaler, PWM mode, or TOP value for timer0 will mess with millis() and micros().

4
  • 1
    I gather, then, that the fastest frequency you could get for PWM is 16Mhz/256 (prescaler = 1), or 62.5Khz?
    – Duncan C
    Commented Jul 6, 2014 at 14:42
  • And further that the PWM frequency has to be an even factor of 62.5KHz? Somebody on another thread wanted to control the fan speed of a 4-pin PC fan. Apparently it wants a PWM control signal of 25KHz. 62.5/25 is 2.5, which is not an integer value.
    – Duncan C
    Commented Jul 6, 2014 at 14:54
  • I updated the answer to elaborate more on the possibilities of achieving specific frequencies,
    – user2973
    Commented Jul 6, 2014 at 16:55
  • On the low frequency end, you could prescale a 16 bit timer in phase-correct mode by a 1024 prescaler to get 16e6/1024/2^16/2 = 0.119Hz, or 7.1 cycles/minute. For lower than that, one would need to toggle the bits based on something like millis(). Or maybe trigger the counter on the external input pin T?.
    – Dave X
    Commented Nov 23, 2015 at 4:41

Your Answer

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

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