How to eliminate fan noise.

Hi!
I have a fan controlled by fast PWM 37kHz Attiny13a. Its speed depends on the temperature. It has three ranges. First is 0-50C - the fan is idle, 50-65C - fan’s speed depends on the temperature, more than 65C - fan rotates on maximum speed.

It works fine in the second range, but when it switches between ranges it starts making noise. it seems that it changes speed on audible frequencies. Here is my code:

int lm35;
int value;
boolean fullSpeed = false;
boolean fanStop = true;

void setup()
{
 TCCR0A =  2<<COM0A0 |   3<<WGM00 ;// clear 0c0a at compare match //enable fast pwm
 TCCR0B =  1<<CS00; //enable fast pwm mode // not to use prescaler


}

void loop() {
            
     analogWrite(0, map(analogRead(2), 0, 1023, 210, 0));
     int dummy;
     dummy = analogRead(3);
      lm35 = analogRead(3);
    
    if (lm35>95) {
        fullSpeed = true;
    } 
    else if (lm35<88) {
      fullSpeed = false;
    }
    else if (lm35<=78) {
      fanStop = true; 
    }
    else if (lm35 >= 85){
      fanStop = false;
    }
    
   if (fullSpeed){
      
      analogWrite( 1, 255);
    
    }
    else if (fanStop){
      analogWrite( 1, 0); 
    }
    
    else  {
        value = (182*((lm35-85)/10))+73;  
      
      analogWrite( 1, value);
     
    }
}

I’ve tried to make with booleans, so to make the range of switching wider trying to eliminate possible noise.
But it doesn’t work.

Post a link to the fan product page or data sheet.

Some, especially brushless fans, are not designed for PWM speed modulation.

This one.

Of course I'm doing it with the help of transistor.

There is no useful information on the page you linked.

Of course I’m doing it with the help of transistor.

Then why did you not post a circuit diagram as well?

Please read and follow the directions in the “How to use this forum” post.

I’ve just measured the PWM frequency on my fan pin it is about 500Hz, while on another pin controlling the led dimmer it is 37kHz. Seems, I do something wrong in my code.

int lm35;
int value;
boolean fullSpeed = false;
boolean fanStop = true;

void setup()
{
  analogWrite(0,210);
     
 TCCR0A =  2<<COM0A0 |   3<<WGM00 ;// clear 0c0a at compare match //enable fast pwm
 TCCR0B =  1<<CS00; //enable fast pwm mode // not to use prescaler

 

}

void loop() {
      
      
      analogWrite(0, map(analogRead(2), 0, 1023, 210, 0));
      int dummy;
       
      dummy = analogRead(3);
      //delay(5);
      lm35 = analogRead(3);
   

    
    
   

     if (lm35>85) {
       int value = (187*((lm35-85)/10))+68;  
        if (value >=239){
        analogWrite( 1, value);
        }
        else {
        analogWrite( 1, 239);
        }
    }
    else {
      
      analogWrite( 1, 0); 
    }
 
}

How can I have fast PWM 37Khz on both pins?

Seems, that PWM is fine. I’ve tested it with pot. But something reduces the fan PWM in code when it works from LM35.

Here is the schematics:

Well, I've just decided to change the code and take LM35 value one time per 1000 loops.