enquiry on fan control with temperature sensor DHT 11

Hi, i’m new to this arduino and really hope there is someone able to help, i’m wanted to create a fan control for an enclosure, if the temperature reach 55 degree celsius the fan will turn on 255, when it lower then 55 degree celsius it would turn off.

the code i’m making is like this, hope someone can correct me if i’m wrong:

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7
float temp;
int tempPin = 7; //arduino pin used for temperature sensor
int tempMin = 25; // the temperature to start the buzzer
int tempMax = 70;
int fan = 6; // the pin where fan is connected
int fanSpeed = 0;

void setup() {
pinMode(fan, OUTPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
}
void loop() {
temp = analogRead(tempPin);
temp = (temp 5.0100.0)/1024.0;
Serial.println(temp);
delay(1000); // delay in between reads for stability
if(temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if((temp >= tempMin) && (temp <= tempMax)) //if temperature is higher than the minimmum range
{
fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
}

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7
float temp; 
int tempPin = 7; //arduino pin used for temperature sensor
int tempMin = 25;   // the temperature to start the buzzer
int tempMax = 70;   
int fan = 6;       // the pin where fan is connected
int fanSpeed = 0;

void setup() {
 pinMode(fan, OUTPUT);
 pinMode(tempPin, INPUT);
 Serial.begin(9600);
}
void loop() {
  temp = analogRead(tempPin);
  temp = (temp *5.0*100.0)/1024.0; 
  Serial.println(temp);
  delay(1000);        // delay in between reads for stability
  if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);
   } 
   if((temp >= tempMin) && (temp <= tempMax))  //if temperature is higher than the minimmum range
   { 
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
}

thanks Jim,

if i wanted to change the temp to start the fan only after it reach 55 then i just change the tempmin from 25 to 55 right??

somethings like this will it work??

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7
float temp;
int tempPin = 7; //arduino pin used for temperature sensor
int tempMin = 55; // the temperature to start the buzzer
int tempMax = 70;
int fan = 6; // the pin where fan is connected
int fanSpeed = 0;

void setup() {
pinMode(fan, OUTPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
}
void loop() {
temp = analogRead(tempPin);
temp = (temp 5.0100.0)/1024.0;
Serial.println(temp);
delay(1000); // delay in between reads for stability
if(temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if((temp >= tempMin) && (temp <= tempMax)) //if temperature is higher than the minimmum range
{
fanSpeed = map(temp, tempMin, tempMax, 55, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
}

this is the attach of the wiring i’m not sure whether would the code match this or i have to do any changes

DHT11 Fan LCD.jpg

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks... Tom... :slight_smile:

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7
float temp; 
int tempPin = 7; //arduino pin used for temperature sensor
int tempMin = 55;   // the temperature to start the buzzer
int tempMax = 70;   
int fan = 6;       // the pin where fan is connected
int fanSpeed = 0;

void setup() {
 pinMode(fan, OUTPUT);
 pinMode(tempPin, INPUT);
 Serial.begin(9600);
}
void loop() {
  temp = analogRead(tempPin);
  temp = (temp *5.0*100.0)/1024.0; 
  Serial.println(temp);
  delay(1000);        // delay in between reads for stability
  if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);
   } 
   if((temp >= tempMin) && (temp <= tempMax))  //if temperature is higher than the minimmum range
   { 
       fanSpeed = map(temp, tempMin, tempMax, 55, 255); // the actual speed of fan
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
}

Hi sorry this is the code.

DHT11 Fan LCD.jpg

If you just want it on or off then you don't need any of the map or analorWrite stuff. Just an if statement and a digitalWrite and an else statement with a digitalWrite. Don't make it harder than it needs to be.

delta which mean i should read like this?? correct me if i’m wrong, appreciate your kindness!

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7
float temp; 
int tempPin = 7; //arduino pin used for temperature sensor
int tempMin = 25;   // the temperature to start the buzzer
int tempMax = 70;   
int fan = 6;       // the pin where fan is connected
int fanSpeed = 0;

void setup() {
 pinMode(fan, OUTPUT);
 pinMode(tempPin, INPUT);
 Serial.begin(9600);
}
void loop() {
  temp = analogRead(tempPin);
  temp = (temp *5.0*100.0)/1024.0; 
  Serial.println(temp);
  delay(1000);        // delay in between reads for stability
  if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);
   } 
   if((temp >= tempMin) && (temp <= tempMax))  //if temperature is higher than the minimmum range
   { 
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       digitalWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
}

Hi,
Are you really using a DHT11?

If so ,you cannot get any readings from it by just reading the analog pin.

Look in the IDE Examples and look for DHT11, you will find an example that uses the device properly.

If you cannot find DHT11 examples it means you do not have the DHT11 library loaded, google will help.

Looking at the fact that you are using DHT library, check the Examples for DHT and look and try them.

The output in of the DHT11 is not analog pin, it is a digital pin, that can output, not just temperature, C and F and K, Humdity, Dew point.

In fact this is the DHT example for DHT11.

//
//    FILE: dht11_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT11_PIN 5

void setup()
{
  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(2000);
}
//
// END OF FILE
//

But I suggest you get the DHT11 Library.

Also your Fritzy picture shows that you DHT11 is not connected correctly.

Tom… :slight_smile:

No, not like that (Reply #8 sorry can't quote on iPhone)

DigitalWrite only needs the pin and HIGH or LOW. It's either on or off. There's no speed to calculate.

If you’re going to set fan speed based on being above/below a certain temperature point then you should add a degree or two of hysteresis around that point to avoid continuous cycling. Consider how a thermostat works:

thanks you guys, appreciate all your kindness in helping me get it work. I will try it again later.