I have an Arduino Micro and I am controlling a motor with PWM but the 490hz frequency of the normal PWM can be heard coming from my motor and through my speakers (I have an MP3 module attached to my board as well).
I need to change one of the timers, and I'd rather it not be timer 0 because that would mess up millsecs(), or timer 4 because I may wish to switch to using a Pro Mini instead of the Micro. And since the Micro doesn't have a Tmer 2, that leaves Timer 3 or Timer 1.
Here are the pin mappings for the timers on the Micro:
TIMER0B, /* 3 /
TIMER3A, / 5 /
TIMER4D, / 6 /
TIMER1A, / 9 /
TIMER1B, / 10 /
TIMER0A, / 11 /
TIMER4A, / 13 */
There's a few things I'm confused about at this point.
First, if I set a prescalar of 1024 then that gives 16000000 / 1024 = 15625. Now does that mean my max PWM frequency will be 15625? In other words, will be pin be toggled 15625 a second? Or, will it actually be toggled at half that speed? I'm thinking it's half, because you can apparently set a prescalar of 1 on some timers and obviously the microcontroller can't toggle a pin on and off in the same cycle. So that would mean my max PWM frequency would be 7812 hz which is too low.
I guess one option to is to use the 256 prescalar, but then my max frequency is gonna be 31250 hz. If I set the output compare A I guess I can halve that to get 15625. But that brings me to the second thing I'm confused about.
I believe I can't vary the PWM frequency on pins marked with an A if I want to have that finer control of my output frequency using the compare A. Is that correct?
If so, that would seem to leave me with only one option, if I want a PWM frequency of 15625 hz... use TIMER1B on Pin 10.
But is there any real downside to using a PWM frequency of 31250 hz with my motor? It seems that since 15625 hz is still within range of our hearing that if I use that I may still be able to hear the noise on my speakers even if the motor doesn't make a sound at that frequency. And if I use the higher frequency will that then allow me to use pin 5 (TIMER3A) and 9 (TIMER1A)?
And how do I go about setting this anyway? I mean if I just change the prescalar can I just use the standard PWM functions? Or are those limited in precision in some way and I should just set the compare B directly to adjust the frequency? It seems timers 1 and 3 are 16bit on the Micro, but I think the pwm functions only use 8 bits of precision? I guess 8 bits is sufficient, I only need to run the motor at a minimum speed of 1/256.