DHT11 Temperature & Humidity Sensor
The classic beginner temperature and humidity sensor — cheap, factory-calibrated and supported on every board.
- Temperature range
- 0 °C to 50 °C
- Temperature accuracy
- ±2 °C
- Humidity accuracy
- ±5 % RH at 25 °C
Wiring & code for
What is the DHT11?
If you have ever followed a beginner tutorial that shows temperature and humidity on a screen, the sensor in the picture was almost certainly a DHT11. It is a small blue plastic box with a perforated grille, four pins, and a price tag of a dollar or two. Inside is everything needed to answer two questions about the air around it: how warm is it, and how humid is it?
What makes it friendly is that all the hard work is done for you. The sensor is calibrated at the factory, measures both values itself, and hands your microcontroller a finished digital reading over one wire. You do not read a voltage, you do not convert a resistance and you do not calibrate anything. You connect three wires, install a library, and call two functions.
It is a good fit for room thermometers, plant and greenhouse monitors, simple weather stations, humidifier or fan controllers and first IoT dashboards. It is a poor fit for anything that needs precision, decimals, freezing temperatures or fast response, and the rest of this page will help you decide which side of that line your project falls on.
Warning
The DHT11 reports whole numbers only, can be off by ±2 °C and ±5 % RH, and is specified for 0 °C to 50 °C and 20–90 % RH. Outside those ranges the readings are not trustworthy. If you need decimals, sub-degree accuracy or outdoor winter use, choose the DHT22 instead.
Specifications
- Sensing elements
- Resistive-type humidity element and NTC thermistor, read by a built-in 8-bit processor
- Operating voltage
- 3.3 V – 5.5 V DC (some datasheet revisions and sellers quote 3.0 V as the minimum)
- Operating current
- 0.3 mA measuring, 60 µA standby (Aosong manual). Some revisions and resellers quote up to 2.5 mA while measuring, so design for that peak
- Temperature range
- 0 °C to 50 °C
- Temperature accuracy
- ±2 °C
- Humidity range
- 20 % to 90 % RH (Aosong). Several resellers quote 20–80 %
- Humidity accuracy
- ±5 % RH at 25 °C
- Resolution
- 8-bit: 1 °C and 1 % RH steps (no decimals)
- Sampling rate
- 1 Hz maximum. Read it no more than once every 2 seconds in practice, which is what the Adafruit library enforces
- Response time
- Slow: several seconds to tens of seconds to follow a sudden change
- Interface
- Single-wire digital (proprietary: not I²C, not Dallas 1-Wire). 40-bit frame with a checksum
- Pull-up resistor
- 4.7 kΩ – 10 kΩ between DATA and VCC (Aosong's own circuit shows about 5.1 kΩ). Built into most 3-pin modules
- Power-up wait
- Wait 1 second after power-up before the first command
- Cable length
- Keep it short, especially at 3.3 V. Aosong guidance allows roughly 20 m at 5 V with a reduced pull-up
- Size
- 15.5 × 12 × 5.5 mm (L × W × H) for the bare sensor
Pinout
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power, 3.3 V – 5.5 V DC. Use 3.3 V on 3.3 V boards such as the ESP32 and Raspberry Pi |
| 2 | DATA | Single-wire digital input/output. Needs a 4.7–10 kΩ pull-up to VCC |
| 3 | NC | Not connected. Leave it floating |
| 4 | GND | Ground |
Swipe sideways to see every column →
On the bare 4-pin sensor, hold it with the blue grille facing you and the pins pointing down: the pins run VCC, DATA, NC, GND from left to right. Most 3-pin modules drop the NC pin and add the pull-up resistor (and often a power LED) on the board, but their pin order varies by maker, so always read the silkscreen instead of assuming. The DATA line swings up to whatever voltage powers the sensor, so on a 3.3 V-only board such as the ESP32, Raspberry Pi or Blue Pill, power the DHT11 from 3.3 V, never from 5 V.
Variants
| Variant | Temp range | Hum range | Accuracy | Protocol | Price |
|---|---|---|---|---|---|
| DHT11 | 0–50°C | 20–90% RH | ±2°C / ±5% RH | Single-wire, 1 Hz | ~$1–2 |
| DHT22 (AM2302) | -40–80°C | 0–100% RH | ±0.5°C / ±2% RH | Single-wire, 0.5 Hz | ~$3–5 |
| AHT10 | -40–85°C | 0–100% RH | ±0.3°C / ±2% RH | I2C | ~$1–3 |
Swipe sideways to see every column →
The DHT11 is the cheapest and simplest of the family, and fine for indoor, room-temperature projects. If you need better accuracy, decimals or freezing temperatures, the DHT22 uses the same wiring and the same library, so you only change DHTTYPE. If you would rather use I²C than a single-wire protocol, the AHT10 is a cheap, accurate alternative.
How the DHT11 works inside
If you carefully prise the case open, you find three parts: a humidity element, a temperature sensor and a small chip that ties them together.
The humidity element is built from two electrodes with a moisture-absorbing material between them. As the surrounding air gets more humid, the material absorbs water vapour and its electrical properties change, which the sensor measures. Aosong's own manual describes the DHT11's element as resistive, while many tutorials call it capacitive, and the DHT22 does use a capacitive polymer element. For you as a user the difference does not matter, because the chip hides it.
The temperature sensor is an NTC thermistor, a resistor whose resistance falls as it gets warmer. Ordinary resistors also change slightly with temperature, but a thermistor is made to change a lot, which makes tiny temperature differences easy to measure.
The chip is the part that makes the sensor easy. It reads both elements, applies the calibration values stored at the factory, converts the result into digital numbers and sends them to your board in a short 40-bit message whenever you ask for a reading. Between requests it sleeps and uses very little power.
DHT11 vs DHT22: which should you use?
The two sensors look alike, share the same pinout, use the same library and send data the same way, so swapping one for the other only needs a one-word code change. The real differences are in accuracy, range and price.
| DHT11 | DHT22 (AM2302) | |
|---|---|---|
| Temperature range | 0 °C to 50 °C | −40 °C to 80 °C |
| Temperature accuracy | ±2 °C | ±0.5 °C |
| Humidity range | 20–90 % RH | 0–100 % RH |
| Humidity accuracy | ±5 % RH | ±2 % RH (max ±5 % RH) |
| Resolution | 1 °C / 1 % RH | 0.1 °C / 0.1 % RH |
| Sampling | 1 Hz max | 0.5 Hz (one reading every 2 s) |
| Typical price | about $1–2 | about $3–5 |
Choose the DHT11 when cost matters more than precision, the sensor lives indoors and a degree or two of error is acceptable. Choose the DHT22 when you want decimals, need to measure below freezing or above 50 °C, or care about the humidity number. If you outgrow both, I²C sensors such as the AHT10, SHT31 or BME280 are more accurate and do not rely on precise bit timing.
Pinout, modules and the pull-up resistor
You will find the DHT11 sold in two forms, and the difference matters for wiring.
The bare sensor has four pins: VCC, DATA, NC and GND. NC means "not connected" and can be ignored. Because the data line is open-drain style (the sensor only pulls it low and relies on a resistor to pull it back high), the bare sensor needs an external pull-up resistor of 4.7 kΩ to 10 kΩ between DATA and VCC. Forgetting it is the single most common cause of "Failed to read from DHT sensor" errors.
The 3-pin module (the one on a small blue or green PCB) has VCC, DATA and GND, and already carries the pull-up resistor, so you can wire it straight to your board. Pin order varies by manufacturer, so read the silkscreen. Some modules label the pins "+", "OUT" and "−", which map to VCC, DATA and GND.
If you are not sure which you have, count the pins: four means add a resistor, three usually means you do not need one.
Powering it: 3.3 V or 5 V?
The DHT11 works from about 3.3 V up to 5.5 V, so both rails are possible, but the right choice depends on your board.
On a 5 V board such as the Arduino Uno or Mega, power the sensor from 5 V. The data line will swing between 0 V and 5 V, which matches the board's logic level, and longer cables behave better.
On a 3.3 V board such as the ESP32, ESP8266, Raspberry Pi or STM32 Blue Pill, power the sensor from 3.3 V. The data line swings to whatever voltage the sensor is powered at, so powering it from 5 V would put 5 V on a GPIO that cannot take it. On a Raspberry Pi in particular, that mistake can permanently damage the board.
One thing to keep in mind at 3.3 V: the manufacturer recommends a short cable, because a long cable drops enough voltage to make readings unreliable. For runs of more than about a metre, 5 V power and a lower-value pull-up work better, but only on boards that can tolerate the resulting signal.
- 1Open the Arduino IDE and go to Tools → Manage Libraries.
- 2Search for "DHT sensor library" and install the one published by Adafruit.
- 3Accept the prompt to also install "Adafruit Unified Sensor", which the library depends on.
- 4Wire the sensor: VCC to 5 V (or 3.3 V on a 3.3 V board), GND to GND, and DATA to a digital pin, with the pull-up resistor in place on a bare sensor.
- 5Open File → Examples → DHT sensor library → DHTtester, set DHTPIN to your pin and DHTTYPE to DHT11, then upload.
- 6Open the Serial Monitor at the baud rate in the sketch and wait about two seconds for the first reading.
Reading the sensor, line by line
The shortest useful sketch is only a few lines, but each line is doing something specific.
#include <DHT.h> loads the Adafruit library, which hides the precise bit timing the sensor needs. #define DHTPIN 2 names the pin that the DATA wire is connected to, and #define DHTTYPE DHT11 tells the library which sensor it is talking to, because the DHT11 and DHT22 format their data differently. DHT dht(DHTPIN, DHTTYPE); creates the sensor object, and dht.begin() in setup() prepares the pin.
In loop(), dht.readHumidity() and dht.readTemperature() each ask the sensor for data and return a floating-point number. If the sensor did not answer correctly or the checksum failed, they return NaN ("not a number"), which is why the sketch checks isnan() before using the values. Skipping that check is how people end up printing "nan" in their dashboards.
The delay(2000) is there on purpose. The sensor needs a little time between readings, and the library will return the previous values if you ask too soon. The sketch below goes one step further and uses millis() so the rest of your program is not frozen while it waits. It also converts to Fahrenheit and calculates the heat index, the "feels like" temperature, using functions that are built into the library.
How the DHT11 talks to your board
You never have to write this yourself, but understanding it explains why the sensor is fussy about timing, wiring and long cables.
The DHT11 uses a single wire for everything. It is not I²C and it is not Dallas 1-Wire, so you cannot put several DHT sensors on one pin; each one needs its own pin.
A reading works like a short conversation. First your board pulls the data line low for at least 18 milliseconds, which wakes the sensor, and then releases it. The sensor answers by pulling the line low for about 80 microseconds and high for about 80 microseconds. Then it sends 40 bits, one at a time. Every bit starts with a low pulse of about 50 microseconds, and the length of the high pulse that follows decides its value: roughly 26–28 microseconds means a 0, and roughly 70 microseconds means a 1. Because those pulses are measured in microseconds, the code has to be precise, which is why the library disables interrupts briefly while it reads.
The 40 bits make up five bytes:
| Byte | Content on the DHT11 |
|---|---|
| 1 | Humidity, integer part |
| 2 | Humidity, decimal part (always 0 on the DHT11) |
| 3 | Temperature, integer part |
| 4 | Temperature, decimal part (always 0 on the DHT11) |
| 5 | Checksum: the sum of the first four bytes |
The library adds up the first four bytes and compares the result with the fifth. If they do not match, the reading is thrown away and you get NaN. That checksum is the only error detection the protocol has, so noise on a long cable shows up as failed reads rather than wrong numbers, which is the safer failure mode.
Getting accurate readings
Even within its ±2 °C and ±5 % RH tolerance, where you put the DHT11 changes what it tells you.
Keep it away from heat. A microcontroller, voltage regulator, relay or power supply a few centimetres away will warm the air around the sensor and push the temperature up and the humidity down. Mount it on a short lead, away from the board, in free-flowing air.
Give it time. The DHT11 reacts slowly. If you carry it from a cold room into a warm one, it can take several seconds to tens of seconds to settle, and humidity takes longer than temperature. Do not judge it from the first few readings.
Protect it from water, not from air. It needs airflow to measure the room, so do not seal it in a closed box, but keep it out of direct sunlight, spray and condensation. Water on the element ruins readings until it dries, and repeated condensation shortens its life.
Expect a small offset. Compare it with a trusted thermometer or hygrometer and, if it is consistently off by the same amount, subtract a fixed correction in your code. Many DHT11s read a degree or two high or low, and that is within specification.
Outside its range, stop trusting it. Below 0 °C, above 50 °C or below 20 % RH, the readings can be wildly wrong or stuck at a limit value.
Troubleshooting
Almost every DHT11 problem comes down to wiring, power or timing. Find your symptom below.
| Symptom | Likely cause | Fix |
|---|---|---|
| "Failed to read from DHT sensor" or NaN every time | Missing pull-up, wrong pin, wrong DHTTYPE, or loose wire | Add a 4.7–10 kΩ resistor between DATA and VCC, check the pin number, and set DHTTYPE to DHT11 |
| Works for a while, then NaN appears now and then | Occasional timing error or noise | Normal. Ignore the failed read and retry on the next cycle |
| Temperature reads several degrees too high | Sensor next to a warm component, or self-heating in an enclosure | Move it away from the board, in open air |
| Humidity is stuck at 20 % or 90 % | The real value is outside the sensor's 20–90 % range | Expect this in very dry or condensing conditions |
| Reads fine on the bench but fails on a long cable | Voltage drop and noise on the data line | Shorten the cable, power at 5 V where the board allows it, and use shielded cable |
| ESP32 fails while Wi-Fi is busy | Timing disturbed by radio activity | Retry on the next loop, update the library, and power from 3.3 V |
| Raspberry Pi gives RuntimeError often | Linux is not real-time, so some reads time out | Catch the exception and retry, as in the example code |
| Readings are identical every time | The sketch is reading too fast and getting the cached value | Wait at least 2 seconds between reads |
Frequently asked questions
Why does my DHT11 only show whole numbers? The DHT11 sends 8-bit values, so it reports temperature in steps of 1 °C and humidity in steps of 1 % RH. The decimal bytes in its data frame are always zero. If you need decimals, the DHT22 gives you 0.1 steps.
Can I read it faster than once every two seconds? The datasheet allows 1 Hz, and the Adafruit library is conservative and refuses to refresh more often than every two seconds. The sensor also reacts slowly to real changes, so faster reads would not give you new information anyway.
Do I need a resistor? On the bare 4-pin sensor, yes: a 4.7–10 kΩ pull-up between DATA and VCC. On most 3-pin modules, no, because it is already on the board.
Can I use it with a 3.3 V board? Yes, and it is the right way to do it. Power the sensor from 3.3 V so the data line never exceeds what the GPIO can take.
Can I use several DHT11 sensors? Yes, but each one needs its own data pin, since the protocol is not an addressable bus.
Is the DHT11 waterproof or outdoor-rated? No. It is specified for 0–50 °C and non-condensing indoor air. For outdoor projects, use a DHT22 or an I²C sensor in a ventilated radiation shield.
How long does a DHT11 last? Indoors and away from condensation, usually for years. Humidity sensors do drift over time, so if readings become unrealistic after a long period, replace it.