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

DHT22 PinOut
arduino dht22 circuit
arduino dht22 fan 2
arduino dht22 fan 1

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 ");    }  
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.