Go Down

Topic: Feature request: Adjustable PWM frequency (Read 20 times) previous topic - next topic

koyaanisqatsi

I'm posting this here because I'm _hoping_ it's not a hardware limitation.  ;)

It would be very useful if the frequency of the PWM output could be set in the software.  I have run across two scenarios that would be better handled by a frequency other than around 500 Hz.

In the case of using PWM on a high-power LED, the 500 Hz PWM is OK only if the LED is not moving.  In my case, I'm making an LED headlight for my bicycle and 500 Hz makes the ride very psychedelic and strains the eyes.  If I could set the PWM frequency to 2K-5KHz, it would be much better

At the other end, controlling the speed of a fan is better suited by a PWM frequency down around 10-30 Hz so that it's at the end or below the audible range.  Many fans and other mechanical devices vibrate as a result of the PWM signal because it's a very intense pulse.

Thanks!
What about elevensies? Luncheon? Afternoon tea? Dinner? Supper?

retrolefty

Having looked at the processor's data sheet, the PWM frequency is not a fixed value, but rather the results of values placed in the various timers parameters registers.

So I guess the PWM frequency could be changed but would require writing functions to replace or used instead of the standard PWM commands. There may be some other software limitation or interaction with other functions that utilize the times but I'm not software competent enough to undertake the analysis.

I agree it would be a nice addition to add to the tool box.

Lefty

halley

There's no friendly setAnalogFrequency() kind of function included in the standard library, but it's a pretty straightforward task.

A google search for "arduino set pwm frequency" will find a few useful threads on this site and on other sites like uchobby.

I think one reason for excluding the function is because there are some subtle interplay that you should be familiar with, before you start messing with these things.

For example, there are six PWM-capable output pins, and each of the six outputs has their own PWM-comparator to adjust the duty cycle of that pin.  But there are only three actual timers, so pins in pairs, 3/11, and 5/6, and 9/10, each pair shares a timer.  Adjust the frequency of pin 3, and pin 11 also gets the new shared frequency.

retrolefty

Quote
For example, there are six PWM-capable output pins, and each of the six outputs has their own PWM-comparator to adjust the duty cycle of that pin.  But there are only three actual timers, so pins in pairs, 3/11, and 5/6, and 9/10, each pair shares a timer.  Adjust the frequency of pin 3, and pin 11 also gets the new shared frequency.


Also isn't one of the three timers a 16 bit Vs 8 bit for the other two? Does that mean it might also be possible to have a greater 'duty' cycle resolution then the 8 bit resolution that the PWM command now offers?

Lefty


koyaanisqatsi

With an 8-bit resolution and a clock speed of 16Mhz, the theoretical max frequency of the PWM output would be around 64Khz  (16Mhz/256 steps) and at 16-bit, it would be about 256Hz (16MHz/65536 steps)  So if you wanted higher res, you would have to sacrifice frequency, which may actually work to an advantage.  And the converse is also true.  There's probably some processing overhead that I'm not considering.

It also seems like you would want (need/require) a frequency that is somewhat evenly divisible by either 256 or 65536 in order to avoid aliasing and rounding. (?)

I'm going to look around for info on this as halley suggested.  In the project I'm doing now, I only need one or two PWM outputs, so hacking it may be an option for me.  :)
What about elevensies? Luncheon? Afternoon tea? Dinner? Supper?

Go Up