Temperature controlled fan - nano- solved

Looking for some guidance please.

I have a 4 wire 12v pwm fan, Arduino nano, appropriate power supplies, DHT11, 16x2lcd i2c

Everything is connected and works well except the fan, the pwm wire of the fan is connected to pin 9, grounds are all connected.

When I start everything up, the fan starts up, The speed increments seem to work ok except the fan runs slow when it should be off, display shows off

I have read others having the same problem and it's stated I have to change the PWM frequency to 25khz to get the fan to stop.

I have been reading and can't seem to get my head around how to implement and what code to add/change.

Any help would be appreciated.

#include<dht.h>      // Including library for dht
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,16,2) for 16x2 LCD.


#define dht_dpin 6 
dht DHT;

#define pwm 9

byte degree[8] = 
             {
               0b00011,
               0b00011,
               0b00000,
               0b00000,
               0b00000,
               0b00000,
               0b00000,
               0b00000
             };

void setup()
{

// Initiate the LCD:
 lcd.init();
 lcd.backlight();
 
 

lcd.createChar(1, degree);

}

void loop()
{
 DHT.read11(dht_dpin);
 int temp=DHT.temperature;
 int hum=DHT.humidity;
 lcd.setCursor(0,0);
 lcd.print("Temp:");
 lcd.print(temp);   // Printing temperature on LCD
 lcd.setCursor(7,0);
 lcd.write(1);
 lcd.print("C H:");
 lcd.print(hum);
 lcd.setCursor(14,0);
 lcd.print("%");
 
 lcd.setCursor(0,1);
 if(temp <26 )
   { 
     analogWrite(pwm,0);
     lcd.print("Fan OFF            ");
     delay(100);
   }
   
   else if(temp==26)
   {
     analogWrite(pwm, 51);
     lcd.print("Fan Speed: 20%   ");
     delay(100);
   }
   
    else if(temp==27)
   {
     analogWrite(pwm, 102);
     lcd.print("Fan Speed: 40%   ");
     delay(100);
   }
   
    else if(temp==28)
   {
     analogWrite(pwm, 153);
     lcd.print("Fan Speed: 60%   ");
     delay(100);
   }
   
   else if(temp==29)
   {
     analogWrite(pwm, 204);
     lcd.print("Fan Speed: 80%    ");
     delay(100);
   }
    else if(temp>29)
   {
     analogWrite(pwm, 255);
     lcd.print("Fan Speed: 100%   ");
     delay(100);
   } 
 delay(3000);
}

Welcome to the Forum. Please read these two posts in the programming questions forum:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

The forum link above has guidelines for posting in a standard way that makes it easiest for people to provide you with useful answers. Making an effort to do this will greatly increase the number and quality of helpful responses that you get.

In this case, the problem is that you have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Thank you, Fixed

squiggy6772:
the fan runs slow when it should be off....

You are saying that when you see "Fan OFF" on the LCD, the fan is still running?

Just to be sure, why do you do this:

#define pwm 9

but then later you do this:

     analogWrite(9,0);

PaulRB:
You are saying that when you see "Fan OFF" on the LCD, the fan is still running?

Correct.

aarg:
Just to be sure, why do you do this:

#define pwm 9

but then later you do this:

     analogWrite(9,0);

redundant statement,
change

  analogWrite(9,0);

to

     analogWrite(pwm,0);

Try disconnecting the fan's speed control pin from the Arduino and connect it to 5V, then ground and then unconnected. What happens in each case?

I am unsure about the advice to use 25KHz, I don't see why that would do anything to prevent slow running when supposed to be off. The usual reason for using 25KHz is to avoid audible noise from the fan which can be caused by lower frequency pwm (25KHz being above human hearing range). After all, at 0% duty cycle, the frequency of a pwm signal is irrelevant, it doesn't have one, it's a steady 0V.

Solved :

From my understanding and reading that's what others did to get the fan to stop.
A Uno and other boards it's easier to control the frequency of the pwm pin..
I scrapped the idea of using a pwm fan and redid the circuit to use a TIP120 and diode to control the fan speed.

Everything works as it should.