.
Circuit Components
Arduino UNO
DHT11 sensor
DC Fan
2n2222 transistor
9 volt battery
16x2 LCD
1K resistor
Connecting wires transistor creates a voltage according to the pwm input.
Fan speed and PWM values and duty cycles values are showing in given table)
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)
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.. 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.
Code Description
First we include the library for lcd and dht sensor and then define pin for lcd, dht sensor and for fan..
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.
#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);
}
Aug 10, 2015
Diagram mistake is that there is no ground in fan motor circuit
Aug 31, 2015
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
Sep 14, 2015
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
Nov 02, 2015
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
Feb 02, 2018
how do you rectified your error ,please tell me,i am doing that project
Dec 06, 2015
please help error library dht to DHT
Dec 17, 2015
Code is working fine, please install dht library properly. Download DHT library from here and and install it using this Tutorial: Installing Additional Arduino Libraries
Mar 18, 2018
bro please give me the code and all detail how to do pleass
Jun 05, 2018
plz send me rectified code . i m doing this project . i requst you plz send me code which is edited by you
Oct 27, 2015
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 29, 2015
pls could you modify the codes if a heater is added to the system in other to activate if temp is low. thanks
Nov 02, 2015
There is no problem in compiling the code. You need to add DHT library in arduono ide library folder.
Thank you
Nov 25, 2015
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 26, 2015
Please check your connections and try to change your transistor.
Dec 17, 2015
hello.. i have problem.. in my proteus, does not have DHT11. How to put that sensor in my proteus library. anyone can help me?
Jan 01, 2016
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
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
Please check the above comments...
Feb 11, 2016
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 23, 2016
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 13, 2016
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.
Apr 03, 2016
Display shows garbage value and fan does not rotate
connection and program written as per given
And compiled and uploaded properly
Apr 04, 2016
Use well regulated Power supply for your circuit for better results.
Apr 10, 2016
when i compile, it shows me this error:
'class DHT' has no member named 'read11'
can someone tell me what does it mean?
Apr 21, 2016
Add dht11 library for arduino in arduino ide.
then try again.
best of luck
May 16, 2016
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
Jun 11, 2016
Circuit is correct, used Motor is DC motor and its Ground is connected to negative terminal of Battery through Transistor 2N2222
Jun 04, 2016
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
Mar 21, 2018
can you please send me full code of this project
Jun 12, 2016
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?
Jul 09, 2016
Please check the connections in Circuit Diagram.
Jul 18, 2016
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.
[]
Aug 20, 2016
Install the DHT library properly, go through the above comments to install DHT library properly.
Mar 23, 2017
Pls give the link to install dht library
Aug 02, 2016?
Aug 20, 2016
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 25, 2016
Are there any alternative for 9v Battery ? i find it quite pricey haha
Thanks . Will AA batteries do ?
Sep 20, 2016
Although the 9v batteries are very cheap, but you can use 4 or 6 AA batteries for simple DC fan.
Sep 01, 2016
will the be a difference if I use lm35 as my sensor instead of DHT11 ......I use the exact circuit l
Sep 20, 2016
Yes you can, but you need to change the Code accordingly, check this Project to interface LM35 to Arduino: Arduino Based Digital Thermometer
Oct 03, 2016
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 07, 2016
You may have forgotten to include the LiquidCrystal.h library, please check and always use latest version of Arduino IDE.
Oct 09, 2016
I got an error stating - " no matching function for call to 'DHT::DHT()' "
Please help me out.
Oct 24, 2016
I did all my connections correct but not printing and not able to control
Oct 29, 2016
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 ??
Dec 01, 2016
Yes you can use DHT22 with this circuit. It is recommended to check its datasheet for its pins and power requirement.
Nov 09, 2016
Can I know what is the voltage of the DC fan used in this project?
Nov 17, 2016
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 28, 2016
Please someone send me your runnable code?
thanks
Jan 31, 2017
Nice work, exactly what I was looking for. But I wonder how anybody got it to work.
First I had a bunch of error on arduino, dht errors. So I used that library for dht11
then LiquidCrystal lcd(7, 6, 5, 4, 3, 2); should be LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
and #define dht_dpin 12 ==> #define dht11pin 12
and dht DHT; ==> dht11 DHT11;
and
DHT.read11(dht_dpin);
int temp=DHT.temperature;
should be
DHT11.read(dht11pin);
int temp=DHT11.temperature;
this is it for the code
and then the fan circuit should be grounded to an arduino analogue ground (from the battery minus to arduino GRND, just under +5V).
After all that change it's working thank you
Mar 08, 2017
I have facing an error to dht11 DHT11; I get an error no matching function for call to 'DHT11::DHT11()'
Mar 26, 2017
Thanks a lot!
It's only after I read your comment I was able to get my fan to stop spinning at a constant speed for all temp.
Great project,very useful.
So yeah, if your error is that fan is spinning at constant speed connect negative of fan to collector of 2222 ,negative of 9V to emitter and a wire from emitter to GND line of arduino.
Dec 27, 2017
New to this environment and struggling with finding the right combination of library and code statements. The constant appears to be 'dht' does not name a type. The library you refer to does not appear in the library manager - updated, renamed or absorbed into a larger one ?
I was hoping this 'Temperature controlled Fan' would be working on my Solar furnace by now but this code problem has held me up 2 long and frustrating days.....
Any more specific direction on this library/code would be appreciated.
Feb 06, 2017
We are doing this project,.while running this code we are getting an error like (fatal error:dht.h: No such file or directory.
Compilation terminated
Mar 04, 2017
there is no interfacing to the fan/motor .. pls help me guys..
Mar 09, 2017
Why should we use DHT 11 or DHT 22 or LM 35 or any what is the reason behind that and you use the dht11 is there any consumption or anything else.....
Mar 20, 2017
please help me in code i got error in dht library .so please send the entire correct code as early as possible.
Mar 26, 2017
In proteus it shows these errors: 1.cannot find model file 'DHTXX.MDF'
2.Missing interface model 'DHTXXITF' IN U1 #P
3. PIN 'DATA' is not modelled
4. PIN 'VDD' is not modelled
5. PIN 'GND' is not modelled
Please please help me to solve all these errors and run simulation of this project.
Mar 31, 2017
It's working! But I don't understand meaning of those byte degree values in the code. Plz explain me==>
byte degree[8] =
{
0b00011,
0b00011,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
Apr 02, 2017
As Sai vasavi mentioned above, even I'm getting the same error:dht.h: No such file or directory. Compilation terminated. Please send the exact link of the library and executable code asap as it is to be submitted tomorrow.
Apr 02, 2017
:D phew!! Kudos... it worked finally. No changes required in the code. Purely depends on installing dht library.
Thanks guys.
May 03, 2017
Where did you get that library? Please tell me how you installed that library? This is for my project. Thanks for your help :)
Sep 10, 2017
what is the voltage of the DC fan used in this project? If the voltage of fan is 5 v , and used battery 9v, so we must use voltage regulator or not?
Sep 11, 2017
Can i know how volt of dc fan, 12v or 5v.
Sep 28, 2017
hi. please help me. what does byte degree coding means? i dont get it
Oct 08, 2017
Arduino: 1.8.1 (Windows 8.1), Board: "Arduino/Genuino Uno"
C:\Users\Debasish Kalita\Documents\Arduino\random\random.ino:1:50: fatal error: dht.h: No such file or directory
#include<dht.h> // Including library for dht
^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Oct 09, 2017
Download and add dht.g library from Github to ArduinoIDE
Oct 25, 2017
if i am using DHT22 sensor instead of DHT11 then what will be the change in the code....please solve my problem
Mar 10, 2018
Can I use arduino nano instead of uno?
and Can I use a thermistor instead of dht11?
Mar 10, 2018
what is the volatge of the dc fan used? 5v or 9v?
Mar 11, 2018
Please how to fix this error
#include <Adafruit_Sensor.h>
^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno
Mar 13, 2018
i get this error:
Arduino: 1.8.5 (Mac OS X), Board: "Arduino/Genuino Uno"
In file included from /Users/DanyaalT/Documents/Arduino/libraries/DHT-sensor-library-master/DHT_U.cpp:22:0:
/Users/DanyaalT/Documents/Arduino/libraries/DHT-sensor-library-master/DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory
#include <Adafruit_Sensor.h>
^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
what can i do
Mar 13, 2018
You did not add the adafruit_sensor library to your Arduino IDE correctly.. Try adding it again follow the tutorial to know how it is done
Mar 17, 2018
display doesn't show any value rather contains only box.please need urgent suggestion.Is the motor is 9v dc
Apr 08, 2018
The diagram shown is wrong. To make it work (according to written code), you have to connect LCD pins like this :
(LCD pin) - (Arduino Digital pin)
RS - pin 7
En - pin 6
D4 - pin 5
D5 - pin 4
D6 - pin 3
D7 - pin 2
Now. You need to add “dht library” to work with DHT11 sensor. To do this, follow this steps :
1. Download
2. Copy the Arduino-master/libraries/DHTstable subfolder of the downloaded file to the libraries subfolder of your sketchbook folder. You can find the location of your sketchbook folder at File > Preferences > Sketchbook location in the Arduino IDE.
This means : after downloading the .zip file, at first extract it. You’ll find a folder named “Arduino-master”. Go inside this folder. Inside this “Arduino-master” folder, there is another subfolder called DHTstable. Copy this ‘DHTstable” folder to your “Sketchbook location” , which default is : This pc/Documents/Arduino. If you can’t find it, open your Arduino IDE, then check : File > Preferences > Sketchbook location .
Now, in documents, go to “Arduino” folder. Then go inside “libraries” subfolder. Then paste “DHTstable” folder there.
There you go, you’ve completed your library setup. Now compile the code, you won’t have any error.
I’ve found this solution on :
One last thing : Connect 2N2222 transistor properly. After constructing the whole circuit, Just “connect a wire between ‘Battery’s Ground point’ to ‘Arduino’s ground point’ in breadboard, which’ll make then a common ground”. Otherwise, your circuit will be open, & the fan won’t work.
I tried to explain step by step, hope this’ll help u guys.
Thank you.
Apr 10, 2018
Thank a ton Ishtiaque
It should be useful for people here
Feb 12, 2019
how about the other pins on lcd?
Apr 10, 2018
The fan keeps rotating at a constant speed, done all the things mentioned above pls help
Apr 02, 2019
Hii @Clyde ,, your query is excatly correct, fan keeps rotaing at constant speed. It means Battery supply is directly given to fan.
Means there is no use of code for changing fan speed.
please help us
Apr 05, 2019
No it does not mean battery is connected directly fan. As you can see there is a switching transistor in-between the fan and battery. If your fan does not reduce speed it means the transistor is not switching.
Try turning off the fan completly to check if the transistor is working
Apr 12, 2018
Please anyone reply
I have added dht file in library of arduino and also for proteus, so finally code successfully run in arduino compiler but proteus shows following two error
* cannot find model file 'DHTXX. MDF
* Simulation failed due to net list linker error
Apr 18, 2018
i followed the program...but why the temperature is not accurate?
48 degree celcius in my room....
Apr 28, 2018
Even though i have installed the DHT library files,there are lots of error while compiling the program please do correct the program and upload it .
May 15, 2018
can you please send me full code of this project
May 16, 2018
The full code of this projects is already given in this page at the bottom of the explanation
Aug 21, 2018
Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"
dht11 DHT11;
^
In function 'void loop()':
sketch_aug17b:34: error: request for member 'read' in '11', which is of non-class type 'int'
DHT11.read(DHT11pin);
^
sketch_aug17b:34: error: 'DHT11pin' was not declared in this scope
DHT11.read(DHT11pin);
^
sketch_aug17b:35: error: request for member 'temperature' in '11', which is of non-class type 'int'
int temp=DHT11.temperature;
^
'dht11' does not name a type
Any suggestions for a solution?
Sep 06, 2018
code is executing properly but the lcd does not show any data. Have tried with multiple lcds but shows the same thing....please help!!
Sep 17, 2018
Dear Saddam, thanks for the great share. It is working perfect. Just wanted to know how many identical items can I connect to this PWM ? Or how can I run multiple PWM circuits for multiple fans ? Thanks
Sep 18, 2018
It depends on how long you are running the wires and how much current is being drawn. If you powering multiple devices an Op-Amp buffer circuit is recommended
Oct 31, 2019
I want to control the speed of 3 fans (Not seperately, they will all run same speed regarding the enviroment temperature) They are all 12v 0,30A. Can I connect all 3 to the same PWM circuit ? In other words how many amperes can I run through the circuit ? Thanks
Pages