With googling around, I found that the default PWM frequency of an Arduino Uno PWM pin D11 is around 500 Hz.
I also found that the default divider is 64 and that PWM- frequency is Clock-speed divided by the divider.
But, for an Arduino Uno (16 MHz) I than calculate 250 kHz ( = 16 MHz / 64).
Am I making a mistake somewhere? (I need a minimum PWM-frequency of 10 KHz, in order to convert PWM-signal to absolute voltage using a D-A-convertor.)
With googling around, I found that the default PWM frequency of an Arduino Uno PWM pin D11 is around 500 Hz.
I also found that the default divider is 64 and that PWM- frequency is Clock-speed divided by the divider.
But, for an Arduino Uno (16 MHz) I than calculate 250 kHz ( = 16 MHz / 64).
Am I making a mistake somewhere?
With best regards,
Leo
Arduino UNO PWM pins operate in one out of two PWM modes (epending on the internal hardware timer used):
Either "fast PWM mode" or "phase-correct PWM mode"
The default PWM frequency calculation for phase-correct PWM with an UNO is:
: 16 MHz / 64 / 255 / 2 = 490.196Hz
The default PWM frequency calculation for"fast PWM" with an UNO is:
16 MHz / 64 / 256 = 976.5625Hz
The dividers 255 (phase-correct PWM) or 256 (fast PWM) are caused by the fact, that it is 8-bit PWM which we are talking about. The Atmega328 is a 8-bit microcontroller.
Whether the default PWM frequency on an UNOs PWM output is actually 490.196Hz or 976.5625Hz simply depends on the PWM pin number used by default.
Higher PWM output frequencies would require mangling with internal timer registers of the Atmega328 in your sketch.
PWM frequencies can be much more speedy by factor 2, 4,8 or 16 I think, if you manipulate some divider registers in a different way.
But it is NOT POSSIBLE to get every PWM frequency you might want.
You could get possibly 15625 Hz (15.625 kHz), but you cannot get 10000 Hz (10 kHz) exactly as a PWM frequency.
The factor ..../256/2 was never mentioned in none of the posts I've checked. And indeed, as you showed: it comes to 490 Hz.
Re 10 kHz: I do understand that I will not get 10kHz exactly. I phrased it wrong in my post; I should have said around 10kHz. But I now see that 4kHz is acceptable to have my laser firing up in 5 mSec's, with a 0.15V variance. That is acceptable.
Terrykings' paper shows that not all dividers, such as divider 4, are applicable; this is pin dependant; see Terry's table attached.
Thanks TERRYKING228,
Thanks for your table.
It initially confused me, but now it is more oe less clear. The numbers for sure.
But, I do have a question: If I translate the Binary number to Numerical, I see no direct relationship in divider and divider. Am I thinking too digital? (Mind you: I am a retired Directional Drliler, normally drilling for Oil and Gas, with a ChemEng background.)
It's possible to get any number of odd PWM frequencies, like 10kHz, by configuring a timer accordingly.
First comes the timer prescaler, that divides the system clock by a power of 2, up to 2^10=1024. If you set the prescaler to 1:16, you'll get 1MHz from a 16MHz clock frequency, or one period every microsecond. Not all prescaler factors can be programmed, though, see the control register descriptions for the various timers.
Next some timers can be configured to count up to a specific MAX value. If you want 10kHz, equivalent to 100µs, and get 1µs clock from the 1:16 prescaler, you set e.g. the ICR (input compare register) to 99. Then the timer counts continuously from 0 to 99, equivalent to 100 steps or 100µs or 10kHz.
Finally you set an OCR (output compare register) to the desired PWM duty cycle. This value must not exceed MAX, so that it compares equal once, whenever the timer counts up or down. In fast PWM mode the output pin, related to the selected OCR, is set on count zero (BOTTOM), and cleared on a compare match. In other PWM modes it counts up to MAX, then again down to BOTTOM, and toggles the output pin on every compare match.
Please note that every timer of an Uno has 2 OCR registers (Mega has 3), for 2 PWM outputs. I.e. when you program a timer for 10kHz PWM, both of its PWM output pins work at 10kHz, with an according compare match range.
But, I just found that changing the timer may also have an effect on other time-controlled parameters. (I use Delays() in the script and operate Servo's.)
I assume that when I power off the Uno, all defaults will be restored at restart?
An Uno has 3 timers, from which T0 is used for timekeeping and consequently should never be adjusted. Some libraries also need a timer, running out of timers is a common problem.
Put your initial settings into setup(), which is called once after a restart.