How to Use Temperature and Humidity (DHT) Sensors

How to Use Temperature and Humidity (DHT) Sensors

This project will show you how to use temperature and hmidity (DHT11 and DHT22) sensors with an Arduino card.

  • 44,572 views
  • 4 comments
  • 35 respects

Components and supplies

Apps and online services

About this project

The DHT22 sensor (also known as the AM2302 reference) and his little brother the DHT11 are temperature and humidity sensors.

DHT22

The DHT22/AM2302 sensor is able to measure temperatures from -40 to + 125 °c with an accuracy of +/-0.5 °c and relative humidity levels from 0 to 100% with an accuracy of +/-2% ( +/-5% at extremes, to 10% and 90% ).

A measurement can be performed every 500 milliseconds ( twice per second ).

DHT11

The DHT11 sensor is able to measure temperatures from 0 to + 50 °c with an accuracy of +/-2 °c and relative humidity levels of 20 to 80% with an accuracy of +/-5%. A measurement can be done every second.

Then, the DHT22 will be better to use.

But … in this project I will show you how to use DHT11 and DHT22.

However, the DHT11 has a lower cost.

Connections

First pin ( DHT ) --> 5V

Second pin ( DHT ) --> Pin D2

A resistance must be connected between the wire of the second pin and the wire of the first pin.

Third Pin --> Nothing

Fourth Pin --> GND

The Code

You must download a library before used the code:

Go on Arduino IDE, go on file/example/DHT sensor Library/DHTtester (the code on DHT tester is the same as the code above and in attachments) of and observe:

Code

Code for DHT22Arduino
#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT22   

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}
Code for DHT11Arduino
#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT11   

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

Schematics

Connections DHT11 / 22
The connections for DHT11 are the same as those of the DHT22.
dht11_22_fefe_0IW5TXXgRU.fzz

Comments

Similar projects you might like

Arduino Temperature - Humidity - Rain Sensor

Project showcase by Rick_Findus

  • 15,343 views
  • 6 comments
  • 19 respects

Temperature and Humidity Data Logger

Project tutorial by Wimpie van den Berg

  • 32,420 views
  • 2 comments
  • 31 respects

Health Kit: Humidity and Temperature Control

Project tutorial by Diana Khalipina

  • 9,366 views
  • 2 comments
  • 50 respects

Date, Time, Temperature and Humidity Display

Project tutorial by Chamath Vithanawasam

  • 24,787 views
  • 11 comments
  • 49 respects

Temperature and Humidity Data logger - Breadboard

Project tutorial by Jed Hodson

  • 9,447 views
  • 2 comments
  • 11 respects

Temperature and Humidity Logger Per Hour

Project tutorial by lefteris1993

  • 3,313 views
  • 0 comments
  • 13 respects
Add projectSign up / Login