What is the exact frequency of PWM signals produced from Arduino pins

In http://arduino.cc/en/Reference/analogWrite it says,

" The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz. "

I want now to check that frequency. Simply I typed this command analogWrite(9,127), which will generate PWM signal with duty cycle 50%. then I used the pulseIn built-in Arduino function () to measre the period in which the signal is HIGH. It returns in average 1010 us, So the period time is 2020 (1010 for the High period and 1010 for the low period) and consequently the PWM frequency from that pin is 495.04 ~ 495Hz.

and the same is done with pin number 5. The High period is 506 uS, The full period is 1012 uS, The PWM frequency from that pin is 988.14 Hz ~ 988 Hz.

So what is the exact PWM frequency ?!

Attached is the sketch and the results

Sketch & Snap shots.zip (56.7 KB)

To get the exact frequency, measure the clock speed and divide by 32768.

To get the exact frequency, measure the clock speed and divide by 32768.

@OP,
Do you understand why AWOL said that ? Do you know what a Pre-scaler is ? Have you read the ATmega328 datasheet ?
If you're going to be asking questions like "what is the EXACT frequency of a PWM signal (that most people could care less what the frequency was ) then maybe you should take the time to read the 500 some odd page datasheet for the cpu chip. It will tell you that (and actually you could deduce it) that the analogWrite statement , which I believe is part of the arduino.h library but I'm not sure actually, is generating the PWM by configuring the Timer for PWM mode with a different pre-scaler for pins 9 & 10 than it is using for pins 5 & 6. What AWOL told you is that the prescaler for the pins with the 488 hz frequency is 32768.
Can you guess what the pre-scaler is for pins 9 & 10 ?

I agree about the zip file. If I've responded to his posts in the past it probably is a virus XD

The fundamental problem with the OP is, of course, defining "exactly" and using a device to measure itself, without external reference.
Imagine, for example, a clock with a pendulum that is 20mm too long.
Now, use that clock to measure one of its indicated hour.
"Exact"?

I don't think so.

Exactly....

Use a scope to measure the frequency.

Did you actually measure the actual high and low duration of the pulses, or did you just assume that the duty cycle that you asked for, was actually achieved ?

Did you notice the last sentence on the first webpage you linked to, at the top of the thread ?

The reason there are two different frequencies, is because the different output pins are connected to different timers, and the features of those timers are different, to meet the different needs that the timers might be used for.

If you want a specific frequency, then use some technique which is supposed to give you a specific frequency.

The analogWrite( ) capability is targetted neither as a frequency generator, nor it is intentionally PWM. It is a method of creating what appears to be an analog, controllable, voltage, which you need to filter with a low pass filter ( either electrically, or in your eye if you are driving leds ). The frequency is not important for this application, as long as it is faster than you see it blinking.

First of all, for your first question,Raschemmel. I was knowiny why AWOL said that but thanks for your time also.

Here is a new version of the question with a pictures of the sketch and the results instead of the zip file:

My target of my question (as I think it seems trivial to many persons in this conversation especially raschemmel :D) is to measure the duty cycle of any PWM signal.

For example, I have a motor attached to arduino uno on pin no (9). and the motor’s speed is changing due to some reasons. So after some periodic interval of time, I want to measure its speed to make some processing on it and then change it.

So, to read the duty cycle of any PWM_Signal (I mean the argument of the analogWrite function) I want to know the frequency of the pwm signal produced from this pin and the time in which the signal will be HIGH or LOW.

For example if the speed of the motor changed to 25% of its speed. and I want to measure its speed (or the duty cycle of the PWM wave produced due to this speed). So, I will do something like that. As the frequency of the PWM wave produced from pin number 9 is 488Hz as AWOL said. So, the period of this wave is 2048 microseconds and hence the half period time = 1024 microseconds.

If, 1024 microseconds comes from analogWrite(127)
Then, pulseIn(9,HIGH) comes from analogWrite(?)

So the value of analogWrite now (which is the speed of the motor now) will equal to (pulseIn(9,HIGH) * 127 / 1024) which is must equal to 64 (25% duty cycle).

The question now is 1024 microseconds is from theory (as the frequency equal to 488 Hz) but when I measured it using analogWrite(9,127) and then pulseIn(9,HIGH) it gives 1010 microseconds and not 1024 microseconds.

That’s why I asked that question!

So, to read the duty cycle of any PWM_Signal (I mean the argument of the analogWrite function) I want to know the frequency of the pwm signal produced from this pin and the time in which the signal will be HIGH or LOW.

Use the pulseIn() function twice, once to measure the on time and the second to measure the off time, saved into two different variables. That gives you the variables need to compute duty cycle by simple division. Then add the two values together and compute the reciprocal to get the switching frequency.

http://arduino.cc/en/Reference/PulseIn

. . . and remember that the timer has a granularity of 4us.

I think the timer has more granularity than 4us

As when I made analogWrite(9,127) and then pulseIn(9,HIGH) … it gives in average 1010 us, instead of 1024 us. (as the frequency equal to 488Hz).

Snapshot of the sketch

Case of pin (9) which produces PWM wave with frequency 488Hz

Case of pin (5) which produces PWM wave with frequency 980Hz

So, you don't get the answer you expect, and you conclude the pwm generation is faulty, and the pulsein function which you used to measure it, is perfectly accurate ?

My presumption would be the exact opposite of yours. I'd assume the pwm was as specified, and the pulsein function is not particularly accurate. Because of the granularity of the timer, and also because of the slightly uncertain timing of the perceived change from low to high , at the leading and trailing edges of the pulses.

Also there is an assumption that the chip's 16 MHz is running at exactly 16,000,000 Hz, where in reality there is always some accuracy variation, up to 200 PPM (+/- 3.2KHz) using ceramic resonators.

Exactly is such a awkward term to deal with let alone attain. :wink: