How to stop DC PWM fan with Arduino uno and temperature sensor

Dear all !
I am a new user of Arduino and I have this problem: my project consists on controlling a PWM DC fan ( 4-wires) with a temperature sensor the problem is that I want to turn off the FAN but I could not do that correctly:

my conxion is the following :

4-wires fan :
Black (GND) ==> GND (12 v adapter ) & GND (arduino)
yellow (12 v) ==> 12 v of the external alimentation source
Blue ( RMP ) ==> pin 9 (arduino)
green( sense ) ==> pin 2 (arduino) & both are connected to 10kohm pulpup resistor ==> 5v (arduino)

the sensor is working correctly without no problems

here is my code :

#include <PWM.h> 
int pwmVal   = 0;
int DEBUG    = 1; // DEBUG counter; if set to 1, will write values back via serial
void setup() {
  Wire.begin(); // start the I2C library // for the sensor 
  Serial.begin(9600); //Start serial communication at 115200 baud
 InitTimersSafe(); //not sure what this is for, but I think i need it for PWM control?
  bool success = SetPinFrequencySafe(9, 25000); //set frequency to 25kHz
  pinMode(2,INPUT); //set RPM pin to digital input the sensor pin of the fan green color 
 if (DEBUG) {
   Serial.begin(9600);} 
}


// Main program
void loop() {
  float temp =getTemp102(); // to get the temperature from the sensor 
  //analogWrite(pwmPin, pwmVal);
  pwmWrite(9, 5); // 51=20% duty cycle, 255=100% duty cycle
     if (DEBUG) { // If we want to read the output
      if (temp > 19 ) { 
        if (pwmVal != 255) {
         pwmVal += 51; // increase the speed  // // 51=20% duty cycle, 255=100% duty cycle
         Serial.print("indoor temperature is : ");
         Serial.print("\t"); 
         Serial.println(temp); 
         Serial.println ( "fan speed  is: ");
         Serial.print(pwmVal);  // Print red value
         Serial.print("\t");
         pwmWrite(9, pwmVal); // 51=20% duty cycle, 255=100% duty cycle
        
         }    // Print a tab
         else  { 
         Serial.println("at max high");  // Print red value
         pwmWrite(9, 255);
         }

the part of code related to the sensor is working correctly so I did not mentioned it while my main

problem is how to to trun off the fan. In fact, I could do this if I write this :

pwmWrite(9, 5); but normally it must be like this : pwmWrite(9, 0); ( the correct instruction )
can any of you help me in this

Thank you in advance !

Hi,

I could do this if I write this :

pwmWrite(9, 5); but normally it must be like this : pwmWrite(9, 0); ( the correct instruction )

I'm not clear about your problem. Are you saying that pwmWrite(9,0) does not stop the fan running but pwmWrite(9,5) stops it?

Why are you using this library instead of the standard analogWrite() function?

yes excatly!!!!
it didnot work with analogWrite() because the pin has another frequency while to control or communicate with fan PWM you need 25 KHz as frequency of pin and already doen with this library

You have tested with analogWrite() and the fan did not spin? Or did it spin but make a loud sound?

This library you are using. It it set to 25KHz by default? Is that frequency chosen to be outside human hearing range?

I tried with anlogueWrite() but no thing happen, I do not know but in order to communicate with the fan I need this frequency it was written in one article on the net.

badiamak:
in order to communicate with the fan I need this frequency

Fans don't "communicate", they are pretty dumb.

badiamak:
it was written in one article on the net.

That's 1% more reliable than "a guy in a bar told me". Do you have the link?

It's analogWrite(), not analogueWrite(), and not all Arduino pins are capable of PWM (most boards have marked the ones that can do PWM with a ~ next to the pin designation, or look up the analogWrite() documentation page).