DHT sensor library
by Adafruit
Adafruit's DHT sensor library handles the single-wire timing protocol used by the whole DHT family (DHT11, DHT21/AM2301, DHT22/AM2302), so you never have to bit-bang the read pulses yourself. Give it a pin and a sensor type, then call readTemperature() and readHumidity(). On a failed read both return NAN instead of garbage numbers, so always check with isnan() before using a value.
Pick the sensor before you pick the code. The DHT11 is the cheap option: it measures roughly 20–90% humidity at about ±5% RH and 0–50 °C at about ±2 °C, with whole-number resolution. The DHT22 costs a little more and is clearly better: 0–100% humidity at about ±2% RH (±5% worst case), −40 to 80 °C at about ±0.5 °C, with 0.1 resolution. The code is identical for both. Only the DHTTYPE define changes, so you can prototype on a DHT11 and swap in a DHT22 later without rewriting anything.
Two rules cause most of the problems people run into. First, never read faster than the sensor can answer: the DHT22 manages one reading every 2 seconds, the DHT11 one per second, and the library's own examples wait 2 seconds between reads. Second, install the dependency. The library needs Adafruit's "Adafruit Unified Sensor" library, which the Arduino Library Manager offers to install alongside it. If you click "Install without dependencies", you will get fatal error: Adafruit_Sensor.h: No such file or directory.
If you get nan or "Failed to read from DHT sensor" the cause is almost always wiring or power, not code. Check that the data pin matches DHTPIN, that a 4.7 kΩ–10 kΩ pull-up resistor sits between the data pin and VCC (3-pin breakout boards already include one, bare 4-pin sensors do not), that DHTTYPE matches the sensor you actually have, and that the supply is stable. On an ESP32, power the sensor from 3.3 V so the data line stays at safe logic levels, and do not use the input-only pins GPIO34–39 as the data pin, because the sensor needs a bidirectional pin.
A note on timing: the library briefly disables interrupts while reading, so a read can disturb other time-critical code (software serial, IR decoding, tight PWM). In a project with other work to do, read on a timer with millis() instead of delay(), as in the ESP32 example below.
If you need better accuracy than a DHT22, or a faster and more stable digital sensor, see the Adafruit BME280 library, which adds pressure and runs over I2C.
Setup
Installation
Arduino IDE — Library Manager
Sketch → Include Library → Manage Libraries → search DHT sensor library
PlatformIO
lib_deps = adafruit/DHT sensor libraryCompatibility
Supported boards
Code
Examples
Basic temperature & humidity read (DHT11)
Minimal setup for a DHT11 on pin 2. The pattern every board integration on this site is built from.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000); // DHT needs time between reads
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println(F("Failed to read from DHT sensor"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F(" % Temp: "));
Serial.print(tempC);
Serial.println(F(" C"));
}Swapping in a DHT22 for better accuracy
Identical code to the DHT11 example. Only the DHTTYPE define changes.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22 // was DHT11 - everything else stays the same
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println(F("Failed to read from DHT sensor"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F(" % Temp: "));
Serial.print(tempC);
Serial.println(F(" C"));
}ESP32: non-blocking reads that keep the last good value
Reads every 2.5 s with millis() so the rest of your program is never blocked, and ignores failed reads instead of printing NaN. Data pin is GPIO4 (do not use GPIO34-39).
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const unsigned long READ_INTERVAL = 2500; // ms
unsigned long lastRead = 0;
float lastTemp = NAN;
float lastHum = NAN;
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
if (millis() - lastRead >= READ_INTERVAL) {
lastRead = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("DHT read failed - keeping last good value"));
} else {
lastTemp = t;
lastHum = h;
Serial.printf("Temp: %.1f C Humidity: %.1f %%\n", lastTemp, lastHum);
}
}
// ...your other tasks run here without waiting on the sensor
}Retry helper for flaky wiring or long cables
Tries a read up to three times before giving up. Useful when a long cable or a weak supply causes occasional failed reads.
bool readDHT(float &t, float &h, uint8_t tries = 3) {
for (uint8_t i = 0; i < tries; i++) {
h = dht.readHumidity();
t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) return true;
delay(2000); // let the sensor recover before retrying
}
return false;
}
// usage:
// float t, h;
// if (readDHT(t, h)) { /* use t and h */ } else { /* report sensor error */ }Fahrenheit and heat index
Pass true to get Fahrenheit, and combine temperature and humidity into a 'feels like' value with computeHeatIndex().
float tempF = dht.readTemperature(true); // true = Fahrenheit
float humidity = dht.readHumidity();
float heatIndex = dht.computeHeatIndex(tempF, humidity, true); // true = input is Fahrenheit
Serial.print(F("Heat index: "));
Serial.print(heatIndex);
Serial.println(F(" F"));