Automatic Temperature Controlled Fan Project using Arduino

In this arduino based project, we are going to control DC fan speed according to the room temperature and show these parameter changes on a 16x2 LCD display. It is accomplished by the data communications between Arduino, LCD, DHT11 sensor Module and DC fan that is controlled by using PWM. PWM is a technique by using which we can control voltage.

 

Circuit Components

  1. Arduino UNO

  2. DHT11 sensor

  3. DC Fan

  4. 2n2222 transistor

  5. 9 volt battery

  6. 16x2 LCD

  7. 1K resistor

  8. Connecting wires

 

This project consists of three sections. One senses the temperature by using humidity and temperature sensor namely DHT11. Second section reads the dht11 sensor module’s output and extracts temperature value into a suitable number in Celsius scale and control the fan speed by using PWM. And last part of system shows humidity and temperature on LCD and Fan driver.

 

Here in this project we have used a sensor module namely DHT11 that are already have discuss our previous project namely “Humidity and Temperature Measurement using Arduino”. Here we have only used this DHT sensor for sensing temperature, and then programmed our arduino according to the requirements.

Working of this project is very simple. We have created PWM at pwm pin of arduino and applied it at base terminal of transistor. Then transistor creates a voltage according to the pwm input.

PWM Wave Form

Fan speed and PWM values and duty cycles values are showing in given table

Temperature

Duty Cycle

PWM Value

Fan Speed

Less 26

0%

0

Off

26

20 %

51

20%

27

40%

102

40%

28

60%

153

60%

29

80%

204

80%

Greater 29

100%

255

100%

 

What is PWM? PWM is a technique by using we can control the voltage or power. To understand it more simply, if you are applying 5 volt for driving a motor then motor will moving with some speed, now if we reduces applied voltage by 2 means we apply 3 volt to motor then motor speed also decreases. This concept is used in the project to control the voltage using PWM. (To understand more about PWM, check this circuit: 1 Watt LED Dimmer)

PWM

The main game of PWM is digital pulse with some duty cycle and this duty cycle is responsible for controlling the speed or voltage.

Suppose we have a pule with duty cycle 50% that means it will give half of voltage that we apply.

Formula for duty cycle given below:

Duty Cycle=  Ton/T

Where T= total time or Ton+Toff

And Ton= On time of pulse (means 1 )

And Toff= Off time of pulse (means 0)

50% Duty Cycle

10% Duty Cycle

90% Duty Cycle

PWM 4

 

Circuit Description

Connections of this temperature controlled fan circuit is very simple, here a liquid crystal display is used for displaying temperature and Fan speed Status. LCD is directly connected to arduino in 4-bit mode (Check this tutorial for more details: LCD Interfacing with Arduino Uno). Pins of LCD namely RS, EN, D4, D5, D6 and D7 are connected to arduino digital pin number 7, 6, 5, 4, 3 and 2. And a DHT11 sensor module is also connected to digital pin 12 of arduino. Digital pin 9 is used for controlling fan speed trough transistor.

Temperature Controlled Fan Circuit Diagram

 

Code Description

First we include the library for lcd and dht sensor and then define pin for lcd, dht sensor and for fan.

Define

Then initialize all the things in setup loop. And in loop by using dht function reads DHT sensor and then using some dht functions we extract temperature and display these on LCD.

Temperature reading

After this we compare the temperature with pre define temperature digit and then generate PWM according to the temperature value.

check

For generating PWM we have used “analogWrite(pin, PWM value)” fuction in 8 bit. Mean if PWM value is equivalent of analog value. So if we need to generate 20% of duty cycle then we passes 255/5 value as PWM in “analogWrite” Function. 

Code: 

#include<dht.h>      // Including library for dht
#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

#define dht_dpin 12 
dht DHT;

#define pwm 9

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

void setup()
{
 lcd.begin(16, 2);
 lcd.createChar(1, degree);
 lcd.clear();
 lcd.print("   Fan Speed  ");
 lcd.setCursor(0,1);
 lcd.print("  Controlling ");
 delay(2000);
 analogWrite(pwm, 255);
 lcd.clear();
 lcd.print("Circuit Digest ");
 delay(2000);
}

void loop()
{
  DHT.read11(dht_dpin);
  int temp=DHT.temperature;
  lcd.setCursor(0,0);
  lcd.print("Temperature:");
  lcd.print(temp);   // Printing temperature on LCD
  lcd.write(1);
  lcd.print("C");
  lcd.setCursor(0,1);
  if(temp <26 )
    { 
      analogWrite(9,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);
}

Video: 

Comments (44)

  • Mohamed's picture
    Mohamed

    Diagram mistake is that there is no ground in fan motor circuit

    Aug 10, 2015
  • vikram's picture
    vikram

    arduino ide is showing compilation error due to the dht library. kindly specify the library used in your program code as dht.h is not a valid library

    Aug 31, 2015
  • Jayant's picture
    Jayant

    Please install the DHT library properly, and make sure that dht.h file is there in the folder. Remember that header file name is case sensitive, if file in the DHT library folder is DHT.h then use DHT.h not dht.h

    Sep 14, 2015
  • ameer's picture
    ameer

    i already change dht.h to DHT.h but i got another error:

    pwn_fan.ino:6:1: error: 'dht' does not name a type
    pwn_fan.ino: In function 'void loop()':
    pwn_fan.ino:39:6: error: expected unqualified-id before '.' token
    pwn_fan.ino:40:15: error: expected primary-expression before '.' token
    pwn_fan.ino:87:14: error: expected '}' at end of input
    Error compiling.

    how can i fix this? can you help me please... thanks in advance

    Nov 02, 2015
  • omar's picture
    omar

    please help error library dht to DHT

    Dec 06, 2015
  • saleem's picture
    saleem

    hello, i want to ask. how did you show the data on digital oscilloscope? is it the interface that allow us to manipulate the temperature setting or anything like that? thank you

    Oct 27, 2015
  • Hajara 's picture
    Hajara

    pls could you modify the codes if a heater is added to the system in other to activate if temp is low. thanks

    Oct 29, 2015
  • saddam4201's picture
    saddam4201

    There is no problem in compiling the code. You need to add DHT library in arduono ide library folder.
    Thank you

    Nov 02, 2015
  • omi's picture
    omi

    I've already run your codes without any error... but the fan spinning at the same speed even though in different temperature. could you help me with the problem please... this technique is so crucial to my project. thank you

    Nov 25, 2015
  • saddam4201's picture
    saddam4201

    Please check your connections and try to change your transistor.

    Nov 26, 2015
  • syafiq's picture
    syafiq

    hello.. i have problem.. in my proteus, does not have DHT11. How to put that sensor in my proteus library. anyone can help me?

    Dec 17, 2015
  • Maddy's picture
    Maddy

    Proteus 8 supports the DHT library, no need to add. For Proteus 7 you can find the DHT library on Internet and can add.

    Jan 01, 2016
  • dhea's picture
    dhea

    hallo, by the way thank you for your shared
    i have a problem when i want to compile the source code, arduino says 'dht' was not name a type
    what should i do?
    please your advice
    thank you anyway

    Jan 01, 2016
  • Maddy's picture
    Maddy

    Please check the above comments...

    Jan 01, 2016
  • jpaul's picture
    jpaul

    please assist,
    Even after downloading and installing the DHT file,when i compile the code,i get this error
    Exit status 1
    'dht' does not name a type
    Can you assist please? Thank you

    Feb 11, 2016
  • Jayant's picture
    Jayant

    Please read all the above comments carefully, and install the DHT library properly, and make sure that dht.h file is there in the folder. Remember that header file name is case sensitive, if file in the DHT library folder is DHT.h then use DHT.h not dht.h

    Feb 23, 2016
  • Rakesh's picture
    Rakesh

    Does anyone compiled this using proteus if done Please mail to me Thanks in advance. please this is my final year project.you can save my ass.

    Feb 13, 2016
  • Balwant deshmukh 's picture
    Balwant deshmukh

    Display shows garbage value and fan does not rotate
    connection and program written as per given
    And compiled and uploaded properly

    Apr 03, 2016
  • saddam khan's picture
    saddam khan

    Use well regulated Power supply for your circuit for better results.

    Apr 04, 2016
  • AZDD's picture
    AZDD

    when i compile, it shows me this error:
    'class DHT' has no member named 'read11'
    can someone tell me what does it mean?

    Apr 10, 2016
  • saddam khan's picture
    saddam khan

    Add dht11 library for arduino in arduino ide.

    then try again.

    best of luck

    Apr 21, 2016
  • Austin Melhado's picture
    Austin Melhado

    it terms of the diagram connection for the fan, is everything correct does there need a ground added to the fan as mention in the first comment or is everything correct

    May 16, 2016
  • Maddy's picture
    Maddy

    Circuit is correct, used Motor is DC motor and its Ground is connected to negative terminal of Battery through Transistor 2N2222

    Jun 11, 2016
  • Dhruv Mehta's picture
    Dhruv Mehta

    friend this connection is for servo ,motor u need to connect fan and wen u connect fan it is nessery to give ground to the ground in auredino if u do dis ur project wil run sucessfully and it will work properly

    Jun 04, 2016
  • Sachin's picture
    Sachin

    Please can you explain the resistor parts? To where the two resistors are exactly connecting to? It's not very clear for me and I need to do this as my project so please help?

    Jun 12, 2016
  • Abhishek's picture
    Abhishek

    Please check the connections in Circuit Diagram.

    Jul 09, 2016
  • Mohammad Shoaib's picture
    Mohammad Shoaib

    I have same problem that is:
    _12:6: error: 'dht' does not name a type
    _12.ino: In function 'void loop()':
    _12:39: error: 'DHT' was not declared in this scope

    please help me as i am beginner about arduino.
    I need final working code.Please please help me.
    []

    Jul 18, 2016
  • Maddy's picture
    Maddy

    Install the DHT library properly, go through the above comments to install DHT library properly.

    Aug 20, 2016
  • Jay's picture
    Jay

    Instead of using a battery, can I use a 12V power adapter and have the ground from the fan into ground on my board, and the 12v power running through the 2n2222 transistor and the power from fan into the other end of the 2n2222 transistor?

    Aug 02, 2016
  • Maddy's picture
    Maddy

    You need to check the datasheet of all the components for their power and current rating before using 12v Adapter, it may burn some components.

    Aug 20, 2016
  • Charlie's picture
    Charlie

    Are there any alternative for 9v Battery ? i find it quite pricey haha
    Thanks . Will AA batteries do ?

    Aug 25, 2016
  • Abhishek's picture
    Abhishek

    Although the 9v batteries are very cheap, but you can use 4 or 6 AA batteries for simple DC fan.

    Sep 20, 2016
  • samuel's picture
    samuel

    will the be a difference if I use lm35 as my sensor instead of DHT11 ......I use the exact circuit l

    Sep 01, 2016
  • deadpool's picture
    deadpool

    Sir , I'm having a problem I tried to compile the code but there was an error "LiquidCrystal" does not name a type I hope you could help me here thank you

    Oct 03, 2016
  • Maddy's picture
    Maddy

    You may have forgotten to include the LiquidCrystal.h library, please check and always use latest version of Arduino IDE.

    Oct 07, 2016
  • Rajesh's picture
    Rajesh

    I got an error stating - " no matching function for call to 'DHT::DHT()' "
    Please help me out.

    Oct 09, 2016
  • Prasanna's picture
    Prasanna

    I did all my connections correct but not printing and not able to control

    Oct 24, 2016
  • Salman's picture
    Salman

    Instead of DHT11. Is it possible to use DHT22. I have read in many articles, DHT22 have more accuracy. So I guess DHT22 will be more useful. If it is possible to use, will be there any change in coding and other electronic components used in the circuit ??

    Oct 29, 2016
  • Abhishek's picture
    Abhishek

    Yes you can use DHT22 with this circuit. It is recommended to check its datasheet for its pins and power requirement.

    Dec 01, 2016
  • Quinno's picture
    Quinno

    Can I know what is the voltage of the DC fan used in this project?

    Nov 09, 2016
  • Shubhang kedia's picture
    Shubhang kedia

    i did all the things same as said but my fan moves at constant speed.....it doesn't even switches off when temperature falls below the limit and its speed doesn't increases when temperature increases...
    PLEASE HELP..

    Nov 17, 2016
  • mumin's picture
    mumin

    Please someone send me your runnable code?
    thanks

    Nov 28, 2016

Leave a comment