ESP32 DHT11 Temperature and Humidity Sensor Tutorial

Learn how to interface a DHT11 temperature and humidity sensor with the ESP32 development board using the Arduino IDE. This beginner-friendly tutorial explains the hardware connections, required libraries, complete Arduino code, and how to monitor real-time environmental readings through the Serial Monitor.

The DHT11 is an affordable digital sensor designed to measure ambient temperature and relative humidity. When connected to the ESP32, it can serve as the foundation for weather monitoring stations, indoor climate monitoring systems, smart home automation, and IoT projects. The ESP32 reads the sensor data and makes it available for further processing, wireless transmission, or cloud-based monitoring.

Project Overview

  • Microcontroller: ESP32 development board
  • Sensor: DHT11 temperature and humidity sensor
  • Communication: Digital single-wire interface
  • Programming environment: Arduino IDE
  • Output: Temperature in degrees Celsius and relative humidity as a percentage
  • Applications: Environmental monitoring, smart homes, and IoT weather stations

1. Hardware Requirements and Wiring

Before starting, gather an ESP32 development board, a DHT11 sensor module, jumper wires, and a USB cable for programming. A breadboard is optional but useful for prototyping.

Connect the DHT11 sensor to the ESP32 according to the wiring table below. This example uses GPIO 4 as the digital data pin.

DHT11 PinFunctionESP32 Connection
VCC / +Power supply3.3V
GND / -GroundGND
DATA / OUTDigital sensor dataGPIO 4

Important: Check the pin labels and voltage requirements of your particular DHT11 module. A bare DHT11 sensor generally requires a pull-up resistor on the data line, commonly around 4.7kΩ–10kΩ between DATA and VCC. Many breakout modules already include this resistor. When connecting the data line to the ESP32, ensure the signal voltage does not exceed 3.3V.

2. Installing the Required Arduino Libraries

The ESP32 needs a compatible DHT sensor library to communicate with the sensor and convert its digital readings into temperature and humidity values. Install the following libraries using the Arduino IDE Library Manager:

  • DHT sensor library by Adafruit: Provides functions for reading temperature and humidity from DHT-series sensors.
  • Adafruit Unified Sensor: Provides a common sensor interface used by compatible Adafruit libraries.

Open the Arduino IDE, select Sketch > Include Library > Manage Libraries, and search for each library by name. Install both libraries before compiling the example.

Make sure ESP32 board support is installed in the Arduino IDE and select the appropriate ESP32 board and port under the Tools menu.

3. Complete ESP32 DHT11 Arduino Code

The following program initializes the DHT11 sensor, reads humidity and temperature every two seconds, checks for invalid sensor readings, and displays the results in the Serial Monitor. The example uses GPIO 4 for the sensor data connection and a baud rate of 115200.

#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println("ESP32 DHT11 Temperature and Humidity Monitor");
  dht.begin();
}

void loop() {
  delay(2000);

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

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

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

Understanding the Code

  • DHTPIN and DHTTYPE: Define the ESP32 GPIO connected to the sensor and identify the sensor as a DHT11.
  • dht.begin(): Initializes communication with the DHT11 sensor.
  • dht.readHumidity(): Retrieves relative humidity as a percentage.
  • dht.readTemperature(): Retrieves temperature in degrees Celsius by default.
  • isnan(): Detects invalid readings so the program does not display failed measurements as valid data.
  • delay(2000): Waits two seconds between readings, which is appropriate for the DHT11's relatively slow sampling rate.

4. Uploading the Program and Viewing Sensor Readings

Connect the ESP32 to your computer with a USB cable. Open the Arduino IDE, paste the program into a new sketch, and select the correct ESP32 board and serial port.

  • Verify the sketch to check for compilation errors.
  • Upload the program to the ESP32.
  • Open the Serial Monitor after the upload completes.
  • Set the Serial Monitor baud rate to 115200.
  • Wait for the sensor to produce readings, which should appear approximately every two seconds.

Example Serial Monitor output:

ESP32 DHT11 Temperature and Humidity Monitor
Humidity: 58.0 %  |  Temperature: 27.0 °C
Humidity: 58.0 %  |  Temperature: 27.1 °C
Humidity: 57.0 %  |  Temperature: 27.1 °C

These values are illustrative only. Your actual readings will depend on the surrounding temperature, humidity, sensor accuracy, and environmental conditions.

5. Common Problems and Troubleshooting Tips

  • Failed to read from DHT11 sensor: Check the VCC, GND, and DATA connections. Confirm that GPIO 4 matches the DHTPIN definition and that the correct sensor type is selected.
  • Incorrect or unstable readings: Avoid loose jumper wires, allow the sensor to stabilize after powering up, and keep it away from direct sunlight, hot components, and sources of moisture.
  • Compilation errors involving DHT.h: Install the DHT sensor library by Adafruit and the Adafruit Unified Sensor library through the Arduino IDE Library Manager.
  • No output in the Serial Monitor: Confirm that the correct port is selected and that the baud rate is set to 115200.
  • Sensor works intermittently: Check whether your sensor module includes a pull-up resistor. For a bare sensor, add an appropriate pull-up resistor between DATA and the 3.3V supply.
  • Readings seem inaccurate: The DHT11 is a basic sensor with limited accuracy and resolution. For applications requiring more precise measurements, consider a DHT22 or a modern digital temperature and humidity sensor.

6. Practical Applications of the ESP32 DHT11 Sensor

Once the sensor is working, you can extend the project into a range of environmental monitoring and automation applications:

  • IoT Weather Station: Collect temperature and humidity readings and transmit them to an online dashboard using the ESP32's Wi-Fi connectivity.
  • Smart Home Monitoring: Monitor indoor conditions and trigger notifications or automation rules when readings cross configured thresholds.
  • Greenhouse Monitoring: Track environmental conditions to support plant care and improve ventilation control.
  • Server Room Monitoring: Observe ambient temperature and create warning systems when temperatures exceed a defined limit.
  • Data Logging: Record readings at regular intervals for later analysis of temperature and humidity trends.

Conclusion

Interfacing a DHT11 temperature and humidity sensor with the ESP32 is a practical introduction to sensor-based electronics and IoT development. By connecting the sensor to GPIO 4, installing the required Adafruit libraries, and uploading the Arduino IDE sketch, you can measure ambient temperature and relative humidity with minimal hardware.

This project provides a foundation for more advanced applications, including wireless environmental dashboards, automated climate control, and cloud-connected monitoring systems. Once the basic readings are reliable, you can build on the example by publishing sensor data over Wi-Fi, displaying measurements on an OLED screen, or storing readings for long-term analysis.