DHT22 Temperature & Humidity Sensor

How to wire and program a DHT22 sensor with ESP32

The DHT22 (also sold as the AM2302) is a low-cost digital sensor that measures both temperature and humidity. It is one of the most widely used sensors in beginner IoT projects because it is inexpensive, easy to wire, and only requires a single data pin. The DHT22 outputs a calibrated digital signal, so no analog-to-digital conversion is needed on your end -- the library handles all the communication for you.

This guide uses the ESP32-WROOM-32 DevKit. Pin labels may differ on your board -- check your board's pinout diagram.

🔗Key Specs

ParameterValue
Temperature range$-40$ to $+80\,\text{°C}$
Temperature accuracy$\pm 0.5\,\text{°C}$
Humidity range$0$ to $100\,\%\,\text{RH}$
Humidity accuracy$\pm 2\,\%\,\text{RH}$
Supply voltage$3.3$ to $5.5\,\text{V}$
ProtocolProprietary single-wire (not OneWire)
Sampling intervalOnce every $2\,\text{s}$ (minimum)

Important: The DHT22 uses its own proprietary single-wire protocol. This is not the same as the Dallas OneWire protocol used by the DS18B20. You cannot mix them on the same bus.

Alternatives: For better accuracy or I2C convenience, consider the SHT31 or BME280 (adds pressure). See our sensor comparison guide for a full breakdown.

🔗DHT11 vs. DHT22

If you have seen the DHT11, you may wonder how it compares. Here is a quick summary:

FeatureDHT11DHT22
Temperature range$0$ to $50\,\text{°C}$$-40$ to $+80\,\text{°C}$
Temperature accuracy$\pm 2\,\text{°C}$$\pm 0.5\,\text{°C}$
Humidity range$20$ to $80\,\%\,\text{RH}$$0$ to $100\,\%\,\text{RH}$
Humidity accuracy$\pm 5\,\%\,\text{RH}$$\pm 2\,\%\,\text{RH}$
Sampling rate$1\,\text{Hz}$ (once per second)$0.5\,\text{Hz}$ (once every 2 s)
PriceLowerSlightly higher

The DHT22 is the better choice for most projects due to its wider range and higher accuracy. The code and wiring are identical -- you only need to change the sensor type constant in your sketch.

🔗What You'll Need

ComponentQtyNotesBuy
ESP32 dev board1AliExpress | Amazon.de .co.uk .com
DHT22 sensor1Also sold as AM2302AliExpress | Amazon.de .co.uk .com
10k ohm resistor1Pull-up for the data lineAliExpress | Amazon.de .co.uk .com
Breadboard1AliExpress | Amazon.de .co.uk .com
Jumper wires3AliExpress | Amazon.de .co.uk .com

Links marked Amazon/AliExpress are affiliate links. We may earn a small commission at no extra cost to you.

🔗Wiring

DHT22 PinESP32 PinNotes
VCC (pin 1)3.3VCan also use 5V
Data (pin 2)GPIO 4Any digital GPIO will work
NC (pin 3)--Not connected (leave floating)
GND (pin 4)GND

You need a 10 kΩ pull-up resistor between the Data pin and VCC. This holds the data line high when the sensor is not actively transmitting.

Tip: Many DHT22 breakout boards (the ones mounted on a small PCB with three pins) already include the pull-up resistor. If you are using a bare 4-pin DHT22 component, you must add the resistor yourself.

Pin labels, GPIO numbers, and ADC channels vary between ESP32 boards. Always check your board's pinout diagram.

🔗Required Libraries

Install the following libraries through the Arduino Library Manager:

  1. DHT sensor library by Adafruit -- provides the DHT class for reading the sensor
  2. Adafruit Unified Sensor by Adafruit -- a dependency required by the DHT library

To install:

  1. Go to Sketch > Include Library > Manage Libraries...
  2. Search for DHT sensor library (by Adafruit) and install it
  3. When prompted, also install the Adafruit Unified Sensor dependency (or search for it separately)

🔗Code Example

This sketch reads temperature and humidity from a DHT22 sensor and prints both values to the Serial Monitor.

#include <DHT.h>

#define DHTPIN 4        // GPIO pin connected to the Data line
#define DHTTYPE DHT22   // Change to DHT11 if using that sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    Serial.println("DHT22 Sensor Test");
    dht.begin();
}

void loop() {
    // Wait at least 2 seconds between readings
    delay(2000);

    float humidity = dht.readHumidity();
    float tempC = dht.readTemperature();

    // Check if the readings failed
    if (isnan(humidity) || isnan(tempC)) {
        Serial.println("Error: Failed to read from DHT22 sensor.");
        return;
    }

    // Compute heat index
    float heatIndex = dht.computeHeatIndex(tempC, humidity, false);

    Serial.print("Temperature: ");
    Serial.print(tempC, 1);
    Serial.print(" °C  |  Humidity: ");
    Serial.print(humidity, 1);
    Serial.print(" %  |  Heat Index: ");
    Serial.print(heatIndex, 1);
    Serial.println(" °C");
}

🔗How It Works

When the ESP32 sends a start signal on the data pin, the DHT22 responds with a 40-bit data frame containing:

  • 16 bits for humidity (integer and decimal parts)
  • 16 bits for temperature (integer and decimal parts)
  • 8 bits for a checksum

The Adafruit DHT library handles all of this communication, including the precise timing required. The $2\,\text{s}$ minimum interval between readings is a hardware limitation of the sensor -- if you read it faster, you will get stale values or read errors.

The computeHeatIndex() function is a bonus feature of the library. It calculates the heat index (what the temperature "feels like" when combined with humidity), which is useful for weather station projects.

🔗Troubleshooting

ProblemPossible CauseSolution
Readings return NaNWiring issue or missing pull-up resistorCheck all connections, add 10 kΩ pull-up if missing
Readings return NaNReading too fastEnsure at least 2 s between reads
Humidity stuck at 99-100%Sensor saturated or damaged by moistureLet the sensor dry out; avoid direct water exposure
Temperature off by a few degreesNormal sensor toleranceThe DHT22 is $\pm 0.5\,\text{°C}$; calibrate in code if needed
Library not foundMissing dependencyInstall both Adafruit DHT and Adafruit Unified Sensor
Readings work once then stopTiming conflict on ESP32Try a different GPIO pin; avoid GPIO 0, 2, or 15

🔗Next Steps

  • Build a simple indoor weather station that displays temperature and humidity on an OLED screen
  • Log readings to an SD card or send them over WiFi using MQTT
  • Compare DHT22 readings with a BME280 or SHT31 to see the accuracy difference
  • Add threshold alerts (for example, trigger a fan if humidity exceeds a set level)