DHT11 guides · On this page

Read Temperature and Humidity with a DHT11 on an Arduino

Read a room's temperature and humidity with a DHT11 on an Arduino, and light the Uno's L LED when the air gets too humid, running in your browser before you build it. The DHT11 is the cheap, common sensor for this. It is easy to wire and read, but coarse, at ±2 °C and ±5 %, with a new reading at most every two seconds.

Left to right: a bare light blue DHT11 with four legs, a DHT11 on a small black board with three header pins, and a larger white DHT22 with four legs.
DHT114 pinsDHT11 module3 pins, often KY-015DHT22White and larger

Try it first, no parts needed

This is the whole project, wired and running in your browser. Press Run simulation, then change the air around the sensor, or hold the button to breathe on it, and watch the Uno print each reading. Push the humidity past 60 % and the Uno's small L LED, just left of the Arduino logo, lights up to say the air is too humid.

Ready
Open in Cirkit Designer

The air around the DHT11

The sketch reads it every 2 seconds.

25 °C
60 %

Arduino Uno serial monitor115200 baud

Output appears here when the simulation runs.

What the code does

The sketch asks the DHT11 for a reading every two seconds, prints it, and lights the Uno's L LED when the air is humid.

dht11_reader.ino
#include <DHT.h>

DHT dht(2, DHT11);           // DATA on pin 2
const int HUMID_ABOVE = 60;  // % RH

void setup() {
  Serial.begin(115200);
  dht.begin();
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  delay(2000);  // the DHT11 answers at most every 2 s
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Read failed: check the wiring");
    return;
  }
  Serial.print(temperature, 1);
  Serial.print(" C  ");
  Serial.print(humidity, 1);
  Serial.println(" %");
  // The Uno's L LED lights when the air is too humid.
  digitalWrite(LED_BUILTIN, humidity > HUMID_ABOVE);
}
  1. DHT dht(2, DHT11);           // DATA on pin 2

    Names the pin and the sensor type. The type must be DHT11; with DHT22 every read fails (see troubleshooting).

  2. if (isnan(humidity) || isnan(temperature)) {
      Serial.println("Read failed: check the wiring");
      return;
    }

    Both reads return NaN when a reading fails, so the sketch checks them with isnan() before printing anything.

  3. digitalWrite(LED_BUILTIN, humidity > HUMID_ABOVE);

    Lights the Uno's built-in L LED when the humidity is above HUMID_ABOVE, so you can see the result without a screen.

Want Fahrenheit? Use dht.readTemperature(true); see the FAQ.

Build it on an Arduino

Now with real parts. Check which pin is which, wire it, get the code, then upload it and watch the readings. Using an ESP32 board? It has its own guide →

Which pin is which: the DHT11 pinout

With the grille facing you, the pins are, left to right: VDD, DATA, NC and GND. A three-pin module prints its own labels; go by those.

DHT11 pinout with the vented face toward you, left to right: 1 VDD supply, 3.3 to 5.5 V; 2 DATA serial data, pulled up to VDD; 3 NC not connected; 4 GND ground.
MeasuresTemperature and relative humidity
Supply3.3 to 5.5 V (older DHT11s: from 3 or 3.5 V)
Temperature−20 to 60 °C, ±2 °C (older DHT11s: 0 to 50 °C)
Humidity5 to 95 % RH, typically ±5 % (older DHT11s: 20 to 90 %)
ReadingsAt most every 2 s; in tenths, or whole numbers on older DHT11s
InterfaceOne data wire, with a pull-up resistor

You'll need

  • An Arduino Uno (a Nano or Mega works the same way) and its USB cable
  • A DHT11: a bare four-pin sensor, or a three-pin module such as the KY-015
  • A 4.7 kΩ resistor, for a bare sensor only; modules have one built in
  • A breadboard and five jumper wires
  • Software to upload the sketch to the Uno: either Cirkit Designer, free in your browser, or the Arduino IDE, a free download for Windows, macOS and Linux
  1. Wire it

    Four connections and one resistor.

    Tap a connection to see it on the circuit.

    DHT11 pinConnect to the UnoShow
    1 VDD5VShow
    2 DATAD2Show
    Pull-up4.7 kΩ resistor from DATA to 5VShow
    3 NCLeave unconnectedShow
    4 GNDGNDShow

    Have a three-pin module, such as the KY-015? Skip the resistor; the module has one built in. Connect + or VCC to 5V, S or OUT to D2, and − or GND to GND. Pin order varies between makers, so go by the labels printed on yours.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    The project opens with the sketch and the DHT sensor library already in it, so there's nothing to install.

    No Arduino IDE yet? Download it free for Windows, macOS or Linux.

    First, open the Library Manager (1), search for DHT sensor library (2), and install the one by Adafruit (3); when it asks about dependencies, install them all.

    Arduino UnoLIBRARY MANAGERDHT sensor libraryType: AllTopic: AllDHT sensor libraryby AdafruitArduino library for DHT11, DHT22,etc Temp & Humidity SensorsMore infoINSTALL123

    Then copy the sketch into a new one:

    How it works line by line is in what the code does, above.

  3. Upload and check it

    Plug in your Uno. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your Uno, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor.

    Show me where to click in Cirkit
    DHT11 on an Arduino UnoFileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    Arduino UnoEdit Code

    Board

    Arduino Uno Change

    2

    Port

    Select PortCOM3 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    Sketch uses 5642 bytes (17%) of program storage space. Maximum is 32256 bytes.
    Global variables use 279 bytes (13%) of dynamic memory, leaving 1769 bytes for local variables. Maximum is 2048 bytes.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM3 at 115200 baud
    25.0 C 60.0 %
    25.0 C 61.0 %
    26.0 C 66.0 %
    27.0 C 74.0 %
    27.0 C 80.0 %
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM3 (Arduino Uno)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM3 (Arduino Uno)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. Click Start Serial Monitor

    In the Arduino IDE, choose Arduino Uno and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud.

    Show me where to click in the Arduino IDE
    Select BoardArduino Uno

    Select other board and port

    Arduino UnoCOM3
    dht11_reader.ino
    #include <DHT.h>
    
    DHT dht(2, DHT11);           // DATA on pin 2
    const int HUMID_ABOVE = 60;  // % RH
    
    void setup() {
    OutputSerial Monitor115200 baud

    Compiling sketch...

    Sketch uses 5642 bytes (17%) of program storage space. Maximum is 32256 bytes.

    Global variables use 279 bytes (13%) of dynamic memory, leaving 1769 bytes for local variables. Maximum is 2048 bytes.

    Uploading...

    25.0 C 60.0 %

    25.0 C 61.0 %

    26.0 C 66.0 %

    27.0 C 74.0 %

    27.0 C 80.0 %

    Done uploading.

    1. Click the board menu
    2. Pick Arduino Uno on COM3
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. Check it is set to 115200 baud

    It works when:

    • A new line appears every two seconds, like 25.0 C 60.0 %.
    • You breathe on the sensor and the humidity rises within a few seconds.
    • Above 60 %, the Uno's L light comes on.

    Not working? If you see Read failed, see troubleshooting.

    Next guideBuild a room monitor with an OLED displayTemperature, humidity and a comfort label on a 128 × 64 OLED: the same circuit, plus four wires.

Troubleshooting

Find your symptom; the checks under it run from the most common cause down.

"Read failed", NaN, or "Failed to read from DHT sensor!"

The board asked and got no valid answer. Check, in order:

  • The code says DHT11, not DHT22. With DHT22 the library's start signal is too short for a DHT11, and every read fails. Adafruit's DHTtester example is set to DHT22; change it.
  • A bare sensor has its pull-up. 4.7 to 10 kΩ from DATA to VDD. A three-pin module has one built in.
  • DATA is on the pin the code uses. DHT dht(2, DHT11) means pin 2.
  • A module is wired by its printed labels. Three-pin modules order their pins differently between makers.
  • VDD and GND aren't swapped. Check pin 1 against the pinout.
  • The first reading comes at least 2 s after power-up. Aosong's manual gives the sensor 2 s to start; the sketch's first delay(2000) covers it.

Nothing in the Serial Monitor, or garbled characters

  • The Serial Monitor is set to 115200 baud. It must match Serial.begin(115200) in the sketch.

"Adafruit_Sensor.h: No such file or directory"

Arduino IDE only
  • Adafruit Unified Sensor is installed. The DHT library needs it. Install it from the Library Manager, or reinstall the DHT library and choose Install all when it asks about dependencies. Cirkit Designer includes both.

The same reading over and over

  • The sketch waits at least 2 s between readings. Ask more often and the library returns its last reading again.
  • Expect each change one reading late. Each reading is the one the sensor took at your previous request. Breathing on it should move the humidity within a few readings.

How a reading works

Behind the vents are a humidity element, a temperature sensor and a small microcontroller. Aosong's older datasheet names them a resistive humidity element and an NTC thermistor, as drawn below; its 2022 manual describes a single combined element. Either way the microcontroller does the measuring and the calibration, so your board only has to ask for the result and decode it.

VDDDATANCGNDHumidity elementTakes up moisture from the air,which changes its resistance.MicrocontrollerMeasures both, applies the factory calibration,and sends the result as 40 bits on DATA.NTC thermistorIts resistance falls as it warms.

To ask, your board holds DATA low for about 20 ms. The DHT11 answers with 40 bits on the same wire: a short high is a 0 and a long high is a 1. The five bytes are humidity, its tenths, temperature, its tenths (whose top bit marks below zero), and a checksum. Older DHT11s send both tenths as zero. If the checksum doesn't match, the library throws the reading away.

An example reading: 60 % and 25 °CRun the circuit at the top, and this shows your own readings as they happen.

    Exact timing, from Aosong's English datasheet
    One DHT11 reading on the DATA line: the board pulls DATA low for at least 18 ms, the DHT11 answers 80 microseconds low and 80 high, then sends 40 bits, each 50 microseconds low followed by 26 to 28 microseconds high for 0 or 70 high for 1, and ends with 50 microseconds low. The bits are five bytes: humidity 60, 0, temperature 25, 0 and checksum 85.

    Aosong's 2022 manual gives slightly different figures: a start signal of 18 to 30 ms, a reply of 83 µs low and 87 µs high, and bits of 54 µs low followed by 23 to 27 µs high for a 0 or 68 to 74 µs for a 1. The library reads both.

    Accuracy and limits

    DHT11
    Temperature±2 °C. Aosong's current DHT11: −20 to 60 °C, in tenths. Older DHT11s: 0 to 50 °C, in whole degrees.
    HumidityTypically ±5 percentage points: a true 50 % can read 45 to 55 %. Aosong's current DHT11 reads 5 to 95 % in tenths; it is specified within its normal 8 to 85 %, tested at 25 °C and 5 V without condensation. Older DHT11s: 20 to 90 %, ±5 % at 25 °C, in whole percent.
    Reading rateAt most every 2 s, which is also how often the library reads
    ResponseTypically 6 s for a humidity reading to cover 63 % of a change, and 5 to 30 s for temperature, faster with air moving
    FreshnessEach reading is the one the sensor took at your previous request

    A one-step change is smaller than the error, so set thresholds where a few percent either way doesn't matter. Mount the sensor away from parts that heat up, with air moving past its vents. To tell the versions apart, look at the back: Aosong's current DHT11 is laser-marked ASAIR, with its −20 to 60 °C range.

    DHT11 or something better?

    InterfaceTemperatureHumidityChoose it for
    DHT11One wire−20 to 60 °C, ±2 °C (older: 0 to 50 °C)5 to 95 %, typically ±5 % (older: 20 to 90 %)A cheap room reading
    DHT22One wire−40 to 80 °C, ±0.5 °C0 to 100 %, ±2 %The same wiring and code, much better readings
    AHT20I²C−40 to 85 °C, ±0.3 °C0 to 100 %, ±2 %Accuracy on a small, cheap I²C board
    BME280I²C or SPI−40 to 85 °C, ±0.5 °C0 to 100 %, ±3 %Air pressure as well, for weather stations

    DHT11 vs DHT22 compares the two DHT sensors in full.

    DHT11 FAQ

    Why does my DHT11 return NaN or "Failed to read from DHT sensor!"?

    Almost always the wiring or the type: check that the code says DHT11, that DATA goes to the pin the code names, and that a bare sensor has its pull-up resistor. Then check you aren't reading within 2 s of power-up. See troubleshooting.

    Does the DHT11 work at 3.3 V?

    Yes, if it's rated for 3.3 V. Aosong's current manual gives 3.3 to 5.5 V and allows wires up to 1 m at 3.3 V. Its older datasheets start at 3 V (Chinese) or 3.5 V (English), and the English one allows only 20 cm at 3.5 V, because the voltage drop along the wires starves the sensor. The ESP32 guide covers 3.3 V boards.

    Do I need the resistor?

    On a bare four-pin sensor, yes: 4.7 to 10 kΩ from DATA to VDD. Three-pin modules usually have one fitted already.

    How accurate is it?

    ±2 °C and ±5 % humidity. Good for "is the room too humid", not for small changes. See accuracy and limits.

    Can it measure below freezing?

    Aosong's current DHT11 reads down to −20 °C; older DHT11s stop at 0 °C. Adafruit's library (1.4.6) decodes a below-zero reading as −1 minus the whole degrees plus the tenths, which doesn't match Aosong's 2022 manual, so check readings below 0 °C against a thermometer. Check the back for the −20 to 60 °C marking. For dependable readings below freezing, a DHT22, AHT20 or BME280 is the safer choice; see the comparison.

    How often can I read it?

    At most every 2 s, by Aosong's current manual. Adafruit's library reads at most every 2 s too, and returns its last result if you ask sooner.

    How do I get Fahrenheit?

    Call dht.readTemperature(true). The library also computes the heat index, how warm the air feels at its humidity, in either unit:

    Add to loop(), after the two readings
    float fahrenheit = dht.readTemperature(true);
    // Heat index: how warm it feels at this humidity.
    float heatIndexF = dht.computeHeatIndex(fahrenheit, humidity);
    float heatIndexC = dht.computeHeatIndex(temperature, humidity, false);

    Simulating the DHT11 in Cirkit Designer

    Cirkit's DHT11 answers the library through the board's real pin, with the timing in Aosong's older English datasheet, so the same code runs on hardware. It is tested on the Arduino Uno, the ESP32 DevKitC and the ESP32-S3. Like an older DHT11, it reads whole numbers from 0 to 50 °C; its humidity goes from 20 to 80 %, short of an older DHT11's 90 %. It adds no measurement error or response lag, and it reports the current values at each request rather than the previous measurement. It gives no reading for its first second after power-up, where Aosong's 2022 manual allows 2 s. On a real sensor, compare a few readings with a reference and give each change time to settle.

    References