electroSome

Temperature Controlled Fan using Arduino

Temperature Controlled Fan using Arduino

Temperature Controlled Fan using Arduino

Here we are going to make a temperature controlled DC fan. DHT22 sensor is used to sense the room temperature and then we adjust speed of a DC fan/motor accordingly using PWM (Pulse Width Modulation). Arduino Uno is the heart of this project and a L293D driver IC is used to drive the DC fan/motor.

Components Required

Circuit Diagram

Temperature Controlled Fan using Arduino – Circuit Diagram

Explanation

First we can connect L293D motor driver IC to Arduino as below. You can read articles L293D Motor Driver IC and H-Bridge Motor Driving for more information about the working of L293D.

After that connect DHT22 sensor to the Arduino, it is using a single wire bus for communication.

Now we can connect 16×2 LCD to the Arduino.

Program

#include "DHT.h"           // Including DHT22 library
#include "LiquidCrystal.h" // Including LCD library

#define DHTPIN 6      // Declaring pin 6 for communicating to DHT22 sensor
#define DHTTYPE DHT22 // Declaring the type of DHT sensor we are using (DHT22 or DHT11)

DHT dht(DHTPIN, DHTTYPE);          // Declaring DHT connection and type
LiquidCrystal lcd(12,11,10,9,8,7); // Declaring LCD connections

int Motor_Pin1 = 4;  // pin 2 on L293D
int Motor_Pin2 = 3;  // pin 7 on L293D
int Enable = 5;      // pin 1 on L293D

void setup() {
  pinMode(Motor_Pin1, OUTPUT);  
  pinMode(Motor_Pin2, OUTPUT);
  pinMode(Enable, OUTPUT);
  lcd.begin(16,2); // Initializes the 16x2 LCD
  dht.begin();     // Initializes DHT sensor
}

void loop() {
  lcd.clear();                        // Clear LCD

  float temp = dht.readTemperature(); // Reading the temperature in Celsius

  if (isnan(temp)) {                  // Validating received data
    lcd.print("Failed to read");
    delay(1000);
    return;
  }

  lcd.setCursor(0,0);
  lcd.print("Temp.: "); 
  lcd.print(temp);      // Writing temperature in the first row
  lcd.print(" C");
  lcd.setCursor(0,1);   // Setting the position to display fan speed

  if(temp <25 ) {                 // If the temperature less than 25
    analogWrite(Enable,0);        // 0% PWM duty cycle
    lcd.print("Fan OFF ");      
    delay(100);
  }
  else if(temp>=25 & temp<30) {      // If the temperature is between 25 & 30
    analogWrite(Enable, 77);         // 30% of maximum duty cycle value (255). 
    lcd.print("Fan Speed: 30% ");
    delay(100);
  }
  else if(temp>=30 & temp<35) {      // If the temperature is between 30 & 35
    analogWrite(Enable, 153);        // 60% of maximum duty cycle value (255).
    lcd.print("Fan Speed: 60% ");
    delay(100);
  }
  else if(temp>=35) {                // If the temperature is above 35
    analogWrite(Enable, 255);        // 100% duty cycle
    lcd.print("Fan Speed: 100%   ");
    delay(100);
  }

  digitalWrite(Motor_Pin1, LOW);  // To drive the motor in a particular direction
  digitalWrite(Motor_Pin2, HIGH); // To drive the motor in a particular direction

  delay(2000); // 2 seconds delay
}

DHT22 Library

You need to manually add DHT library to Arduino IDE as it is not included by default. You can ignore it if you already added it. Otherwise you can do following steps for that.

Explanation

First of all, we have included libraries for DHT22 and LCD. After that we have declared pins connected to DHT22, LCD and L293d motor driver.

#include "DHT.h"            // Including DHT22 library
#include "LiquidCrystal.h"  // Including LCD library

#define DHTPIN 6      // Declaring pin 6 for communicating with DHT22 sensor
#define DHTTYPE DHT22 // Declaring the type of DHT sensor we are using (DHT22 or DHT11)

DHT dht(DHTPIN, DHTTYPE);          // Declaring DHT connection and type
LiquidCrystal lcd(12,11,10,9,8,7); // Declaring the LCD connections

int Motor_Pin1 = 4;  // pin 2 on L293D
int Motor_Pin2 = 3;  // pin 7 on L293D
int Enable = 5;      // pin 1 on L293D

Next we are initializing the Arduino and modules connected to it.

void setup() {
  pinMode(Motor_Pin1, OUTPUT);
  pinMode(Motor_Pin2, OUTPUT);
  pinMode(Enable, OUTPUT);
  lcd.begin(16,2); // Initializes the 16x2 LCD
  dht.begin();     // Initializes DHT sensor
}

In the next section we are reading temperature from DHT22 sensor and storing it in a variable ‘temp’.

float temp = dht.readTemperature(); // Reading the temperature in Celsius

  if (isnan(temp)) {                // Validating received temperature (whether it is a number or not)
    lcd.print("Failed to read");
    delay(1000);
    return;
  }

Then we are displaying the temperature in LCD.

lcd.setCursor(0,0);   //Setting the cursor at the start of the LCD
lcd.print("Temp.: "); //Writing the temperature on the LCD
lcd.print(temp);      //Writing the value of temperature on the LCD
lcd.print(" C");
lcd.setCursor(0,1);   //setting the cursor at next line

In the next section we are creating 4 steps of fan speed depending on the temperature.

if(temp <25 ) {                      // If the temperature less than 25
    analogWrite(Enable,0);           // 0% PWM duty cycle
    lcd.print("Fan OFF ");      
    delay(100);
  }
  else if(temp>=25 & temp<30) {      // If the temperature is between 25 & 30
    analogWrite(Enable, 77);         // 30% of maximum duty cycle value (255). 
    lcd.print("Fan Speed: 30% ");
    delay(100);
  }
  else if(temp>=30 & temp<35) {      // If the temperature is between 30 & 35
    analogWrite(Enable, 153);        // 60% of maximum duty cycle value (255).
    lcd.print("Fan Speed: 60% ");
    delay(100);
  }
  else if(temp>=35) {                // If the temperature is above 35
    analogWrite(Enable, 255);        // 100$ duty cycle
    lcd.print("Fan Speed: 100%   ");
    delay(100);
  }

Hope you clearly understand the program, if you have any doubts please do comment below.

Working

DHT22 sensor is used to read the temperature to control fan speed. It can even read relative humidity also. This sensor is very easy to use and having very good accuracy compared to other sensors. We are using L293D motor driver IC for controlling DC fan/motor with Arduino. It can drive 2 DC motors and we can also control the speed by providing PWM signals.

In the program we have set four different conditions to run the DC fan. If the temperature is less than 25°C, then the DC fan will remain off and details will be displayed on the LCD. If the temperature is between 25°C and 30°C, then the DC fan will start working at low speed (30% duty cycle). Similarly if the temperature is between 30°C and 35°C, then the DC fan will run at a medium speed (60% duty cycle). If the  temperature is greater than or equal to 35°C, then the DC fan will run at full speed (100% duty cycle).

Video