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:
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
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 defined 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.
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.
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:
Relative Humidity Range
20 ~ 90%
0 ~ 100%
Relative Humidity Accuracy:
Long Term Stability
±1% per year
±0.5% per year
Operating Temperature Range
0 ~ 50°C
-40 ~ 80°C
3,3V ~ 5.5V
0.5mA ~ 2.5mA
1 ~ 1.5mA
Idle Supply Current:
100uA ~ 150uA
40 ~ 50uA
Max sampling period
(Max device update rate).
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
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.
The DHT22 is also known as the RHT03 - the following datasheet references RHT03.
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
If you want to know what the air "feels like" then this is the parameter to
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
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)
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)
70 ~ 75°F
(21°C) ~ (24°C)
http://www.theweatherprediction.com/habyhints/190/, video 2]
Videos and Some Notes on them
1. Vince Condella : Humidity vs Dew point.
What is the difference between RH and the Dew point? with easy to understand
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.
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
Wikipedia defines it as:
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)"
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)
Comment out unused sensors leaving your own uncommented.
Github original source is here: dht
library on github where you can download and install the library
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
Goto Menu Sketch --> Include Library --> Manage Libraries...
In the Filter Search type DHT.
Click on the DHT Sensor Library.
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).
Processor Board : Arduino Uno R3.
DHT11 (either on a breakout board or standalone).
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)
Looking from the front of the device pin one is to the left. These are the connections:
2 Signal (Bi-directional) processor is master.
3 N.C. (DHT22 = GND).
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
Note: Clicking any text in the box below will copy it to the
// 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
// (1) Saturation Vapor Pressure = ESGG(T)
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
// (2) DEWPOINT = F(Vapor Pressure)
doubleT=log(VP/0.61078);// temp var
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#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
// 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);
// Wait a few seconds between measurements.
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius
// Read temperature as Fahrenheit
// Check if any reads failed and exit early (to try again).
Serial.println("Failed to read from DHT sensor!");
// Compute heat index
// Must send in temp in Fahrenheit!
Serial.print(" *C ");
Serial.print("Heat index: ");
Serial.print(" *C ");
Serial.print(" *F ");
Serial.print("Dew Point (*C): ");
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):
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
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)
unsignedlongcurrenttime; // Check if sensor was read less than two seconds ago and return early // to use last reading.
// ie there was a rollover
returntrue;// return last correct measurement
//delay(2000 - (currenttime - _lastreadtime));
} firstreading=false; /*
Serial.print("Currtime: "); Serial.print(currenttime);
Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
This is the output from the Arduino serial monitor showing:
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
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
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.
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
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