# DHT11, DHT22 and AM2302 Sensors

## Overview

This tutorial covers the low cost [DHT temperature & humidity sensors](http://www.adafruit.com/category/35_66 "Link: http://www.adafruit.com/category/35\_66"). 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](http://learn.adafruit.com/thermistor). 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.

![](https://cdn-learn.adafruit.com/assets/assets/000/000/576/medium800/weather_dhtsensors.jpg?1396764183)

## 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://www.adafruit.com/products/386 "Link: http://www.adafruit.com/products/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://www.adafruit.com/products/385)/ [AM2302](https://www.adafruit.com/product/393) (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
- 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&nbsp;[DHT22](http://www.adafruit.com/products/385) / [AM2302](https://www.adafruit.com/product/393) 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&nbsp;[DHT11](http://www.adafruit.com/products/386)&nbsp;and&nbsp;[DHT22](http://www.adafruit.com/products/385) or [AM2302](https://www.adafruit.com/product/393) from the adafruit shop!**

# DHT11, DHT22 and AM2302 Sensors

## 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.![](https://cdn-learn.adafruit.com/assets/assets/000/000/577/medium800/weather_dhtbreadboard.jpg?1396764191)

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, it's 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

Info: 

This diagram shows how we will connect for the testing sketch. Connect data to pin 2, you can change it later to any pin.

![](https://cdn-learn.adafruit.com/assets/assets/000/000/578/medium800/weather_dhtwiring.gif?1447976206)

# DHT11, DHT22 and AM2302 Sensors

## Using a DHTxx Sensor with Arduino

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://www.arduino.cc/en/Main/Software) software running at this time. Next it’s necessary to install our DHT library, which can be done though the Arduino Library Manager:

**Sketch→Include Library→Manage 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.

![](https://cdn-learn.adafruit.com/assets/assets/000/069/265/medium800/weather_dht-lib-1.png?1547267322)

 **IMPORTANT:** &nbsp;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:

![](https://cdn-learn.adafruit.com/assets/assets/000/069/266/medium800/weather_dht-lib-2.png?1547267332)

Now load up the&nbsp; **Examples→DHT→DHTtester** &nbsp;sketch

![](https://cdn-learn.adafruit.com/assets/assets/000/000/579/medium800/weather_dhttester.gif?1447976216)

If you're using a&nbsp; **DHT11** &nbsp;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!![](https://cdn-learn.adafruit.com/assets/assets/000/000/580/medium800/weather_dhtout.gif?1447976226)

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

# DHT11, DHT22 and AM2302 Sensors

## DHT CircuitPython Code

Warning: 

# Adafruit CircuitPython Module Install

To use the&nbsp;DHT sensor&nbsp;with your&nbsp;Adafruit CircuitPython&nbsp;board you'll need to install the&nbsp;[Adafruit\_CircuitPython\_DHT](https://github.com/adafruit/Adafruit_CircuitPython_DHT)&nbsp;module on your board.

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](https://circuitpython.org/downloads) for your board.

Next you'll need to install the necessary libraries&nbsp;to use the hardware--carefully follow the steps to find and install these libraries from&nbsp;[Adafruit's CircuitPython library bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle).&nbsp; Our introduction guide has&nbsp;[a great page on how to install the library bundle](../../../../welcome-to-circuitpython/circuitpython-libraries)&nbsp;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&nbsp; **adafruit\_dht.mpy** &nbsp;from&nbsp;[its releases page on Github](https://github.com/adafruit/Adafruit_CircuitPython_DHT/releases).

Before continuing make sure your board's lib folder or root filesystem has the&nbsp; **adafruit\_dht.mpy&nbsp;** module&nbsp;copied over.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/108/medium800/weather_dhtfile.png?1515800171)

# 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**

Warning: 

Here's an example using a Trinket M0 - **you can use any CircuitPython board** , just check that the Data pin is `pulseio`-capable.

![weather_adafruit_gemma_dhtcirpy_bb.png](https://cdn-learn.adafruit.com/assets/assets/000/050/100/medium640/weather_adafruit_gemma_dhtcirpy_bb.png?1515799704)

In this example we'll use a Feather M0 and DHT22 sensor connected to pin D6

![weather_m0_dht_bb.png](https://cdn-learn.adafruit.com/assets/assets/000/050/101/medium640/weather_m0_dht_bb.png?1515799866)

[Fritzing Source](https://cdn-learn.adafruit.com/assets/assets/000/047/595/original/m0_dht.fzz?1508892874)
# Usage

To&nbsp;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&nbsp;[connect to the board's serial REPL&nbsp;](../../../../welcome-to-circuitpython/the-repl)so you are at the CircuitPython&nbsp; **\>\>\>** &nbsp;prompt.

Next import the **board** and **adafruit\_dht** modules, these are necessary modules to initialize and access the sensor:

```
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).&nbsp; 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!&nbsp; You can do this by reading the **temperature** property which returns temperature in degrees Celsius:

```
dht.temperature
```

![](https://cdn-learn.adafruit.com/assets/assets/000/047/431/medium800/weather_Screen_Shot_2017-10-20_at_2.46.03_PM.png?1508536002)

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
```

![](https://cdn-learn.adafruit.com/assets/assets/000/047/432/medium800/weather_Screen_Shot_2017-10-20_at_2.46.23_PM.png?1508536069)

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.&nbsp; 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.&nbsp; 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

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Introducing_Gemma_M0/dht22/code.py

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)

![](https://cdn-learn.adafruit.com/assets/assets/000/050/105/medium800/weather_adafruit_gemma_dht.png?1515800078)

# DHT11, DHT22 and AM2302 Sensors

## Python Docs

# DHT11, DHT22 and AM2302 Sensors

## Downloads

- [Arduino library and example code for DHT sensors](https://github.com/adafruit/DHT-sensor-library)
- [Adafruit\_Sensor library](https://github.com/adafruit/Adafruit_Sensor) (required by the DHT library above)
- [DHT11 datasheet&nbsp;](http://www.adafruit.com/datasheets/DHT11-chinese.pdf)(in chinese, so see the DHT22 datasheet too!)
- [DHT22 datasheet](http://www.adafruit.com/datasheets/DHT22.pdf)
- [K&R Smith calibration notes](http://www.kandrsmith.org/RJS/Misc/Hygrometers/calib_many.html)

# Simulator

You can try out a [DHT simulator by Wowki](https://wokwi.com/) here: [https://wokwi.com/arduino/libraries/DHT-sensor-library](https://wokwi.com/arduino/libraries/DHT-sensor-library)

![](https://cdn-learn.adafruit.com/assets/assets/000/094/467/medium800/weather_image.png?1598807097)


## Featured Products

### DHT22  temperature-humidity sensor + extras

[DHT22  temperature-humidity sensor + extras](https://www.adafruit.com/product/385)
The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and spits out a digital signal on the data pin (no analog input pins needed). It's fairly simple to use but requires careful timing...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/385)
[Related Guides to the Product](https://learn.adafruit.com/products/385/guides)
### DHT11 basic temperature-humidity sensor + extras

[DHT11 basic temperature-humidity sensor + extras](https://www.adafruit.com/product/386)
 **Discontinued -**  **you can grab the&nbsp;** [DHT20 - AHT20 Pin Module - I2C Temperature and Humidity Sensor](https://www.adafruit.com/product/5183)&nbsp; **instead!&nbsp;**

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/386)
[Related Guides to the Product](https://learn.adafruit.com/products/386/guides)
### AM2302 (wired DHT22)  temperature-humidity sensor

[AM2302 (wired DHT22)  temperature-humidity sensor](https://www.adafruit.com/product/393)
Discontinued - [**you can grab** AM2301B - Wired Enclosed AHT20 - Temperature and Humidity Sensor&nbsp; **instead!**](https://www.adafruit.com/product/5181)

The AM2302 is a wired version of the [DHT22](http://www.adafruit.com/products/385), in a large plastic...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/393)
[Related Guides to the Product](https://learn.adafruit.com/products/393/guides)

## Related Guides

- [Adafruit VEML6070 UV Sensor Breakout](https://learn.adafruit.com/adafruit-veml6070-uv-light-sensor-breakout.md)
- [Create an Internet of Things Dashboard with Adafruit IO](https://learn.adafruit.com/create-an-internet-of-things-dashboard-with-adafruit-dot-io.md)
- [Calibrating Sensors](https://learn.adafruit.com/calibrating-sensors.md)
- [Trinket Temperature & Humidity LCD Display](https://learn.adafruit.com/trinket-temperature-humidity-lcd-display.md)
- [IoT Temperature Logger with Analog Devices ADT7410, Feather and Adafruit IO](https://learn.adafruit.com/iot-temperature-logger-with-arduino-and-adafruit-io.md)
- [Adafruit BMP280 Barometric Pressure + Temperature Sensor Breakout](https://learn.adafruit.com/adafruit-bmp280-barometric-pressure-plus-temperature-sensor-breakout.md)
- [Manually bridging MQTT to Adafruit.IO](https://learn.adafruit.com/manually-bridging-mqtt-mosquitto-to-adafruit-io.md)
- [Weather Wise Wifi Umbrella Stand](https://learn.adafruit.com/weather-wise-wifi-umbrella-stand.md)
- [Using LoraWAN and The Things Network with CircuitPython](https://learn.adafruit.com/using-lorawan-and-the-things-network-with-circuitpython.md)
- [ESP8266 WiFi Weather Station with Color TFT Display](https://learn.adafruit.com/wifi-weather-station-with-tft-display.md)
- [Adafruit CCS811 Air Quality Sensor](https://learn.adafruit.com/adafruit-ccs811-air-quality-sensor.md)
- [Purple Air AQI Display](https://learn.adafruit.com/purple-air-aqi-display.md)
- [Adafruit SGP30 TVOC/eCO2 Gas Sensor](https://learn.adafruit.com/adafruit-sgp30-gas-tvoc-eco2-mox-sensor.md)
- [Weather Display Matrix](https://learn.adafruit.com/weather-display-matrix.md)
- [No-Code Indoor Air Quality Monitor with Separate Display](https://learn.adafruit.com/no-code-indoor-air-quality-monitor-with-separate-display.md)
