Sensors/ Environment/ DHT22 / AM2302

DHT22 / AM2302 Temperature and Humidity Sensor

The DHT22 is a versatile and affordable sensor for measuring temperature and humidity. It provides calibrated digital output and is easy to interface with microcontrollers. With a temperature measurement range of -40°C to 80°C and humidity range of 0% to 100%, the DHT22 is suitable for a variety of applications, including environmental monitoring and HVAC systems.

DHT22 / AM2302 Temperature and Humidity Sensor image
DHT22 / AM2302 · Analog
Analog
Interface
4pins
Connections
3.3-5.5V
Supply
±0.5°C
Accuracy
±2% RH
Humidity accuracy
$5
Typical price
On this page

DHT22 / AM2302 pinout

4 pins · Analog

The DHT22/AM2302 uses the same 4-pin layout as the DHT11, with improved accuracy and wider measurement range.

View:
DHT22 / AM2302 Temperature and Humidity Sensor pinout
PinTypeDescriptionNotes
VCCPowerPower supply input (3.3V to 6V)Works with both 3.3V and 5V logic levels
DATACommunicationDigital signal outputSingle-wire digital interface for data transmission
NCControlNot connectedLeave this pin unconnected
GNDPowerGround connectionConnect to ESP32 ground
  • Uses proprietary single-wire digital protocol

  • Requires 5kΩ to 10kΩ pull-up resistor on DATA pin

  • Higher accuracy than DHT11 (±0.5°C vs ±2°C)

  • Can be read once every 2 seconds

  • Operates in wider temperature range (-40°C to 80°C)

Wiring the DHT22 / AM2302 to ESP32

4 connections · all required

Connect the DHT22/AM2302 using the single-wire digital interface with a pull-up resistor for stable readings.

DHT22 / AM2302 Temperature and Humidity Sensor wiring with ESP32
DHT22 / AM2302 pinESP32 pinPurpose
VCC3.3VPower supply (3.3V to 6V supported)
GNDGNDGround connection
DATAGPIO4Digital data line
DATA (Pull-up)5kΩ to VCCPull-up resistor for reliable communication
  • Any GPIO pin works - GPIO4 is commonly used

  • 5kΩ pull-up resistor recommended (10kΩ also works)

  • Use 3.3V for ESP32, sensor supports up to 6V

  • Minimum 2 second interval between readings

  • Better accuracy than DHT11 for demanding applications

DHT22 / AM2302 code examples

5 platforms
Platform:

DHT22 / AM2302 Arduino example

Copy
#include "DHT.h"

#define DHTPIN 4     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22   // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println("DHT22 Sensor Example");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  float humidity = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float temperature = dht.readTemperature();

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

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %	");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
}

This Arduino sketch demonstrates how to interface with the DHT22 sensor. It initializes the sensor on digital pin 4 and reads the temperature and humidity every 2 seconds. The readings are then printed to the Serial Monitor. The code includes checks to ensure that the sensor readings are valid.

DHT22 / AM2302 ESP-IDF example

Copy
// Requires the esp-idf-lib DHT driver (ESP Component Registry):
//   idf.py add-dependency "esp-idf-lib/dht^1.2.0"

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "dht.h"

#define DHT_GPIO  GPIO_NUM_4
#define DHT_TYPE  DHT_TYPE_AM2301   // DHT22 / AM2302

void app_main(void)
{
    float hum, temp;

    while (1) {
        if (dht_read_float_data(DHT_TYPE, DHT_GPIO, &hum, &temp) == ESP_OK) {
            printf("Hum %.1f%%  Temp %.1f C\n", hum, temp);
        } else {
            printf("Could not read data from sensor\n");
        }

        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}

ESP-IDF ships no DHT driver of its own, so this example uses the maintained esp-idf-lib DHT driver from the ESP Component Registry. Install it into your project first with idf.py add-dependency "esp-idf-lib/dht^1.2.0", then build as usual.

dht_read_float_data() performs one complete read and fills in the humidity and temperature values, returning ESP_OK on success. DHT_TYPE_AM2301 is the correct sensor type for the DHT22 (also sold as AM2302); for a DHT11 use DHT_TYPE_DHT11 instead. No extra include is needed for GPIO_NUM_4 - dht.h pulls in the GPIO driver. The loop prints a reading every 2 seconds, matching the sensor's 0.5 Hz sampling rate.

DHT22 / AM2302 ESPHome example

Copy
sensor:
  - platform: dht
    pin: GPIO4
    model: DHT22
    temperature:
      name: "Living Room Temperature"
    humidity:
      name: "Living Room Humidity"
    update_interval: 60s

The ESPHome configuration utilizes the dht platform to define the DHT22 sensor. The pin specifies the GPIO pin where the sensor’s DATA pin is connected (e.g., GPIO4). The model is set to DHT22 to ensure accurate data interpretation. Two sensor entities are defined: one for temperature and one for humidity, each assigned a user-friendly name like ‘Living Room Temperature’ and ‘Living Room Humidity.’ The update_interval is set to 60 seconds, meaning the sensor will update the readings every minute.

DHT22 / AM2302 PlatformIO example

Copy
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps = 
    adafruit/DHT sensor library @ ^1.4.2
monitor_speed = 115200
src/main.cppCopy
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    Serial.println("DHT22 Sensor Example");

    dht.begin();
}

void loop() {
    // Wait a few seconds between measurements
    delay(2000);

    // Reading temperature or humidity takes about 250 milliseconds!
    float humidity = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float temperature = dht.readTemperature();

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

    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.print(" %	");
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" °C");
}

This PlatformIO sketch demonstrates how to use the DHT22 sensor with an ESP32 board. It initializes the sensor on GPIO pin 4 and reads temperature and humidity data every 2 seconds. The results are printed to the Serial Monitor. The code includes checks to ensure the sensor data is valid, retrying if the readings are invalid.

DHT22 / AM2302 MicroPython example

Copy
from machine import Pin
from time import sleep
import dht

# Initialize the DHT22 sensor
sensor = dht.DHT22(Pin(4))

print('DHT22 Sensor Example')

while True:
    sensor.measure()
    temperature = sensor.temperature()  # Temperature in Celsius
    humidity = sensor.humidity()  # Relative Humidity in %

    print('Temperature: {} °C'.format(temperature))
    print('Humidity: {} %'.format(humidity))

    sleep(2)

This MicroPython code uses the dht module to interact with the DHT22 sensor. It initializes the sensor on GPIO pin 4, reads temperature and humidity every 2 seconds, and prints the results to the console. The sensor.measure() function is used to fetch the data from the sensor, and the temperature() and humidity() methods are used to extract the values.

DHT22 / AM2302 specifications

From the datasheet
Operating Voltage
3.3V to 5.5V
Temperature Range
-40°C to 80°C
Humidity Range
0% to 100% RH
Temperature Accuracy
±0.5°C
Humidity Accuracy
±2% RH
Sampling Rate
0.5 Hz (once every 2 seconds)
Dimensions
15.1mm x 25mm x 7.7mm

About the DHT22 / AM2302

The DHT22 (also sold wired into a housing as the AM2302) is the DHT11’s more capable sibling: the same capacitive-humidity-plus-thermistor design and single-wire digital protocol, but with markedly better numbers - ±0.5 degC and ±2% RH accuracy over a much wider -40 to 80 degC range, against the DHT11’s ±2 degC / ±5% RH and 0-50 degC ceiling.

The tradeoff is that the DHT22’s single-wire timing is tighter than the DHT11’s, and it’s genuinely sensitive to wiring: multiple independent reports trace intermittent read failures to long cable runs, where the added capacitance rounds off the signal’s rising edges until the sensor stops responding reliably. Keep the data line short, use a solid pull-up, and consider a twisted pair with a shared ground if you need to run it more than a meter or two. Expect one reading every 2 s (0.5 Hz) at best.

For the price, the DHT22’s ±2% RH humidity accuracy is no longer class-leading - I2C sensors like the SHT40 match or beat it while skipping the timing-critical wiring entirely - but the DHT22 remains a solid, thoroughly documented choice, especially if you need its wide temperature range or already have code built around it. If only temperature matters, the DS18B20 covers a wider range still; for less accuracy at a lower price, there’s the DHT11.

DHT22 / AM2302 troubleshooting

4 common issues

Failed to Read from DHT Sensor

Issue: Receiving 'Failed to read from DHT sensor!' or NaN readings when attempting to retrieve data from the DHT22 (AM2302) sensor.

Possible causes include incorrect wiring, insufficient power supply, or improper sensor initialization.

Solution: Double-check the wiring connections: ensure VCC is connected to 3V to 6V, GND to ground, and the data pin to the appropriate GPIO pin on the microcontroller. Verify that a suitable pull-up resistor (typically 5kΩ) is connected between the data pin and VCC. Ensure the sensor is properly initialized in the code, and that the correct sensor type is specified in the library. Additionally, consider increasing the time between sensor readings, as the DHT22 has a sampling rate of once every 2 seconds.

Incorrect Readings at Sub-Zero Temperatures

Issue: The DHT22 sensor provides incorrect temperature readings when the ambient temperature falls below 0°C (32°F).

Possible causes include limitations in the sensor's design or firmware that affect accuracy at low temperatures.

Solution: Be aware that the DHT22 may have reduced accuracy at temperatures below freezing. For applications requiring precise measurements in sub-zero conditions, consider using a sensor specifically designed for low-temperature accuracy.

Sensor Stops Responding After Prolonged Use

Issue: The DHT22 sensor ceases to provide valid readings after extended periods of operation, requiring a reset to resume functionality.

Possible causes include sensor lockup due to environmental factors or power supply instability.

Solution: Implement periodic sensor resets in your code to mitigate potential lockups. Ensure a stable power supply and consider adding decoupling capacitors to filter out noise. If the problem persists, evaluate the operating environment for factors that may adversely affect the sensor's performance.

Interference with Other I2C Devices

Issue: Connecting the DHT22 sensor alongside other I2C devices causes communication issues or device malfunctions.

Possible causes include the DHT22's communication protocol conflicting with I2C devices.

Solution: The DHT22 uses a proprietary single-wire protocol and does not operate on the I2C bus. Ensure that the sensor is connected to a dedicated GPIO pin and that its communication does not interfere with I2C devices. If issues persist, consider isolating the sensor's data line from the I2C bus.

Where to buy the DHT22 / AM2302

DHT22 / AM2302 Temperature and Humidity Sensor
DHT22 / AM2302 Temperature and Humidity Sensor
$5per unit, typical
Amazon and AliExpress links are affiliate links - buying through them supports the site at no extra cost to you.

Resources