DHTxx Sensors Datasheet by Adafruit Industries LLC

nrlnh mt lenrnmg 5y
DHT11, DHT22 and AM2302 Sensors
Created by lady ada
Last updated on 2020-10-17 01:58:27 AM EDT
k annflult \Cmning system
Overview
This tutorial covers the low cost DHT temperature & humidity sensors (https://adafru.it/aJU). These sensors are very
basic and slow, but are great for hobbyists who want to do some basic data logging. The DHT sensors are made of
two parts, a capacitive humidity sensor and a thermistor (https://adafru.it/aHD). There is also a very basic chip inside
that does some analog to digital conversion and spits out a digital signal with the temperature and humidity. The digital
signal is fairly easy to read using any microcontroller.
DHT11 vs DHT22
We have two versions of the DHT sensor, they look a bit similar and have the same pinout, but have different
characteristics. Here are the specs:
DHT11 (http://adafru.it/386)
Ultra low cost
3 to 5V power and I/O
2.5mA max current use during conversion (while requesting data)
Good for 20-80% humidity readings with 5% accuracy
Good for 0-50°C temperature readings ±2°C accuracy
No more than 1 Hz sampling rate (once every second)
Body size 15.5mm x 12mm x 5.5mm
4 pins with 0.1" spacing
DHT22 (http://adafru.it/385)/ AM2302 (https://adafru.it/uF2) (Wired version)
Low cost
3 to 5V power and I/O
2.5mA max current use during conversion (while requesting data)
Good for 0-100% humidity readings with 2-5% accuracy
© Adafruit Industries https://learn.adafruit.com/dht Page 3 of 16
Good for -40 to 80°C temperature readings ±0.5°C accuracy
No more than 0.5 Hz sampling rate (once every 2 seconds)
Body size 15.1mm x 25mm x 7.7mm
4 pins with 0.1" spacing
As you can see, the DHT22 (http://adafru.it/385) / AM2302 (https://adafru.it/uF2) is a little more accurate and good over
a slightly larger range. Both use a single digital pin and are 'sluggish' in that you can't query them more than once
every second or two.
You can pick up both the DHT11 (http://adafru.it/386) and DHT22 (http://adafru.it/385) or
AM2302 (https://adafru.it/uF2) from the adafruit shop!
© Adafruit Industries https://learn.adafruit.com/dht Page 4 of 16
* adafrult learning system l l l l l l l l l | I I l I | l | |
Connecting to a DHTxx
Sensor
Luckily it is trivial to connect to these sensors, they have fairly long 0.1"-pitch pins so you can plug them into any
breadboard, perfboard or similar.
Likewise, it is fairly easy to connect up to the DHT sensors. They have four pins
1. VCC - red wire Connect to 3.3 - 5V power. Sometime 3.3V power isn't enough in which case try 5V power.
2. Data out - white or yellow wire
3. Not connected
4. Ground - black wire
Simply ignore pin 3, its not used. You will want to place a 10 Kohm resistor between VCC and the data pin, to act as a
medium-strength pull up on the data line. The Arduino has built in pullups you can turn on but they're very weak, about
20-50K
AM2302 (wired DHT22) temperature-humidity sensor
$15.00
IN STOCK
Add To Cart
© Adafruit Industries https://learn.adafruit.com/dht Page 5 of 16
This diagram shows how we will connect for the testing sketch. Connect data to pin 2, you can change it later to any
pin.
If you have an AM2302
DHT22 and AM2302 often have a pullup already inside, but it doesn't hurt to add another one!
© Adafruit Industries https://learn.adafruit.com/dht Page 6 of 16
* adafruit learning system 0 0 mm Managu M. An 5 10...: All a an. Inn u... lbw—v w mm a... .uv H mm, mu. m nan - mm 5...... mm mm, m amt, am, at Yum: l mm m M v. Mm I u...» "man... by Mun-m W... n...“ Wm an...” A mm m." .m law m w mm, mm: m." nun. m V. Mm 3
Using a DHTxx
Sensor
To test the sketch, we'll use an Arduino. You can use any micrcontroller that can do microsecond timing, but since its a
little tricky to code it up, we suggest verifying the wiring and sensor work with an Arduino to start.
You should have the Arduino IDE (https://adafru.it/fvm) software running at this time. Next it’s necessary to install our
DHT library, which can be done though the Arduino Library Manager:
SketchInclude LibraryManage Libraries…
Enter “dht” in the search field and look through the list for “DHT sensor library by Adafruit.” Click the “Install” button,
or “Update” from an earlier version.
IMPORTANT: As of version 1.3.0 of the DHT library you will also need to install the Adafruit Unified Sensor library,
which is also available in the Arduino Library Manager:
Now load up the ExamplesDHTDHTtester sketch
© Adafruit Industries https://learn.adafruit.com/dht Page 7 of 16
DHTIesml \ Alduina 0022 Fl: Edi Sketch Vods Hab H “”144: r: rm; 5?: h m1 r )H m-n‘ 'muubhrj’rempéLiflu: 52mm: u mnm r: ilLarh, nun: 10111;;n nuclude ”DHT.h” manna DHTPIII 2 , ma: p)“ we‘xe cinna- w m- Mrnr Amara 1‘: 1m»: v ng‘ uumn: Dx-ITF'IE m-nu , , Mr L manna mn'rm: wrrzz , mr mu nu r . mum 1 m , u m mm mm YuuL ”mm 1: ugm Hi the iensn' r: \ZFH'flK‘ L [.04 mn . mum mm A \ptuuw of the DHT dhtmm'nu, DHTI'YPE); vom set-m) <><>
If you're using a DHT11 sensor, comment out the line that sets the type:
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
and uncomment the line that says:
#define DHTTYPE DHT11 // DHT 11
This will make the data appear correctly for the correct sensor. Upload the sketch!
© Adafruit Industries https://learn.adafruit.com/dht Page 8 of 16
mam :esr.\ sunny: 3:.7u Hunxdicy: 32.7n Hulndxcy: 32,70 Huludlcy: 32,-m 32.5w 32.9: SLED 32,50 32.5w 32.9: 32.5u 32,50 szvsu 32.511 32.9: 22,5n rempexaw: . Temparacur Taupexacurs '[enperacur - rempexaw: - Temperecur . rupamzun. fA-paracura' tempusw: Temperanur - rupmzun. felpexacura Temperature: Temperanur Tampixacuxi. Taupexacurs 25.7w 25.7u 25,7n szu 25.7u 25.7u 25mm 25,70 zquu 25.4u 254m 25,40 zsvau zs.su 25.9u 25,9n *c ~: *1: n: *c ta: «c n: ~c ta: «c ,c n: ta: ~: *1: caragavetum v 96am baud v v
You should see the temperature and humidity. You can see changes by breathing onto the sensor (like you would to
fog up a window) which should increase the humidity.
You can add as many DHT sensors as you line on individual pins, just add new lines such as
DHT dht2 = DHT(
pin
,
type
);
below the declaration for the initial dht object, and you can reference the new dht2 whenever you like.
© Adafruit Industries https://learn.adafruit.com/dht Page 9 of 16
* adafruit learning system c Elvin-s Hill! A DIM MMI'IEd ‘W‘im I admumfl‘lmw Yuday, 2:11 PM 3 Remote Disc , hooLwtm Sen 1, 1016, 4:43 PM a c c
DHT CircuitPython Code
Adafruit CircuitPython Module Install
To use the DHT sensor with your Adafruit CircuitPython board you'll need to install
the Adafruit_CircuitPython_DHT (https://adafru.it/Beq) module on your board.
First make sure you are running the latest version of Adafruit CircuitPython (https://adafru.it/Amd) for your board. In
particular for Gemma M0, Trinket M0, and M0 basic boards you must be running CircuitPython 2.1.0 or higher to have
access to the necessary pulseio module!
Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these
libraries from Adafruit's CircuitPython library bundle (https://adafru.it/zdx). Our introduction guide has a great page on
how to install the library bundle (https://adafru.it/ABU) for both express and non-express boards.
Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:
adafruit_dht.mpy
You can also download the adafruit_dht.mpy from its releases page on Github (https://adafru.it/Ber).
Before continuing make sure your board's lib folder or root filesystem has the adafruit_dht.mpy module copied over.
Wiring
DHT wiring is very simple:
The left-most pin is power. We recommend powering from 5V (sometimes 3V is not enough) - this is OK even if
you are using 3.3V logic
The second pin is data. Connect a 10K pullup resistor from this pin to 3.3V. If you are using a DHT11 it's required.
If you're using a DHT22 or AM2302 you can sometimes leave this off
Skip the third pin
The right-most pin is ground
For the DATA pin you must pick a pin that has PWM support (pulseio) - Check the board's guide for what pins
have timers available
© Adafruit Industries https://learn.adafruit.com/dht Page 10 of 16
fn‘rvmn
Here's an example using a Trinket M0 - you can use any
CircuitPython board, just check that the Data pin is
pulseio -capable.
In this example we'll use a Feather M0 and DHT22
sensor connected to pin D6
https://adafru.it/A0o
https://adafru.it/A0o
Usage
To demonstrate the usage of the DHT sensor module you can connect to your board's serial REPL and run Python
code to read the temperature and humidity.
Next connect to the board's serial REPL (https://adafru.it/Awz)so you are at the CircuitPython >>> prompt.
Next import the board and adafruit_dht modules, these are necessary modules to initialize and access the sensor:
© Adafruit Industries https://learn.adafruit.com/dht Page 11 of 16
T22 pa
import board
import adafruit_dht
You may also want to try powering the DHT sensor from 5V (we found sometimes it really needs more power) but still
having the 10K pull-up resistor to 3.3V volts)
Now create an instance of either the DHT11 or DHT22 class, depending on the type of sensor you're using (for the
AM2302 sensor use the DHT22 class). You must pass in the pin which is connected to the signal line, for example a
DHT22 or AM2302 sensor connected to board pin D6 would need this code:
dht = adafruit_dht.DHT22(board.D6)
Note for a DHT11 sensor you'd instead use adafruit_dht.DHT11 in place of the adafruit_dht.DHT22 code above.
At this point you're all set and ready to start reading the temperature and humidity! You can do this by reading the
temperature property which returns temperature in degrees Celsius:
dht.temperature
To read the humidity grab the value of the humidity property, it will return the percent humidity as a floating point
value from 0 to 100%:
dht.humidity
In most cases you'll always get back a temperature or humidity value when requested, but sometimes if there's
electrical noise or the signal was interrupted in some way you might see an exception thrown to try again. It's normal
for these sensors to sometimes be hard to read and you might need to make your code retry a few times if it fails to
read. However if you always get errors and can't ever read the sensor then double check your wiring (don't forget the
pull-up resistor if needed!) and the power to the device.
Example Code
Here's a full example sketch which also manages error-retry logic (which will happen once in a while.
Don't forget to change the logic pin to whatever pin you're using! Then save this as main.py on your CircuitPython
board
© Adafruit Industries https://learn.adafruit.com/dht Page 12 of 16
«pm beard (m (in: 1-,»: adaimmdm nu- 1m: my: . . m = adafnnt_dht.m122\bnzrd.u2) s . tenpnrztum 2 dht.tnmperzmre 15,3 «c any: snag 254 -c Midity: was 25.0 1.: “why 521.55 25.5 -c “any 54.9: v 25,5 ac Midflyr 55.1 25.; ‘c Mid'lty' 51.». 25,1 Ac Midhy, sun
import time
import adafruit_dht
import board
dht = adafruit_dht.DHT22(board.D2)
while True:
try:
temperature = dht.temperature
humidity = dht.humidity
# Print what we got to the REPL
print("Temp: {:.1f} *C \t Humidity: {}%".format(temperature, humidity))
except RuntimeError as e:
# Reading doesn't always work! Just print error and we'll try again
print("Reading from DHT failure: ", e.args)
time.sleep(1)
If you are using a DHT11, change the code to use a adafruit_dht.DHT11(board.D2) object.
Open the REPL to see the output! Breathe on the sensor to see it move temperature and humidity up (unless you are a
White Walker in which case the temperature will go down)
© Adafruit Industries https://learn.adafruit.com/dht Page 13 of 16
* adafruit learning system
Python
Docs
Python Docs (https://adafru.it/C4a)
© Adafruit Industries https://learn.adafruit.com/dht Page 14 of 16
«~2me “WWW . “mm w h “(rm pmu ‘ mu * adafrult learning system inmu‘nan Sm: 5w wax umum 59mm manage Wmnnramrs WW Seusar ‘yp wzz Dru/w v“. I Umw: : 71 wax Vane, 12mm m Value: amwz Resulutwn Mam Hummny 5mm Sensor ‘vné' [W22
Downloads
Arduino library and example code for DHT sensors (https://adafru.it/aJX)
Adafruit_Sensor library (https://adafru.it/aZm) (required by the DHT library above)
DHT11 datasheet (https://adafru.it/aJY)(in chinese, so see the DHT22 datasheet too!)
DHT22 datasheet (https://adafru.it/aJZ)
K&R Smith calibration notes (https://adafru.it/BfU)
Simulator
You can try out a DHT simulator by Wowki (https://adafru.it/N8B) here: https://wokwi.com/arduino/libraries/DHT-sensor-
library (https://adafru.it/Ncg)
© Adafruit Industries https://learn.adafruit.com/dht Page 15 of 16
© Adafruit Industries Last Updated: 2020-10-17 01:58:27 AM EDT Page 16 of 16