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);
}