A practical guide to DHT11 and DHT22 temperature and humidity sensors — how they work, how to wire them, how to read them with Arduino and ESP32, and which one to choose for your project.
Temperature and humidity are two of the most commonly measured environmental variables in electronics projects. Whether you’re building a weather station, monitoring a server room, automating a greenhouse, controlling a home HVAC system, or just learning how sensors work, you’ll encounter these measurements sooner or later.
The DHT11 and DHT22 are the two sensors that beginners and professionals reach for first — and for good reason. They’re inexpensive, need only a single data wire, work directly with microcontrollers like Arduino and ESP32, and have extensive library and community support. If you’ve spent any time in the maker or IoT space, you’ve almost certainly seen one of them.
But they’re not identical, and choosing the wrong one for your application is a common mistake. This guide explains exactly how these sensors work, how to wire and code them, what their real-world limitations are, and — most importantly — when to choose one over the other.
What the DHT11 and DHT22 Actually Are
Both the DHT11 and DHT22 are capacitive humidity sensors combined with a thermistor, packaged together with a small signal-conditioning chip that handles the measurement and outputs a calibrated digital result. The “DHT” stands for Digital Humidity and Temperature.
Unlike analogue sensors that output a varying voltage you need to convert with an ADC, DHT sensors send their data as a serial digital signal over a single wire. The microcontroller sends a start signal, and the sensor responds with a 40-bit data packet — 16 bits of humidity data, 16 bits of temperature data, and an 8-bit checksum to verify the reading was received correctly.
This single-wire protocol (sometimes called a 1-Wire-like protocol, though it’s not the standard Dallas 1-Wire) keeps wiring minimal and makes these sensors easy to use even on microcontrollers with limited GPIO pins.
The key difference between the two variants comes down to the quality of the internal sensing elements and the resolution of the signal conditioning circuitry — which translates directly into the accuracy and range specifications you’ll see on their datasheets.
DHT11 vs DHT22: The Specs That Actually Matter
Here’s a direct comparison of the specifications that affect real-world use:
| Specification | DHT11 | DHT22 (AM2302) |
|---|---|---|
| Temperature range | 0°C to 50°C | −40°C to +80°C |
| Temperature accuracy | ±2°C | ±0.5°C |
| Temperature resolution | 1°C | 0.1°C |
| Humidity range | 20% to 80% RH | 0% to 100% RH |
| Humidity accuracy | ±5% RH | ±2–5% RH |
| Humidity resolution | 1% RH | 0.1% RH |
| Sampling rate | Once per second (1 Hz) | Once per 2 seconds (0.5 Hz) |
| Supply voltage | 3.3V to 5.5V | 3.3V to 6V |
| Current (measuring) | 0.3 mA | 1.5 mA |
| Current (standby) | 60 µA | 50 µA |
| Package | 4-pin through-hole | 4-pin through-hole (or 3-pin module) |
A few things stand out from this comparison. The DHT22’s temperature accuracy of ±0.5°C versus the DHT11’s ±2°C is a fourfold improvement — that’s the difference between knowing it’s 22°C and knowing it’s somewhere between 20°C and 24°C. The DHT22 also measures below zero, which matters enormously if your project is exposed to outdoor winter temperatures. And the DHT22’s humidity range covers 0–100% RH versus the DHT11’s 20–80% RH, making it the only viable choice for applications near saturation (like terrarium monitoring or condensation detection).
The DHT11’s one genuine advantage is sampling rate — it can be read once per second versus the DHT22’s two-second minimum interval. For most applications this is irrelevant, but if you need rapid logging it’s worth knowing.
Understanding the Sensor Internals
How Humidity Is Measured
Both sensors use a capacitive humidity sensing element. This consists of two electrodes separated by a hygroscopic (moisture-absorbing) polymer film. As relative humidity increases, the polymer absorbs water vapour and its dielectric constant changes. This alters the capacitance between the electrodes in a measurable, repeatable way.
The internal IC measures this capacitance, compensates for temperature (since temperature affects both the polymer’s moisture absorption and the capacitance reading), and converts the result to a relative humidity percentage.
Capacitive sensors have a significant advantage over resistive humidity sensors: they’re more accurate, more linear, less prone to contamination, and recover better after saturation. This is why the DHT family uses this approach rather than simpler resistive elements.
How Temperature Is Measured
Temperature is measured using a Negative Temperature Coefficient (NTC) thermistor — a resistor whose resistance decreases as temperature increases. The DHT22 uses a more precise thermistor than the DHT11, which is the primary reason for its better temperature accuracy and wider range.
The internal IC reads the thermistor resistance, applies factory calibration coefficients stored in one-time programmable (OTP) memory, and outputs a calibrated temperature value. This factory calibration is what gives you the published accuracy figures — without it, raw thermistor readings would require individual characterisation for each unit.
The Serial Data Protocol
When your microcontroller wants a reading, it pulls the data line low for at least 18 milliseconds (for DHT11) or 1 millisecond (for DHT22) to signal a start condition. The sensor then takes control of the line and sends a 40-bit response: two bytes of humidity data, two bytes of temperature data, and one checksum byte.
The protocol encodes bits using the duration of high pulses — a short high pulse represents a 0, a longer high pulse represents a 1. Timing is critical: the receiving microcontroller must accurately measure pulse widths in the range of 26–70 microseconds. This is why you should always use a proven library rather than writing your own timing code from scratch. Even small sources of latency in your main loop can cause bit-read errors.
The checksum byte is simply the sum of the four data bytes (lower 8 bits). Always check it — corrupted readings that pass through unchecked can silently introduce bad data into your system.
Wiring the DHT11 and DHT22
Both sensors use identical wiring. The bare 4-pin package (which is what you get with the standalone sensor) has the following pinout, reading from left to right with the ventilated face towards you:
- Pin 1 — VCC: Power supply, 3.3V to 5V
- Pin 2 — Data: Serial data signal
- Pin 3 — NC: Not connected (leave floating)
- Pin 4 — GND: Ground
The data line requires a pull-up resistor between the data pin and VCC. Without it, the line floats when neither the sensor nor the microcontroller is driving it, causing unreliable readings or no readings at all. The recommended value is 4.7kΩ to 10kΩ — 10kΩ works reliably in most setups.
If you’re using a breakout module (the small PCB version rather than the bare sensor), the pull-up resistor is already included on the board, so you can connect directly to your microcontroller’s GPIO without any additional components.
Connecting to an Arduino Uno
- VCC → 5V
- GND → GND
- Data → Digital pin 2 (or any digital GPIO)
- 4.7kΩ–10kΩ resistor between Data and 5V (if using bare sensor)
Connecting to an ESP32
- VCC → 3.3V
- GND → GND
- Data → Any GPIO pin (e.g. GPIO4)
- 4.7kΩ–10kΩ pull-up resistor between Data and 3.3V (if using bare sensor)
One important note for ESP32 users: the ESP32 is a 3.3V device, and its GPIO pins are not 5V tolerant on most variants. Always power the DHT sensor from 3.3V when using an ESP32, not 5V. The sensor works correctly at 3.3V — you don’t need level shifting.
Keep the wiring short. DHT sensors are sensitive to long cable runs. For cable lengths beyond about 20cm, increase the pull-up resistor to 10kΩ. For runs beyond a metre, you’ll likely see intermittent errors and may need to add a 100nF decoupling capacitor between VCC and GND close to the sensor.
Reading DHT Sensors with Arduino
The easiest way to use DHT sensors with Arduino is the DHT sensor library by Adafruit, available in the Arduino Library Manager. Install it along with the Adafruit Unified Sensor library it depends on.
#include <DHT.h>
#define DHTPIN 2 // Data pin connected to digital pin 2
#define DHTTYPE DHT22 // Use DHT11 for the DHT11, or DHT22 for the DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// DHT22 needs 2 seconds between readings; DHT11 needs 1 second
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Celsius by default
// float tempF = dht.readTemperature(true); // Pass true for Fahrenheit
// Check for failed readings
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor. Check wiring.");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% | Temperature: ");
Serial.print(temperature);
Serial.println("°C");
}
A few things to note about this code. The isnan() check is essential — the library returns NaN (not a number) on a failed read rather than crashing, but NaN will silently break any arithmetic you do with the value if you don’t catch it. Always handle it.
The delay(2000) is the minimum safe interval for the DHT22. Read it faster and you’ll get stale data (the previous reading) or errors. For the DHT11 you can use delay(1000), but in practice there’s rarely a good reason to read faster than every two seconds for environmental monitoring.
If you’re building something that needs a non-blocking loop (using millis() instead of delay()), the library also has a readHumidity(true) and readTemperature(false, true) variant that forces a new read rather than returning a cached value — useful when you need precise timing control.
Reading DHT Sensors with ESP32
The same Adafruit DHT library works with ESP32 in the Arduino IDE. The setup is almost identical — just change the pin number to whichever GPIO you’ve used and ensure you’re supplying 3.3V to VCC.
#include <DHT.h>
#define DHTPIN 4 // GPIO4 on the ESP32
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Sensor read failed.");
return;
}
Serial.printf("Humidity: %.1f%% Temperature: %.1f°C\n", humidity, temperature);
}
For ESP32 projects that send sensor data to the cloud — MQTT brokers, HTTP APIs, or services like ThingSpeak — the DHT22 is the far more useful choice. Its 0.1°C resolution gives you meaningful data for trend analysis, and its wider temperature range means it works even if the device is deployed outdoors or in an unheated space.
If you’re building a battery-powered ESP32 sensor node and using deep sleep, remember to call dht.begin() after waking up, and give the sensor at least 1 second after power-on before attempting a read. DHT sensors have a brief initialisation period on startup — reading too early will return an error.
Common Problems and How to Fix Them
“Failed to read from DHT sensor” errors
This is the most common issue and usually has one of five causes. First, check the pull-up resistor — this is by far the most frequent culprit when using the bare sensor rather than a breakout module. Without it, readings will fail entirely or be very intermittent. Second, verify the wiring polarity — DHT sensors connected backwards don’t immediately burn out but produce no output. Third, ensure the delay between reads is at least 2 seconds for DHT22 or 1 second for DHT11. Fourth, confirm you’ve specified the correct sensor type in the library call (DHT11 vs DHT22) — the timing differs and they’re not interchangeable in code. Fifth, check that your power supply is stable — DHT sensors draw a brief current spike during measurement that can cause issues on weak power rails.
Readings seem stuck or always return the same value
This usually means the sensor is returning cached data. Ensure you’re not calling readTemperature() and readHumidity() too rapidly. In the Adafruit library, readings are cached for ~2 seconds by default to protect the sensor — calling it faster just returns the last result.
Humidity reads 99% or temperature reads nonsensically high
This often indicates condensation on the sensing element. DHT sensors can be temporarily saturated by sudden exposure to high humidity or condensation. Leave the sensor at room temperature in dry air for 30–60 minutes — it will typically recover. If it doesn’t recover, the sensor may be permanently damaged. This is one reason to avoid mounting DHT sensors in positions where condensation can form directly on them.
Intermittent errors on ESP32 but not Arduino
The ESP32 runs at higher clock speeds and the Wi-Fi radio can cause brief interruptions that disrupt the timing-sensitive DHT protocol. Use a GPIO pin away from the antenna, and if you’re still seeing errors, try disabling Wi-Fi briefly during the read, or switch to a library specifically optimised for ESP32 such as DHT_nonblocking or use the Adafruit_DHT fork with ESP32 timing fixes.
Practical Project Ideas
These sensors open the door to a huge range of projects, from simple to sophisticated.
Personal weather station. Combine a DHT22 with a barometric pressure sensor (like the BMP280) and an OLED display to build a compact indoor weather station that shows temperature, humidity, and pressure. Add an ESP32 and you can log the data to a cloud service or serve it as a web page on your local network.
Greenhouse or grow tent monitor. Plants have specific temperature and humidity requirements. A DHT22 connected to a microcontroller can trigger fans, heaters, or humidifiers when readings go outside a defined range. This is a classic home automation project with real practical value.
Server rack or electronics enclosure monitor. Overheating and condensation are two major causes of electronics failure. A DHT22 mounted inside an enclosure with an ESP32 can send alerts if temperature exceeds a threshold or if humidity rises to a level where condensation becomes a risk.
Comfort index display. Relative humidity significantly affects how warm a given temperature feels. You can calculate the heat index (apparent temperature) from DHT readings and display it alongside the raw values — a genuinely useful addition to a room display.
Baby room or nursery monitor. Parents often want to know their baby’s room is within the recommended temperature and humidity range. A DHT22 with an ESP32 pushing data to a phone app is a practical and achievable beginner-to-intermediate project.
Limitations to Know Before You Deploy
DHT sensors are excellent for their price point, but there are a few real-world limitations worth understanding before you build something you depend on.
They drift over time. Capacitive humidity sensors are known to drift, typically reading a few percent high after extended exposure to high humidity environments. In a dry indoor setting, drift is negligible over years. In a high-humidity environment (bathroom, greenhouse, outdoor), you may see 1–3% drift annually. For critical applications, periodic recalibration against a reference sensor is good practice.
They respond slowly. DHT sensors have a relatively slow response time — on the order of several seconds. They’re designed for ambient environmental monitoring, not for detecting rapid changes. If you need fast-response temperature sensing (for example, measuring the temperature of a liquid or a rapidly changing process), a thermocouple or RTD is a better choice.
They’re not designed for condensing environments. Both sensors are rated for non-condensing conditions. Persistent exposure to condensation will damage the capacitive element and produce permanently erratic readings.
The DHT11’s accuracy is genuinely limiting. ±2°C sounds like a small error until you consider that the comfort range for most people is about 4°C wide. A DHT11 reading of 22°C means the actual temperature could be anywhere from 20°C to 24°C. For anything where temperature accuracy actually matters, the DHT22 is the right choice.
DHT11 or DHT22 — Making the Decision
Here’s a simple framework for choosing between them.
Choose the DHT11 if you are learning and just want to get a sensor working, your project only needs a rough indication of temperature and humidity, the environment will always be between 0°C and 50°C, and you need to poll the sensor as fast as once per second.
Choose the DHT22 for almost everything else. Its wider temperature range, significantly better accuracy, and 0.1° resolution make it suitable for real environmental monitoring rather than just approximate readings. The two-second minimum read interval is irrelevant in the vast majority of applications, and the slightly higher price is worth it for any project you actually care about.
If you’re unsure, default to the DHT22. The improved accuracy becomes apparent the first time you compare its readings against a reference thermometer and find they match within half a degree.
Final Thoughts
The DHT11 and DHT22 have earned their place as the go-to temperature and humidity sensors for the maker and IoT world. They are genuinely easy to use, well-supported by libraries across every major platform, and inexpensive enough to include in projects where cost is a constraint. The single-wire protocol means a minimum of wiring, and the calibrated digital output means you get a usable reading without any analogue signal processing.
The DHT11 is a fine learning tool — if you’ve never worked with environmental sensors before, it’s a low-stakes way to get your first readings on screen. But the DHT22 is the sensor you’ll actually trust in a real project. Its accuracy, range, and resolution are meaningfully better in every dimension that matters, and once you’ve built something that depends on reliable environmental data, you’ll understand exactly why the upgrade is worth it.
Both sensors are a single data wire away from making your projects environmentally aware. Once you have one running, you’ll find yourself wanting to add temperature and humidity sensing to almost everything you build.
Pick up a DHT11 to get started, or go straight to the DHT22 if you want your readings to actually mean something.
