Arduino DHT11 Temperature & Humidity Sensor — Complete Tutorial
The DHT11 is one of the most popular sensors for Arduino beginners. It reads temperature (0–50°C) and humidity (20–80% RH) with a single digital pin. This guide covers wiring, code, library setup, and common errors.
Time to complete: 10 minutes
Skill level: Beginner
DHT11 Specifications
| Parameter | Value |
|---|---|
| Temperature range | 0°C to 50°C |
| Temperature accuracy | ±2°C |
| Humidity range | 20% to 80% RH |
| Humidity accuracy | ±5% RH |
| Sampling rate | 1 reading per second (max) |
| Operating voltage | 3.3V – 5V |
| Current | 2.5 mA (during measurement) |
| Interface | Single-wire digital (1-Wire) |
| Package | 4-pin or 3-pin module |
DHT11 vs DHT22
| Feature | DHT11 | DHT22 |
|---|---|---|
| Temperature range | 0–50°C | -40–80°C |
| Temperature accuracy | ±2°C | ±0.5°C |
| Humidity range | 20–80% | 0–100% |
| Humidity accuracy | ±5% | ±2–5% |
| Sampling rate | 1 Hz | 0.5 Hz |
| Price | ~$1 | ~$3 |
Choose DHT22 when you need outdoor use, wider temperature range, or better accuracy.
Choose DHT11 for indoor projects, learning, and budget builds.
DHT11 Pinout
The DHT11 comes in two versions:
4-pin bare IC:
| Pin | Name | Connect to |
|---|---|---|
| 1 | VCC | 5V |
| 2 | DATA | Digital pin (with 10kΩ pull-up) |
| 3 | NC | Not connected |
| 4 | GND | GND |
3-pin module (blue/white board):
| Pin | Name | Connect to |
|---|---|---|
| 1 (left) | VCC/S | 5V |
| 2 (middle) | DATA | Digital pin |
| 3 (right) | GND | GND |
The 3-pin module already has the pull-up resistor built in. No extra resistor needed.
Wiring — Arduino Uno/Nano
3-pin module (easiest):
| DHT11 Module | Arduino |
|---|---|
| VCC (+) | 5V |
| DATA (out) | Digital pin 2 |
| GND (-) | GND |
4-pin bare sensor:
| DHT11 Pin | Arduino | Notes |
|---|---|---|
| Pin 1 (VCC) | 5V | |
| Pin 2 (DATA) | Digital pin 2 | + 10kΩ pull-up to 5V |
| Pin 3 (NC) | — | Leave unconnected |
| Pin 4 (GND) | GND |
The 10kΩ pull-up resistor connects between the DATA pin and 5V. Without it, readings will be unreliable.
Install the DHT Library
- Open Arduino IDE
- Go to Sketch → Include Library → Manage Libraries
- Search for
DHT sensor libraryby Adafruit - Click Install — also install Adafruit Unified Sensor if prompted
Or install manually: download from github.com/adafruit/DHT-sensor-library and add via Sketch → Include Library → Add .ZIP Library.
Code — Read Temperature and Humidity
#include "DHT.h"
#define DHTPIN 2 // Data pin connected to digital pin 2
#define DHTTYPE DHT11 // Sensor type: DHT11 or DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 Temperature & Humidity Sensor");
dht.begin();
}
void loop() {
delay(2000); // DHT11 needs at least 1 second between readings
float humidity = dht.readHumidity();
float tempC = dht.readTemperature(); // Celsius (default)
float tempF = dht.readTemperature(true); // Fahrenheit
// Check for failed reading
if (isnan(humidity) || isnan(tempC)) {
Serial.println("ERROR: Failed to read from DHT sensor.");
Serial.println("Check wiring and try again.");
return;
}
// Calculate heat index
float heatIndexC = dht.computeHeatIndex(tempC, humidity, false);
float heatIndexF = dht.computeHeatIndex(tempF, humidity);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print(" °C / ");
Serial.print(tempF);
Serial.println(" °F");
Serial.print("Heat Index: ");
Serial.print(heatIndexC);
Serial.print(" °C / ");
Serial.print(heatIndexF);
Serial.println(" °F");
Serial.println("---");
}
Expected Serial Monitor output:
DHT11 Temperature & Humidity Sensor
Humidity: 45.00 %
Temperature: 23.00 °C / 73.40 °F
Heat Index: 22.54 °C / 72.57 °F
---
Display on LCD 16x2
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27 or 0x3F
void setup() {
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("DHT11 Sensor");
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t, 1);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(h, 1);
lcd.print(" %");
}
}
Simulate on Wokwi
Test without hardware: wokwi.com includes the DHT11 sensor. Create an Arduino project, add DHT11 to pin 2, and paste the code above.
See the Wokwi guide for setup instructions.
Troubleshooting
Serial Monitor shows "Failed to read from DHT sensor":
- Check that DATA pin is connected to the correct Arduino pin (default: pin 2)
- If using bare DHT11, verify the 10kΩ pull-up resistor is installed
- Change
#define DHTPIN 2if you're using a different pin - Make sure you selected DHT11 not DHT22 in the library
Reading shows 0°C and 0% humidity:
- Usually a wiring issue. Double-check VCC (5V), GND, and DATA connections
- Try a different Arduino digital pin
Values fluctuate wildly:
- DHT11 needs at least 1 second between readings. Don't reduce the
delay(2000) - Check for loose wiring on a breadboard
"DHT.h: No such file or directory" error:
- The DHT library is not installed. Follow the Library Manager steps above.
Library installed but still errors:
- In Arduino IDE, go to Sketch → Include Library and verify DHT sensor library appears
- Close and reopen Arduino IDE after installing
Tips for Better Accuracy
- Avoid direct sunlight on the sensor — it reads higher temperatures
- Keep away from heat sources (voltage regulators, motor drivers)
- Allow 30 seconds warm-up after powering on before trusting readings
- Average multiple readings for a more stable value:
float avgTemp = 0;
for (int i = 0; i < 5; i++) {
avgTemp += dht.readTemperature();
delay(1000);
}
avgTemp /= 5;
Frequently Asked Questions (FAQ)
What is the DHT11 used for? Indoor temperature and humidity monitoring. Common applications: weather stations, HVAC control, terrariums, server room monitoring, home automation systems.
Can I use the DHT11 with ESP32? Yes. Connect DATA to any GPIO (e.g., GPIO 4). Change #define DHTPIN 4. The library works identically on ESP32.
How many DHT11 sensors can I connect to one Arduino? Each sensor needs its own data pin and pull-up resistor. An Arduino Uno supports up to 14 DHT11 sensors simultaneously (limited by digital pins).
What's the maximum cable length for DHT11? Up to 20 meters with a 4.7kΩ pull-up resistor. For longer runs, use shielded cable and reduce pull-up to 10kΩ.
Can DHT11 measure below 0°C? No. DHT11 range is 0–50°C. For sub-zero temperatures, use a DHT22 (range: -40°C to 80°C) or DS18B20.
Why does my DHT11 show 0% humidity? Usually a wiring problem. Confirm the DATA pin connection and the pull-up resistor (for bare sensors). Also try increasing the delay() to 3000 ms.
Related Guides
- Wokwi Simulator — Test this circuit online first
- Arduino Nano vs Uno — Which board to use
- ESP32 vs Arduino — Upgrade to wireless IoT
- Voltage Divider Calculator — For sensor circuits