Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
:
:
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine.

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 needs to measure temperature (the temperature value compensates the humidity value automatically) but you can still obtain the temperature value from the DHT22 - a two for one bonus! Measurements you can make:

  • Humidity
  • Temperature
  • Dew Point
  • Heat Index

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.


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 (so you should only use them if you really need them as floating point code takes more memory and is slower).

Note: This sensor can be used with any microcontroller since all it needs is a bi-directional I/O pin and a bit of code.

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 also show show you how you can use the DHT22 in your own Arduino sketches.

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 (it should be 5k-1k) then the distance is up-to 20m.

Maximum Sensor 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.

DHT22 and DHT11 Humidity Accuracy

To measure humidity accurately, both measure temperature, since to a large extent the humidity of a gas is dependent on temperature - as a bonus you can retrieve the temperature value - but note that it is not that accurate for the DHT11; It is quite good for the DHT22.

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

Relative Humidity

In fact 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).

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.
Datasheet Download Datasheet

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.

Note: 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.

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.

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

Arduino Software Setup for DHT22 and DHT11 Sensors:

IDE Version Used : 1.6.4
Board used : Arduino Uno R3
Device Used : DHT11 (either on a breakout board or standalone).

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. DHT11.
  2. 4k7.
  3. Breadboard.
  4. Arduino Uno R3.
  5. 10uF Electrolytic.

Wiring Layout DHT22 and DHT11

dht11 dht22 arduino breadboard circuit

DHT11 & DHT22 connections

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

Arduino Library

Note: To use the DHT22 change the code in the sketch below
#define DHTTYPE DHT11
by uncommenting the code corresponding to the sensor you are using and re-commenting the others.

The original code is from the dht library example. But you can install it from within the Arduino IDE easily:
  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.

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

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. 

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

DHT22 and DHT11 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 that when not ironing).

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.

Jump from DHT22 to the Home page.

New! Comments

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



Claim Your: Useful

"Arduino Software Guide"

   Right Now...





Privacy Policy | Contact | About Me

Site Map | Terms of Use



Visit our Facebook Page:

   Click Here



Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:


And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:

Now



:
:
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:



But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates


Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.



Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's

SUPERB and FANTASTIC."

- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan

bursach<at>gmail.com<

"Awesome site,
very, very easy and nice
to navigate!"


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.
Regards."


- Anon

Recent Articles

  1. How to use the DHT22 (or DHT11) with an Arduino; full code and description. Also including a comparison of the DHT11 vs DHT22.

    The Essential Guide to the DHT22/11 humidity sensor including a library code guide for the Arduino. Learn how to determine Humidity, Dew point and Heat Index.

    Read more

  2. How to Use the MAX7219 to drive an 8x8 LED display Matrix on the Arduino.

    The Essential Guide to the MAX7219; What it is and how you can easily use one in any of your projects.

    Read more

  3. How to use a DS18B20 (single or multiple on one wire) for temperature measurement using the Arduino Uno.

    How to Easily Use the DS18B20 Maxim One-Wire thermometer with Arduino example code showing external or parasitic power modes.

    Read more