Skip to main content
ESPBoards

ESP32 DHT11 Temperature and Humidity Sensor

The DHT11 is a low-cost digital sensor for measuring temperature and humidity. It provides calibrated digital outputs and is easy to interface with microcontrollers. With a temperature measurement range of 0-50°C and humidity range of 20-90%, it's suitable for basic environmental sensing applications.

Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

Quick Links

DHT11 Temperature and Humidity Sensor Datasheet ButtonDHT11 Temperature and Humidity Sensor Specs ButtonDHT11 Temperature and Humidity Sensor Specs ButtonDHT11 Temperature and Humidity Sensor Specs Button

DHT11 Price

Normally, the DHT11 Temperature and Humidity Sensor costs around $2 per unit.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

About DHT11 Temperature and Humidity Sensor

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and outputs a digital signal on the data pin (no analog input pins needed). It's fairly simple to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds.

DHT11 Sensor Technical Specifications

Below you can see the DHT11 Temperature and Humidity Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.

  • Protocol: Single-Wire Digital
  • Interface: Single-Wire Digital
  • Temperature Range: 0°C to 50°C
  • Humidity Range: 20% to 90% RH
  • Temperature Accuracy: ±2°C
  • Humidity Accuracy: ±5% RH
  • Operating Voltage: 3.3V to 5.5V
  • Sampling Rate: Once every 2 seconds

DHT11 Sensor Pinout

Below you can see the pinout for the DHT11 Temperature and Humidity Sensor. The VCC pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND pin is the ground connection and must be connected to the ground of your ESP32!

The DHT11 pinout is straightforward:

  • VCC (Power): Connect to 3.3V or 5V power supply.
  • DATA: Outputs digital signal; connect to a digital input on your microcontroller.
  • NC (Not Connected): No connection required.
  • GND (Ground): Connect to the ground of the microcontroller.

DHT11 Wiring with ESP32

Below you can see the wiring for the DHT11 Temperature and Humidity Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.

To interface the DHT11 with an ESP32:
  • Connect the VCC pin to the 3.3V or 5V pin on the ESP32.
  • Connect the GND pin to the ground (GND) of the ESP32.
  • Connect the DATA pin to a digital GPIO pin on the ESP32 (e.g., GPIO 4).
  • Place a 10kΩ pull-up resistor between the DATA pin and VCC to ensure reliable communication.

Code Examples

Below you can find code examples of DHT11 Temperature and Humidity Sensor with ESP32 in several frameworks:

Arduino Core Image

ESP32 DHT11 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the DHT11 Temperature and Humidity Sensor:

#include "DHT.h"

#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT11 sensor type

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(115200);
Serial.println("DHT11 Sensor Initialization");
dht.begin();
}

void loop() {
delay(2000); // Wait a few seconds between measurements

float humidity = dht.readHumidity();
float temperature = dht.readTemperature();

if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
}

This Arduino sketch interfaces with the DHT11 sensor to read temperature and humidity data. It utilizes the DHT library to communicate with the sensor. The setup() function initializes serial communication and the sensor. In the loop() function, it waits for 2 seconds between measurements, reads humidity and temperature, checks for successful readings, and then prints the values to the Serial Monitor.

Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP-IDF Image

ESP32 DHT11 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the DHT11 Temperature and Humidity Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main ESP-IDF file:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

#define DHT11_PIN GPIO_NUM_4

void dht11_task(void *pvParameter) {
// DHT11 reading logic here
while (1) {
// Read temperature and humidity
// Print values
vTaskDelay(2000 / portTICK_PERIOD_MS); // Delay for 2 seconds
}
}

void app_main() {
xTaskCreate(&dht11_task, "dht11_task", 2048, NULL, 5, NULL);
}

This ESP-IDF code sets up a FreeRTOS task to read data from the DHT11 sensor connected to GPIO 4. The dht11_task function contains the logic to read temperature and humidity from the sensor and print the values. The task runs in an infinite loop with a 2-second delay between readings. The app_main function creates this task with a stack size of 2048 bytes and a priority of 5.

Update the I2C pins (I2C_MASTER_SDA_IO and I2C_MASTER_SCL_IO) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESPHome Image

ESP32 DHT11 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the DHT11 Temperature and Humidity Sensor

sensor:
- platform: dht
model: DHT11
pin: GPIO4
temperature:
name: "Living Room Temperature"
humidity:
name: "Living Room Humidity"
update_interval: 60s

This ESPHome configuration specifies the dht platform and sets the model to DHT11 for correct handling of the sensor. The pin key defines the GPIO pin (e.g., GPIO4) to which the sensor's data pin is connected. The temperature and humidity keys define the sensor outputs, giving user-friendly names like 'Living Room Temperature' and 'Living Room Humidity.' The update_interval is set to 60 seconds, which means the ESP32 will read and update the sensor data every 60 seconds.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 DHT11 PlatformIO Code Example

Example in PlatformIO Framework

For PlatformIO, make sure to configure the platformio.ini file with the appropriate environment and libraries, and then proceed with the code.

Configure platformio.ini

First, your platformio.ini should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/DHT sensor library @ ^1.4.3
adafruit/Adafruit Unified Sensor @ ^1.1.6
monitor_speed = 115200

ESP32 DHT11 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the DHT11 Temperature and Humidity Sensor:

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 4 // Define the GPIO pin for the data pin
#define DHTTYPE DHT11 // Specify the DHT model

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(115200);
Serial.println("DHT11 Sensor Example");

// Initialize the sensor
dht.begin();
}

void loop() {
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();

// Check if any readings failed
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Print results to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

delay(2000); // Wait before next reading
}

The platform = espressif32 specifies that the project is for ESP32 boards, and board = esp32dev sets the specific development board being used. The lib_deps field includes the required libraries for the DHT11 sensor (DHT sensor library and Adafruit Unified Sensor), ensuring compatibility. The code defines the GPIO pin (e.g., GPIO4) for the sensor’s data line and initializes the DHT11 sensor using the DHT library. Temperature and humidity readings are printed to the Serial Monitor every 2 seconds, with error handling to check for invalid readings.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 DHT11 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the DHT11 Temperature and Humidity Sensor with your ESP32.

from machine import Pin
from time import sleep
import dht

# Initialize the DHT11 sensor on GPIO4
dht_sensor = dht.DHT11(Pin(4))

print("DHT11 Sensor Example")

while True:
try:
# Measure temperature and humidity
dht_sensor.measure()
temperature = dht_sensor.temperature() # Temperature in Celsius
humidity = dht_sensor.humidity() # Relative Humidity in %

# Print temperature and humidity to the console
print("Temperature: {:.1f} °C".format(temperature))
print("Humidity: {:.1f} %".format(humidity))
except OSError as e:
print("Failed to read sensor.")

# Delay between readings
sleep(2)

The dht module simplifies interactions with the DHT11 sensor, handling initialization and data retrieval. The sensor is initialized with the GPIO pin (e.g., GPIO4), and a confirmation message is printed to indicate successful setup. Inside an infinite loop, the script continuously reads the temperature (in Celsius) and relative humidity (in percentage) using the measure() method. The readings are formatted and printed to the console. Error handling with try and except ensures the program can recover from sensor read failures. A 2-second delay between readings ensures manageable updates.

Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy.

Conclusion

We went through technical specifications of DHT11 Temperature and Humidity Sensor, its pinout, connection with ESP32 and DHT11 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.