25Khz PWM with digitalWrite...

Hi,
I thought I would be able to very quickly set up a GPIO-driven PWM controller for a fan using something like this...

int PWMOUT = 10;

void setup()
{
 pinMode(PWMOUT, OUTPUT);  // PWMOUT pin as output
}

void loop()
{
 while(1) { 
   digitalWrite(PWMOUT,HIGH);
   delayMicroseconds(20);
   digitalWrite(PWMOUT,LOW);
   delayMicroseconds(20);
 }
}

Then just modify the two delay values to achieve the required duty cycle.
The fan requires a 25Khz input so I can't use the PWM/servo library directly.

At first glance it worked just fine, but looking at the signal with a scope it
has a lot of jitter (frequency can vary from about 20-25Khz).
I'm using a ProMicro 5V/16Mhz from Sparkfun.
It runs with a MEGA32U4, haven't dug into the datasheet yet...
Any thoughts?

Hi,

The fan motor probably is not highly sensitive.

Maybe try the available 31372.55 Hz PWM

See: Arduino-PWM-Frequency - ArduinoInfo for all the possible settings..

Have fun digging into the 32U4 datasheet. You'll see that the timer/counters are different than with the 328P. If you need the delay() function, you'll need to find which timer/counter is being used on 32U4, then select another timer/counter to use, while adjusting its mode and frequency for 25KHz PWM.

Might be quicker to just use a Pro Mini instead, then use the sample code in link provided by teryking228.

edsut:
The fan requires a 25Khz input so I can't use the PWM/servo library directly.

Computer fan ?

The 4-pin computer fans I tries worked fine on the default PWM frequency.
But you could have some whining noise at lower speeds. Just try it.
Leo..