Hello, I'm trying to control four dc motors with 20khz PWM. The PWM pins i'm using are 5,6,9,11
I'm following this guy's post
I'm currently changing PWM in timer 1,2,3,4 to 4Khz (or at least that's what i think i'm doing)
int erase = 7; // this is 111 in binary and is used as an eraser
TCCR1B &= ~erase;
TCCR2B &= ~erase; // this operation (AND plus NOT), set the three bits in TCCR2B to 0
TCCR3B &= ~erase;
TCCR4B &= ~erase;
int prescaler = 2; // this could be a number in [1 , 6]. In this case, 3 corresponds in binary to 011.
TCCR1B |= prescaler;
TCCR2B |= prescaler; //this operation (OR), replaces the last three bits in TCCR2B with our new value 011
TCCR3B |= prescaler;
TCCR4B |= prescaler;
The prescaler values that he mentioned are
prescaler = 1 ---> PWM frequency is 31000 Hz
prescaler = 2 ---> PWM frequency is 4000 Hz
prescaler = 3 ---> PWM frequency is 490 Hz (default value)
prescaler = 4 ---> PWM frequency is 120 Hz
prescaler = 5 ---> PWM frequency is 30 Hz
prescaler = 6 ---> PWM frequency is <20 Hz
but the problem is prescaler 2 that i'm using is too little 4khz and prescaler 1 is too much 31Khz my controller supports up to 25khz
So what should I do to get 20Khz to 25Khz ?
I also noticed that when changing the frequency the scale from 0 to 255 in analogWrite gives a different speed is that behavior normal ?