Arduino and DHT11

Detailed Tutorial

1. Introduction:

This tutorial explains how to read temperature and humidity data through DHT series sensor on Arduino platform.

DHT series sensors have their other names too –

  • DHT11 = RHT01
  • DHT21 = RHT02= AM2301 = HM2301
  • DHT22 = RHT03= AM2302
  • DHT33 = RHT04 = AM2303
  • DHT44 = RHT05

DHT library included here in this tutorial supports following sensor of DHT series

  1. DHT11
  2. DHT22
  3. AM2302
  4. RHT03

This library automatically detects if any one of the above mentioned sensor is connected to Arduino.

2. Understanding DHT11 sensor

DHT11 sensor gives humidity and temperature data. It has got following pin interface.

DHT11-Pinout_

3. Circuit

Make following connections of DHT sensor to Arduino in the following manner.

DHT-on-arduino

4. DHT Library

Download DHT library from here. Install the same.

5. Programming

Test Source Code:

// DHT Temperature & Humidity Sensor library for Arduino
// http://arduino.cc/playground/Main/DHTLib
//
//

#include <dht.h>

dht DHT;

#define DHT11_PIN 4
#define DHT21_PIN 5
#define DHT22_PIN 6

void setup()
{
 Serial.begin(115200);
 Serial.println("DHT TEST PROGRAM ");
 Serial.print("LIBRARY VERSION: ");
 Serial.println(DHT_LIB_VERSION);
 Serial.println();
 Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
 // READ DATA
 Serial.print("DHT22, \t");
 int chk = DHT.read22(DHT22_PIN);
 switch (chk)
 {
 case DHTLIB_OK: 
 Serial.print("OK,\t"); 
 break;
 case DHTLIB_ERROR_CHECKSUM: 
 Serial.print("Checksum error,\t"); 
 break;
 case DHTLIB_ERROR_TIMEOUT: 
 Serial.print("Time out error,\t"); 
 break;
 default: 
 Serial.print("Unknown error,\t"); 
 break;
 }
 // DISPLAY DATA
 Serial.print(DHT.humidity, 1);
 Serial.print(",\t");
 Serial.println(DHT.temperature, 1);

delay(1000);


// READ DATA
 Serial.print("DHT21, \t");
 chk = DHT.read21(DHT21_PIN);
 switch (chk)
 {
 case DHTLIB_OK: 
 Serial.print("OK,\t"); 
 break;
 case DHTLIB_ERROR_CHECKSUM: 
 Serial.print("Checksum error,\t"); 
 break;
 case DHTLIB_ERROR_TIMEOUT: 
 Serial.print("Time out error,\t"); 
 break;
 default: 
 Serial.print("Unknown error,\t"); 
 break;
 }
 // DISPLAY DATA
 Serial.print(DHT.humidity, 1);
 Serial.print(",\t");
 Serial.println(DHT.temperature, 1);

delay(1000);

// READ DATA
 Serial.print("DHT11, \t");
 chk = DHT.read11(DHT11_PIN);
 switch (chk)
 {
 case DHTLIB_OK: 
 Serial.print("OK,\t"); 
 break;
 case DHTLIB_ERROR_CHECKSUM: 
 Serial.print("Checksum error,\t"); 
 break;
 case DHTLIB_ERROR_TIMEOUT: 
 Serial.print("Time out error,\t"); 
 break;
 default: 
 Serial.print("Unknown error,\t"); 
 break;
 }
 // DISPLAY DATA
 Serial.print(DHT.humidity,1);
 Serial.print(",\t");
 Serial.println(DHT.temperature,1);

delay(1000);
}
//
// END OF FILE
//

6. Output

Run the following code and you will get the following output.

Output

This is how to use DHT series sensors on Arduino.

Advertisements