All articles

    Temperature and Humidity Sensors: the DHT11, the DHT22 and Their Alternatives

    Comparing the DHT11 and DHT22: range, accuracy and reading speed, wiring with a single data wire and a pull-up resistor, how the protocol works, Arduino code without delay, heat index and dew point, calibrating with salt, where to place the sensor, common errors (NaN and reading too fast), and more accurate alternatives such as the SHT31 and BME280.

    Updated 5 min read

    A small weather station, controlling a fan by temperature, or monitoring a storeroom's humidity: they all usually start with a DHT sensor. It's cheap and easy, and reads both temperature and humidity over a single wire.

    The sensor sends temperature and humidity over a single data wire
    The sensor sends temperature and humidity over a single data wire

    DHT11 or DHT22?

    DHT11 (blue)DHT22 / AM2302 (white)
    Temperature0 to 50 °C, ±2−40 to 80 °C, about ±0.5
    Humidity20 to 80%, ±5%0 to 100%, ±2–5%
    Minimum time between readings1 second2 seconds
    PriceCheaperA bit more expensive

    If you're measuring a room's air roughly, the DHT11 is enough. If you want a number you can trust, or outdoors, go for the DHT22. The DHT11 also gives whole numbers only (25, not 25.4), so you won't see small changes.

    Wiring

    Data on any digital pin, and a 10k resistor from data to the supply
    Data on any digital pin, and a 10k resistor from data to the supply
    • VCC: 3.3 to 5 V.
    • DATA: on any digital pin, with a 10k pull-up resistor to VCC. Most modules on a small board have this resistor fitted already. See the pull-up article.
    • GND.

    The bare sensor (4 pins): facing you with the vents towards you, from the left: VCC, DATA, an unconnected pin, and GND. The module (3 pins): the order varies from one seller to another, so read what's printed on the board.

    A 100 nF capacitor between VCC and GND close to the sensor is recommended, especially if the wire is long.

    How does the protocol work?

    It's not I2C or UART; it's a dedicated single-wire protocol:

    1. The microcontroller pulls the line LOW for a short time (about 1–18 ms) as a "send" signal.
    2. The sensor replies and sends 40 bits: 16 for humidity, 16 for temperature, and 8 for checking (a checksum).
    3. Each bit is identified by the length of its pulse: a short pulse = 0, a long one = 1.

    That's why the timing is very sensitive, and the library disables interrupts for a moment while reading. If the checksum doesn't match, the library returns NaN instead of a wrong number, which is good.

    The code (the Adafruit DHT library)

    #include <DHT.h>
    DHT dht(2, DHT22);   // or DHT11
    
    void setup() { Serial.begin(9600); dht.begin(); }
    
    void loop() {
      delay(2000);
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      if (isnan(h) || isnan(t)) { Serial.println("read failed"); return; }
      Serial.print(t); Serial.print(" C, "); Serial.print(h); Serial.println(" %");
    }

    Without delay

    In a project with a display and buttons, you don't want the program stopping for two seconds. Read at intervals with millis:

    unsigned long tRead = 0;
    float t = NAN, h = NAN;
    
    void loop() {
      if (millis() - tRead >= 2500) {
        tRead = millis();
        float nt = dht.readTemperature();
        float nh = dht.readHumidity();
        if (!isnan(nt) && !isnan(nh)) { t = nt; h = nh; }  // keep the last good reading
      }
      // the rest of the program keeps running: buttons, display...
    }

    Heat index and dew point

    • Heat index: the temperature "you feel" taking humidity into account. The library calculates it: dht.computeHeatIndex(t, h, false). 35 °C at 60% humidity feels like about 45.
    • Dew point: the temperature at which, if the air cooled to it, water would condense. A simple approximation when humidity is above 50%: dew point ≈ temperature − (100 − humidity) ÷ 5. Example: 30 °C and 70% → about 24 °C. Useful if you're monitoring a storeroom or an electronics enclosure for condensation.

    Calibration: the salt test

    Humidity is harder to get accurate than temperature. A simple test: put a spoonful of table salt with a few drops of water (a damp paste, not water) in a sealed container with the sensor, and leave them 8–12 hours at a steady temperature. The air in the container settles at about 75%. If the sensor reads 71%, add 4 in the code.

    Where to put the sensor

    • Out of direct sunlight: the sun heats the sensor itself, so it reads much higher than the air.
    • With ventilation: inside a sealed box it measures the box's air, not the room's.
    • Outdoors: put it in shade, inside a cover open at the bottom to protect it from rain.
    • Away from electronics that get warm (the regulator and the ESP32), and if it's on the same board, place it at the edge.

    Common problems

    • The reading is NaN: wrong wiring, no pull-up, the wrong sensor type in the code (DHT11 instead of DHT22), or a very long wire.
    • Reading too fast: if you read faster than allowed, you'll get the same old value or an error.
    • The temperature is higher than reality: the sensor is near something that heats up, such as the regulator or the ESP32 itself. Move it away on a short wire, or put it outside the enclosure.
    • Very high humidity (fog or water) can degrade its accuracy over time.
    • A long cable: a few metres work fine. For longer, power it from 5 V and use twisted cable, or choose an I2C sensor in a module close to the microcontroller.

    Better alternatives

    SensorAdvantages
    SHT31 / SHT40Very high accuracy, I2C
    BME280Temperature, humidity and air pressure, I2C
    DS18B20Temperature only, with good accuracy, and a sealed waterproof version

    Watch out: some modules labelled BME280 are actually a BMP280 (no humidity). The BMP280 returns zero humidity or isn't recognised as a BME. And for measuring water temperature or several points, see the DS18B20.

    A project idea

    An ESP32 with a DHT22 and an OLED display showing the reading, plus a web page over Wi-Fi so you can see the reading from your phone. See getting started with the ESP32 and a web page from the ESP32. And if it's on a battery, have it read every 10 minutes and sleep in between: ESP32 deep sleep.

    FAQ

    Two DHTs side by side read different numbers? That's normal within the accuracy (±2 °C for the DHT11). Give them the salt test, or compare them with a trusted thermometer, and correct each one.

    Can I connect several DHTs on the same pin? No, each sensor needs its own pin. If you need several points on one wire, the DS18B20 does that.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.