Advertisement

Arduino DHT22 (AM2302) Tutorial + Library

dht22You 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.

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

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

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define fan 4

int maxHum = 60;
int maxTemp = 40;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(fan, OUTPUT);
  Serial.begin(9600); 
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  if(h > maxHum || t > maxTemp) {
      digitalWrite(fan, HIGH);
  } else {
     digitalWrite(fan, LOW); 
  }
  
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C ");

}

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.

10 Comments

Join the conversation!

Error! Please fill all fields.
  • jasyen27gmail-com

    Sir. im interested with how to use the dht11 with fans,.. but my problem is i can’t seem to make it work with two dht11 sensors with different output for dc fans like for the first sensor is for intake and the other one is exhaust.. i hope you can help me..

    this is my email… jasyen27@gmail.com

  • a-petitjeantelenet-be

    Hello Sir ,
    Is it possibble to change the dht22 into DS18b20 sensor because it is smaller
    How a can fix it ( it also change ths code i think )
    Please can you help ???

    PS : a lm35dz sensor is also possible for mi

    my email :
    a.petitjean@telenet.be

  • Anup kumar

    I am getting “Failed to read Sensor data!”..
    So many times I executed…same thing

    • jaymehta-resumegmail-com

      I am also getting the same message as “Failed to read Sensor data!”.. Please tell me if you have found the error or prob regarding this.
      Thanks..

      -Jay

  • Tim

    Are you using a 3pin or 4pin fan?

  • 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

  • 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

  • electron

    hey sorry my arduino is giving me an error about DHT please help

  • irshadshark

    It’s nice but I need full instruction

  • Manar

    I like it could you please add other projects using other kind of sensors thanks in advance.

Looking for the latest from TI?