Sub topic 2
Meet the DHT11 and DHT22
This chapter assumes you can write and upload a basic sketch with setup() and loop(), use the Serial Monitor, place parts on a breadboard, and work with float and int values. It does not cover wiring or library installation. Those come in the next chapters. Here you learn what the two sensors are, how they differ, and how to pick one for a project.
What is inside the package
A DHT sensor is a small plastic package with a perforated housing. The housing lets air reach two sensing elements and a tiny chip.
- A humidity element is a capacitor or resistor-like layer of moisture-absorbing material. Its electrical properties change as it takes up water vapor from the air.
- A temperature element is an NTC thermistor, a resistor whose resistance drops as temperature rises.
- An 8-bit controller chip measures both elements, applies factory calibration, and turns the result into a digital message.
The last item is why these sensors are easy to use. You do not read an analog voltage and convert it yourself. The sensor does the measuring and conversion, then sends finished numbers over a single data wire. The Arduino only has to ask for them. The timing of that exchange is covered in a later chapter.
DHT11 and DHT22 use the same kind of electronics and the same communication scheme. They differ in the quality of the sensing elements, the resolution of the numbers, and the range they cover. The DHT22 is also sold as the AM2302, which is a DHT22 in a three-pin package, often with the pull-up resistor already attached.
The two sensors side by side
The figures below come from typical manufacturer datasheets. Treat them as approximate. Clones and older batches vary, and datasheets from different vendors do not always agree.
| Property | DHT11 | DHT22 (AM2302) |
|---|---|---|
| Humidity range | 20 to 80 %RH | 0 to 100 %RH |
| Humidity accuracy | about ±5 %RH | about ±2 %RH (up to ±5 %RH at extremes) |
| Humidity resolution | 1 %RH | 0.1 %RH |
| Temperature range | 0 to 50 °C | -40 to 80 °C |
| Temperature accuracy | about ±2 °C | about ±0.5 °C |
| Temperature resolution | 1 °C | 0.1 °C |
| Minimum time between reads | 1 second | 2 seconds |
| Supply voltage | about 3 to 5.5 V | about 3.3 to 6 V |
| Typical cost | lowest | roughly two to four times the DHT11 |
Three rows deserve a closer look.
Range
The DHT11 cannot report below 0 °C or above 50 °C, and its humidity range stops at 20 % and 80 %. Outside those limits you may get a clamped value, a wrong value, or a failed read. A sensor on a shaded outdoor shelf in winter, or in a damp bathroom, will leave the DHT11's range. The DHT22 covers almost any indoor or weather-station use. Its temperature limit of -40 °C is a limit of the sensor chip. Condensation can still disturb the humidity reading at the top of the range.
Resolution and accuracy are different things
Resolution is the smallest step in the reported number. Accuracy is how close the number is to the true value. A DHT22 reports 23.4 °C, in steps of 0.1 °C, but the true temperature could be 22.9 to 23.9 °C. A DHT11 reports whole numbers only. Its library output looks like 23.00, because the library returns a float but the sensor sent no fractional part.
A high-resolution number is not evidence of a high-accuracy sensor. Do not assume a tenth-of-a-degree change means the room actually changed by that amount. The chapter on accuracy and calibration offsets deals with this in detail.
Sampling rate
The DHT11 can be read about once per second, and the DHT22 about once every two seconds. That is slow compared with an analog sensor. The reason is that the sensor needs time to complete a measurement, and the chip is not designed to run continuously. Reading faster than the limit usually returns the previous values or a failed read. That topic has its own chapter.
Why the numbers are what they are
The sensor sends 40 bits per reading: 16 bits of humidity, 16 bits of temperature, and an 8-bit checksum. The checksum lets the receiver check that the first four bytes arrived intact.
The two sensors fill the 16-bit fields differently.
- The DHT11 uses one byte for the integer part and one byte for the decimal part. In practice the decimal byte is zero, which gives the whole-number readings.
- The DHT22 sends each value as a single 16-bit integer in tenths. A raw humidity of 652 means 65.2 %RH. For temperature, the top bit is a sign flag, not part of a two's-complement number. A raw value of
0x8065means -10.1 °C, not a large positive number.
You do not decode this yourself, because the library does it. The detail matters if you ever test with a logic analyzer or write your own driver. It is also the reason you cannot swap one sensor for the other by editing only the library constant in a sketch that does its own decoding. The decoding would be wrong.
Telling them apart physically
The two look alike, so check before you buy or wire one.
- The DHT11 housing is usually blue. The DHT22 is usually white.
- A bare sensor has four pins, and pin 3 is not connected. A module version has three pins and a small circuit board that carries a pull-up resistor.
- The label on the front may say DHT11, DHT22, AM2302, or only a brand name. If a part is unmarked, a test with the other sensor type in the sketch is a quick check. A wrong type gives
NaNor obviously wrong values.
Pin order differs between bare sensors and modules, and between module brands. Never assume. Check the printed labels on the board, or the datasheet.
Choosing the sensor type in code
The Adafruit DHT library supports both sensors with the same class. You pick the type with one constant. This sketch is the same as the example you will build on later, with the type chosen at the top.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22 // change to DHT11 if you have the blue sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
if (isnan(humidity) || isnan(temperatureC)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F(" % Temperature: "));
Serial.print(temperatureC);
Serial.println(F(" C"));
}If you set DHT22 with a DHT11 attached, or the reverse, the sketch still compiles. The library decodes the bits with the wrong rules, so you get failed reads or odd values. Setting the wrong type is a common reason for a sensor that seems broken.
The two-second delay(2000) suits both sensors, because it is longer than the DHT11's one-second limit and exactly the DHT22's. Using the slower interval for either sensor is the safe default.
Where the DHT family falls short
Knowing the limits helps you decide when not to use these sensors.
- Slow response. The sensing elements take seconds to follow a change in air. A quick breath on the sensor shows up gradually. These sensors suit room and weather monitoring, not fast events.
- Drift. Humidity elements shift slowly over months or years. Exposure to condensation, solvents, or very high humidity speeds this up. Expect to recheck against a reference instrument if you rely on the numbers.
- Self-heating and placement. A sensor mounted next to a voltage regulator, a display, or a microcontroller reads warm. Mount it away from heat sources and in moving air.
- One reader per wire. Each sensor needs its own data pin. There is no addressing, so several sensors on one data line do not work.
- Timing-sensitive protocol. The library times pulses in microseconds. On boards with interrupts running heavily, such as one driving long LED strips, reads can fail.
Choosing between them
For most projects the choice is simple, and the diagram below captures it.
In short:
- Use a DHT11 for classroom demos, throwaway prototypes, or indoor readings where whole degrees are fine.
- Use a DHT22 for any project where you will compare values, log data over time, or work outside the DHT11's range.
- Look at I2C sensors when you need better accuracy, faster response, or a shared bus. The last chapter covers that choice.
The price difference is small compared with the time lost debugging a sensor that sits outside its range, so default to the DHT22 when in doubt.
Summary of what to remember
- DHT11 and DHT22 measure with the same method and speak the same protocol, but differ in range, resolution, accuracy, and speed.
- The DHT11 gives whole numbers within 0 to 50 °C and 20 to 80 %RH. The DHT22 gives tenths over a much wider range.
- Resolution is not accuracy. A DHT22 shows 0.1 steps, but it is only accurate to about ±0.5 °C.
- Set
DHTTYPEto match the sensor you hold, and read no faster than the slower limit.
Exercise
A project logs the temperature inside an unheated garden shed all year, where winter nights drop to -8 °C. A teammate suggests a DHT11 because it is cheaper and the sketch already uses it. Which sensor should you pick, and what would you expect to see if you used the DHT11 anyway?
Show answer
Pick the DHT22. Its range is -40 to 80 °C, so it covers -8 °C. The DHT11 only works from 0 to 50 °C, so below 0 °C its readings are outside its specification. You would likely see clamped, wrong, or failed readings (NaN) on cold nights, and the whole-number resolution would hide small changes anyway.
Exercise
Your DHT22 sketch compiles and uploads, but you accidentally left #define DHTTYPE DHT11. Why might this give failed or odd readings, and what is the fix?
Show answer
The DHT11 and DHT22 encode their 40-bit messages differently. The DHT11 sends an integer byte and a decimal byte, while the DHT22 sends a 16-bit value in tenths with a sign bit for temperature. With the wrong type the library decodes the bits with the wrong rules, so values come out wrong or the checksum fails and the read returns NaN. Change the line to #define DHTTYPE DHT22, then re-upload.
Exercise
A DHT22 shows 24.6 °C in the Serial Monitor. A reference thermometer in the same spot shows 24.0 °C. Is the sensor broken?
Show answer
Not necessarily. The DHT22 has about ±0.5 °C accuracy, so a 0.6 °C difference is slightly outside typical specification but is within the range of unit-to-unit variation, self-heating from nearby parts, or the reference's own error. The 0.1 °C resolution does not mean the value is accurate to 0.1 °C. Check placement first, then consider a calibration offset, which a later chapter covers.