Menu
Arduino
Audio
Datasheets
DIY
Gadgets
Hobby
Lights
Measure
Power supply
Radio
Service
Solar
Theory
Various
Alarms
Useful
Search this site...
Home » Arduino » Arduino DHT22 (AM2302) Tutorial + Library
Arduino DHT22 (AM2302) Tutorial + Library
Posted on February 17th
You can use the DHT22 (or AM2302) humidity/temperature sensor and the
Arduino UNO board to read data and print it out to the serial monitor or to display it
on an LCD. In my case I chose the serial monitor version because is faster and
cheaper, but you may want to look at other articles, including this one, if you want to
use an LCD.
[ad#Adsense Top Normal size=”336″]
I’ve chosen the DHT22 over DHT11 because it has a wider range of measurement, 0 to
100% for humidity and -40°C to +125°C for temperature. Also it has a digital output
(Single-bus) that provides higher data accuracy. I also use a DC fan that will start
spinning when the humidity level reaches 60% or the temperature is higher than
40°C, but you may change these values in the sketch.
Arduino DHT22 Wiring, Circuit and Photos
Arduino DHT22 humidity and temperature simple projec...
In the sketch you can see that I use the Adafruit DHT library that is very simple to use
and if you follow the code line by line your project will have zero errors.
Arduino DHT22 sketch
1. #include "DHT.h"
2.
3. #define DHTPIN 2 // what pin we're connected to
4. #define DHTTYPE DHT22 // DHT 22 (AM2302)
5. #define fan 4
6.
7. int maxHum = 60;
8. int maxTemp = 40;
9.
10. DHT dht(DHTPIN, DHTTYPE);
11.
12. void setup() {
13. pinMode(fan, OUTPUT);
14. Serial.begin(9600);
15. dht.begin();
16. }
17.
18. void loop() {
19. // Wait a few seconds between measurements.
20. delay(2000);
21.
22. // Reading temperature or humidity takes about 250 milliseconds!
23. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
24. float h = dht.readHumidity();
25. // Read temperature as Celsius
26. float t = dht.readTemperature();
27.
28. // Check if any reads failed and exit early (to try again).
29. if (isnan(h) || isnan(t)) {
30. Serial.println("Failed to read from DHT sensor!");
31. return;
32. }
33.
34. if(h > maxHum || t > maxTemp) {
35. digitalWrite(fan, HIGH);
36. } else {
37. digitalWrite(fan, LOW);
38. }
39.
40. Serial.print("Humidity: ");
41. Serial.print(h);
42. Serial.print(" %\t");
43. Serial.print("Temperature: ");
44. Serial.print(t);
45. Serial.println(" *C ");
46.
47. }
download the sketch
You may want to change maxHum and maxTemp values to the ones you desire the
fan to start. You may also use a relay instead of the fan if you need to connect some
other equipment that requires higher voltages. Also do not forget to connect a 10K
resistor between Vcc and Data pin on DHT22 sensor.
ST-50/56
rkcinst-usa.com
Stick type temperature
sensor High response
Related projects and circuits
AM2302 / Arduino Room Read Read the State
DHT22 Temperature Temperatures of a Button
Datasheet Monitor using I2C, with Arduino
Comments
1.
Manar
I like it could you please add other projects using other kind of sensors thanks in
advance.
Posted on February 19, 2015
Reply
2.
irshadshark
It’s nice but I need full instruction
Posted on February 20, 2015
Reply
3.
electron
hey sorry my arduino is giving me an error about DHT please help
Posted on May 25, 2015
Reply
4.
anthony
Hi i have everything setp the way you have it in the diagrams but my fan wont
respond to my dht22 readings i have tried changing the stats in the script but all
i can get is a reading from the dht22
Posted on June 7, 2015
Reply
5.
Troy
Cool article, I have mine with the LCD shield though. I was just after some
pointers
@anthony: Perhaps try with an LED, or use the onboard LED to test with. for
example. I just tried it with PIN13 (UNO’s built in LED), and the LED illuminated
fine. Change the code to accept a lower max temperature, then re-upload it.
int maxTemp = 40;
change it to….
int maxTemp = 15;
and you may find that the fan/LED works fine
Posted on August 22, 2015
Reply
6.
Tim
Are you using a 3pin or 4pin fan?
Posted on November 14, 2015
Reply
7.
Anup kumar
I am getting “Failed to read Sensor data!”..
So many times I executed…same thing
Posted on December 7, 2015
Reply
Leave a Comment
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Submit Your Comment
Check this checkbox to get notifications of followup comments via e-mail. You
can also subscribe without commenting.
Exit Mobile Mode