The DHT11 is a low-cost digital sensor that measures temperature and humidity. It’s easy to use, but less accurate compared to the DHT22. It communicates with the ESP32 using a single digital pin, making it perfect for beginners’ IoT projects.
In this tutorial, we’ll connect the DHT11 sensor to GPIO 25 of the ESP32 DOIT DevKit v1 and read the temperature & humidity values using the Arduino IDE.
(If your DHT11 has 4 pins, connect the NC pin to nothing.)
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
// Define pin and sensor type
#define DHTPIN 25
#define DHTTYPE DHT11 // DHT11, DHT21, DHT22
// Initialize DHT sensor
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
void setup() {
Serial.begin(115200);
Serial.println("DHT11 Unified Sensor Example");
// Start DHT sensor
dht.begin();
Serial.println("DHT11 Sensor Test with ESP32");
// Set delay between sensor readings
delayMS = sensor.min_delay / 1000;
}
void loop() {
// Delay between measurements
delay(delayMS);
sensors_event_t event;
// Get humidity event
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
} else {
Serial.print("Humidity: ");
Serial.print(event.relative_humidity);
Serial.print(" % ");
}
// Get temperature event
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
} else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" °C");
}
}
Include Library
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
We use the official DHT library to handle communication.
Define Pin & Type
// Define pin and sensor type
#define DHTPIN 25
#define DHTTYPE DHT11 // DHT11, DHT21, DHT22
Sensor data pin is connected to GPIO 25.
Create Object
// Initialize DHT sensor
DHT_Unified dht(DHTPIN, DHTTYPE);
This creates a DHT object for reading data.
Setup
Serial.begin(115200);
dht.begin();
Start the serial monitor and initialize the sensor.
Read Data
sensors_event_t event;
dht.temperature().getEvent(&event);
dht.humidity().getEvent(&event);
Read humidity, temperature in °C.
Error Handling
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
} else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" °C");
}
Handles sensor reading errors.
Print Data
Data is printed on the Serial Monitor every 2 seconds.
DHT11 Sensor Test with ESP32
Humidity: 65.00 % Temperature: 29.00 °C
Humidity: 64.50 % Temperature: 28.80 °C
Problem | Cause | Solution |
---|---|---|
Reading nan (not a number) | Loose wiring / wrong pin | Recheck wiring & pin number |
No output in Serial Monitor | Wrong baud rate | Set Serial Monitor to 115200 |
Values don’t update | Missing delay | Add delay(2000) in loop |
Library errors | Missing DHT library | Install “DHT sensor library by Adafruit” in Arduino IDE |
DHT22 is more accurate and has a wider range, while DHT11 is cheaper but limited.
Yes ✅, DHT11 supports both 3.3V and 5V.
At most once every 2 seconds (sensor limitation).
Yes, but each must be connected to a different GPIO pin.
nan
values?It usually means bad wiring or the wrong GPIO pin is defined in the code.