Reading Temperature & Humidity using DHT11 and ESP32 DOIT DevKit v1

adminESP3215 hours ago5 Views

Introduction

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.

Components Required

  • ESP32 DOIT DevKit v1
  • DHT11 sensor (with or without breakout board)
  • Breadboard & jumper wires
  • USB cable

Circuit Diagram

  • DHT11 VCC → 3.3V
  • DHT11 GND → GND
  • DHT11 DATA → GPIO 25

(If your DHT11 has 4 pins, connect the NC pin to nothing.)

Arduino Code

#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");
  }


}

Step-by-Step Code Explanation

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(&amp;event);
      dht.humidity().getEvent(&amp;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.

    Output Example

    DHT11 Sensor Test with ESP32
    Humidity: 65.00 %    Temperature: 29.00 °C
    Humidity: 64.50 %    Temperature: 28.80 °C
    

    Real-Life Applications

    • Weather Stations – Monitor environmental conditions.
    • Smart Agriculture – Track humidity for irrigation systems.
    • Home Automation – Smart thermostats & climate control.
    • IoT Projects – Send data to cloud dashboards (e.g., ThingSpeak, Blynk).

    Troubleshooting

    ProblemCauseSolution
    Reading nan (not a number)Loose wiring / wrong pinRecheck wiring & pin number
    No output in Serial MonitorWrong baud rateSet Serial Monitor to 115200
    Values don’t updateMissing delayAdd delay(2000) in loop
    Library errorsMissing DHT libraryInstall “DHT sensor library by Adafruit” in Arduino IDE

    What is the difference between DHT11 and DHT22?

    DHT22 is more accurate and has a wider range, while DHT11 is cheaper but limited.

    Can I power DHT11 with 5V on ESP32?

    Yes ✅, DHT11 supports both 3.3V and 5V.

    How often should I read values from DHT11?

    At most once every 2 seconds (sensor limitation).

    Can I connect multiple DHT11 sensors to ESP32?

    Yes, but each must be connected to a different GPIO pin.

    Why am I getting nan values?

    It usually means bad wiring or the wrong GPIO pin is defined in the code.

    Leave a reply

    Loading Next Post...
    Follow
    Search Trending
    Popular Now
    Loading

    Signing-in 3 seconds...

    Signing-up 3 seconds...