How to use the DHT22 and Arduino to Measure Humidity, Temperature, Dew Point and Heat Index.

The basic function of the DHT22 (or DHT11) is to measure Humidity but in order to do that accurately it also measures temperature (the temperature value compensates the humidity value automatically). You can also obtain the temperature value from the DHT22 - a two for one bonus!


Measurements you can make are:
  • Humidity,
  • Temperature,
  • Dew Point (derived from above),
  • Heat Index (derived from above).

Code here calculates the dew point; The temperature below which water saturated air releases water as water vapour.

The code also calculates the heat index - an indication of what the temperature "feels-like" due to humidity e.g. the temperature could be 30°C but the humidity makes it "feel-like" 40°C.

This page shows you an Arduino tutorial for the DHT22 which is also applicable to the DHT11 since they use the same electronic interface - the only differences between the devices are in the device specifications i.e. capability and accuracy (DHT22 is the better one because it is newer). However either will measure relative humidity to a good accuracy.

Warning: The DHT22/11 The 1-wire interface is not compatible with the Dallas 1-wire system. In addition the protocol is not multi-drop; You can only use one device per microcontroller pin.

Dew Point and Heat Index are values that can be easily calculated using the Arduino Uno R3 Board with both calculations requiring use of floating point code. Floating point routines take up a lot more memory but are convenient and an alternative is to use fixed point maths but this takes more effort.

Note: This sensor can be used with any microcontroller since all it needs is a bi-directional I/O pin and some code. You could even use an MCP23017.

These sensors are relative humidity sensors with a built in temperature sensor (which allows the humidity reading to be more accurate - see table below for accuracy of the device). Further on you can learn about details of relative humidity, heat index and dew point. The example, shown further down, shows you how to use the DHT22 in your own Arduino sketches.

DTH22 Pull Up Resistor

The sensor requires a pull up resistor that is specified in the datasheet (1k to 5k is recommended).

The DHT22/11 sensors use a serial communication interface, operating over long distances (over 20m is claimed DHT11, 100m for DHT22). You can buy sensors without a breakout board but the board is convenient and has a 10k pull-up resistor that is on-board. In fact the pull up resistor should be from 5k to 1k then the distance can be further. A stronger (lower) resistance will allow more current and therefore a longer distance.

DHT11 and DHT22 Max cable length:

  • DHT11: over 20m
  • DHT22: up to 100m

Major differences are:

  • DHT22 more expensive than DHT11 (but more accurate).
  • DHT22 usable below 0°C.
  • DHT22 senses from 0 to 100%RH while DHT11 senses from 20 to 90%RH.

Humidity Accuracy

To measure humidity accurately you have to know the temperature as, to a large extent, the humidity of a gas is dependent on temperature. Both of these sensors measure temperature and as a bonus you can retrieve the temperature value. However note that it is not that accurate for the DHT11; It is quite good for the DHT22. See the device specifications for details.

Note: Humidity measurements depend on Temperature to a large extent.

Relative Humidity

The sensor measures Relative Humidity and this is sometimes misleading concerning the actual water content in the air (which changes with temperature). Working out the dew point can be a better measure since it produces an absolute number (the dew point temperature). This allows you to assess the comfort level of the air i.e. sticky, dry etc. whereas RH may not indicate this (see videos below to understand this difference).

DHT22 vs DHT11 (difference) Specifications:


Parameter DHT11 (RHT01) DHT22 (RHT03)
Relative Humidity Range 20 ~ 90% 0 ~ 100%
Relative Humidity Accuracy: ±5%RH ±2%RH
Temperature Accuracy: ±2°C ±0.5°C
Resolution 1%RH,1°C 0.1%RH,0.1°C
Repeatability ±1% ±1%
Long Term Stability ±1% per year ±0.5% per year
Operating Temperature Range 0 ~ 50°C -40 ~ 80°C
Power Supply: 3,3V ~ 5.5V 3.3 ~5.5V
Supply Current: 0.5mA ~ 2.5mA 1 ~ 1.5mA
Idle Supply Current: 100uA ~ 150uA 40 ~ 50uA
Max sampling period
(Max device update rate).
1 second 2s
Manufacturer MaxDetect Technology Co. Ltd.

The data sheet indicates it can cope with a cable length of more than 20m. For less than 20m a 5k pull-up is recommended (most breakout boards have this pull up resistor built-in). If you use more than a 20m cable run then the resistor is not defined and you have to find it experimentally - for a longer cable, resistance increases, therefore you need more current to pull up the voltage successfully, so you you will need a smaller pull-up resistor .

Note : The AM2302 is similar to the RHT03 but has a lower max.cable length and lower max. supply (5.5V). Check the device you have AM2302 is made by Aosong.

Warning:The serial protocol used is strictly Single-Master-Slave only, meaning that each DHT11 you use has to have a dedicated processor pin and this pin must be bi-directional i.e. can be changed from input to output on the fly.

DHT22 Datasheet

The DHT22 is also known as the RHT03  - the following datasheet references RHT03.

Download RHT03 Datasheet

Note: The DHT11 is also known as the RHT01.

What is Relative Humidity and the Dew point?

Relative Humidity

You can think of relative humidity as "how close the air is to saturation". It is the amount of moisture in the air compared to the amount that the air can hold at that temperature. Video 2 (below) shows how RH is totally misleading.

Dew Point

If you want to know what the air "feels like" then this is the parameter to calculate!

It is the temperature at which the air is saturated with water. Below the dew point water will saturate out onto solid objects since the air can not hold onto the water when the temperature falls i.e. clouds form, rain or snow falls.

Since the dew point is an absolute number it is often used to define "how the air feels"

Note: The Dew Point temperature defines when air is completely saturated.

Dewpoint tables

Dew Point (°F) Dew Point (°C) Description
Below 55°F  (13°C) Air Feels Dry
 55 ~ 60°F (13°C) ~ (16°C) Air Feels Comfortable
 60 ~ 64°F (16°C) ~ (18°C) Air Fairly Humid
 65 ~ 69°F (18°C) ~ (21°C) Humid
 70 ~ 75°F (21°C) ~ (24°C) Very Humid
Above 75°F (24°C) Oppressive

[sources http://www.livescience.imwcom/43269-what-is-dew-point.html, http://www.theweatherprediction.com/habyhints/190/, video 2]

Videos and Some Notes on them

1. Vince Condella : Humidity vs Dew point.

https://www.youtube.com/watch?v=S8W-xl4mcJ8

What is the difference between RH and the Dew point? with easy to understand props.

2. NWS Chicago Humidity vs Dew point.

This video shows visually (and with some numbers) what the difference is and shows why an RH of 100% at 55°F feels better than an RH of 50% at 95°F.

3. Mike Hermann Humidity: Relative and absolute.

The water content of air changes with temperature and hotter air can contain more water than colder air.

Absolute humidity : e.g. 5g/m3

Relative humidity : More commonly used in weather reports - it compares amount of vapor to the maximum it can hold (the dew point). Relative humidity changes with temperature.


Heat Index

If you want to know what the temperature "feels like" then this is the parameter to calculate! The DHT library also contains a function that works out the heat index - this is an attempt to quantify how hot a person will "feel", reported as a Temperature and calculated using Relative Humidity and Air Temperature.

Wikipedia defines it as:

"The heat index (HI) or humiture or humidex  is an index that combines air temperature and relative humidity in an attempt to determine the human-perceived equivalent temperature€”how hot it feels. The result is also known as the "felt air temperature" or "apparent temperature". For example, when the temperature is 32 °C (90 °F) with very high humidity, the heat index can be about 41 °C (106 °F)"
[source http://en.wikipedia.org/wiki/Heat_index]

Note: Heat Index attempts to quantify what Temperature "Feels like".

Humidity Sensor Hardware and software

Arduino Software Setup:

IDE Version Used : Ver 1.6.4
DHT Sensor Library : Ver 1.3.1

Arduino DHT Library

To use the DHT22 change the code in the sketch below (Heading:Arduino Sketch)

//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

Comment out unused sensors leaving your own uncommented.

Library Installation

Github original source is here: dht library on github where you can download and install the library manually.

The above github link is there in case Arduino library manager is changed to not show that library.

An easier way is install it from within the Arduino IDE

  1. Goto Menu Sketch --> Include Library --> Manage Libraries...
  2. In the Filter Search type DHT.
  3. Click on the DHT Sensor Library.
  4. Hit install.

Parts for DHT11 project:

Note: The DHT22 has the same pinout as the DHT11 (except the 3rd pin NC on DHT11 is now GND on the DHT22).
  1. Processor Board : Arduino Uno R3.
  2. DHT11 (either on a breakout board or standalone).
  3. 4k7.
  4. Breadboard.
  5. 10uF Electrolytic.

Note the DHT11 used above, operates identically to the DHT22 it just has a different specification. See table here.

Wiring Layout (Applies to the DHT22 and the DHT11)

dht11 dht22 arduino breadboard circuit

Sensor connections

Looking from the front of the device pin one is to the left. These are the connections:

1 Vcc
2 Signal (Bi-directional) processor is master.
3 N.C. (DHT22 = GND).
4 GND

Arduino Sketch

I added in the dew point code found here along with the output code for displaying the dew point (It was removed at some point from the main code - this is because floating point takes a lot of space and is slow and may not be needed).  Here is the difference:
  • Without Dew Point function and display : code size = 7688 Bytes
  • With Dew Point function and display : code size =6274 Bytes
So just for that function 1400 Bytes are needed!

Create a new sketch, then copy and paste the code below to your own sketch and upload.

Note: Clicking any text in the box below will copy it to the clipboard.

// John Main added dewpoint code from : http://playground.arduino.cc/main/DHT11Lib
// Also added DegC output for Heat Index.
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
double dewPoint(double celsius, double humidity)
{
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.246);

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078); // temp var
  return (241.88 * T) / (17.558 - T);
}

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2 // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

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

  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();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);
  float hiDegC = dht.convertFtoC(hi);

  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(hiDegC);
  Serial.print(" *C ");
  Serial.print(hi);
  Serial.print(" *F ");
  Serial.print("Dew Point (*C): ");
  Serial.println(dewPoint(t, h));
}

Read Function:

This question is asked a lot : What is the function for reading humidity from dht sensor from arduino?

Create an instance of the class:

DHT dht(DHTPIN, DHTTYPE);

Then initialise the library (in the Arduino setup function):

dht.begin();

Then read the humidity and temperature using these functions:

float h = dht.readHumidity();

float t = dht.readTemperature();    // Read temperature as Celsius

float f = dht.readTemperature(true);    // Read temperature as Fahrenheit

DHT Library Notes

Sensor Update

The sensor is slow to change its output value (update) which is 2s for DHT22. The library code will return the previous value if you ask for the data too often (see code snippet below) - if you use a different library check that this is the case. 

Interestingly the DHT11 allows a faster update rate (1s) compared to the DHT22 (2s) but this enables the DHT22 to offer a better relative humidity measurement (2%RH) as opposed to (5%RH) for the DHT11.

Code : in DHT.cpp for public function read(void)

boolean DHT::read(void) {
  uint8_t laststate = HIGH;   uint8_t counter = 0;   uint8_t j = 0, i;   unsigned long currenttime;
  // Check if sensor was read less than two seconds ago and return early
  // to use last reading.   currenttime = millis();   if (currenttime < _lastreadtime) {     // ie there was a rollover     _lastreadtime = 0;   }
  if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {     return true; // return last correct measurement     //delay(2000 - (currenttime - _lastreadtime));   }
  firstreading = false;
/* Serial.print("Currtime: "); Serial.print(currenttime); Serial.print(" Lasttime: "); Serial.print(_lastreadtime); */   _lastreadtime = millis();

Output

This is the output from the Arduino serial monitor showing:
  • Relative Humidity,
  • Temperature,
  • Heat Index,
  • Dew Point.
Arduino DHT11 / DHT22 sensor serial data display

Warnings

This library turns off interrupts as it operates, and uses delay functions to perform the reading and writing of the sensor - this means all other operations are suspended - in general this is a bad idea but it is good to see something working.

As you develop "real" code it will probably be a problem (depending on what you are trying to do) because you will want to do actions in real-time i.e. not have to wait for a humidity sensor "data request" to finish before doing a time critical action.

If you are relying on interrupt code to drive other interfaces e.g. to read a serial interface then data at the serial interface will probably be lost at some point  i.e. when a sensor acquisition is made and the serial data stream is incoming - this will be an intermittent problem since sensor reading and incoming data will not always coincide.

You can find other interrupt driven libraries for the sensor (a reference is made on this page) i.e. delays are not hard coded and functions will test inputs when fired off by an interrupt timer expiring - not by hanging around doing nothing.

Application Ideas:

Shower room : When the air gets too saturated start an extractor fan to get rid of wet air.

Weather Station : Relative Humidity,Temperature and Dew point data logger.

Tumble Dryer : Automatic shut down at a specific humidity level (does not have to be bone dry e.g. for ironing clothes, you need a higher humidity level).

Environmental control : Preservation of books/materials ensuring that the air is not saturated i.e. measuring the dew point - and automatic action when the temperature gets close to the dew point - e.g. extractor fans and/or controlling temperature.

Humidity Control System : Control the amount of water in the air using a microcontroller and water vaporiser e.g. an ultrasonic vaporiser.



Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.




Privacy Policy | Contact | About Me

Site Map | Terms of Use