Utility Bar

ESP32 Using DHT11 Temperature & Humidity Sensor - Lesson 7

ESP32 Using DHT11 Temperature & Humidity Sensor

Ethan Zaitchik |

Table of Contents

    In this lesson you will learn how to use the DHT11 sensor with your ESP32. The DHT11 can measure temperature and humidity, and you can display the readings in the Serial Monitor or on an OLED display.

    What you will learn

    • How the DHT11 sensor works
    • How to wire the DHT11 to the ESP32
    • How to install the DHT library
    • How to read temperature and humidity using code
    • How to display readings on the Serial Monitor

    Components needed

    Get All the Parts You Need

    This tutorial is part of our comprehensive ESP32 learning series. Instead of buying components individually, save time and money with our ESP32 Basic Starter Kit. It includes everything you need for this lesson and 20+ other projects.

    What's Included: ESP32 board, OLED display, sensors (DHT11, PIR, LDR), relay module, buzzers, LEDs, buttons, breadboard, resistors, and all cables, plus access to our complete lesson plans.

    View ESP32 Starter Kit →

    Step 1 - Understanding the DHT11

    The DHT11 is a digital sensor that can measure temperature and humidity. It has three main pins: VCC, GND, and DATA. The DATA pin sends the sensor readings to the ESP32.

    Step 2 - Wiring the DHT11

    1. Place the DHT11 on the breadboard
    2. Connect the VCC pin to 3.3 V on the ESP32
    3. Connect the GND pin to GND on the ESP32
    4. Connect the DATA pin to GPIO 5 on the ESP32

    DHT11 ESP32 wiring diagram

    Step 3 - Installing the DHT Library

    1. Within the Arduino IDE, click Sketch
    2. Click Include Library
    3. Click Manage Libraries
    4. In the Library Manager search box, search DHT sensor library
    5. Under DHT sensor library by Adafruit click Install
    DHT sensor library install screenshot

    Step 4 - Writing the Code to Read the DHT11

    Open the Arduino IDE and enter the following code. This reads the temperature and humidity and prints it to the Serial Monitor. Once entered, upload the code.

    #include "DHT.h"
    #define DHTPIN 5      // GPIO pin connected to the DHT11
    #define DHTTYPE DHT11 // DHT 11 sensor
    
    DHT dht(DHTPIN, DHTTYPE);
    
    void setup() {
      Serial.begin(115200);
      dht.begin();
    }
    
    void loop() {
      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");
    
      delay(2000);
    }
    

    How the Code Works

    This program reads temperature and humidity from the DHT11 sensor and prints it to the Serial Monitor.

    #include "DHT.h"
    Imports the DHT sensor library so the ESP32 can communicate with the sensor.
    #define DHTPIN 5
    Specifies GPIO 5 for the DHT11 data pin.
    #define DHTTYPE DHT11
    Specifies the sensor model as DHT11.
    DHT dht(DHTPIN, DHTTYPE);
    Creates a DHT sensor object that will read temperature and humidity.
    void setup()
    Runs once when the ESP32 starts.
    Serial.begin(115200);
    Starts serial communication to view readings.
    dht.begin();
    Initializes the DHT11 sensor.
    void loop()

    Runs continuously after setup.

    • dht.readHumidity() reads relative humidity
    • dht.readTemperature() reads temperature in °C
    • Checks if readings are valid using isnan() and prints an error if not
    • Serial.print() and Serial.println() output the readings to the Serial Monitor
    • delay(2000) waits 2 seconds before repeating for stable readings

    Step 5 - Expected Result

    Serial Monitor output
    DHT11 sensor photo
    1. Open the Serial Monitor in Arduino IDE
    2. Set the baud rate to 115200
    3. Humidity and temperature values should update every two seconds
    4. If it shows “Failed to read from DHT sensor,” check wiring

    Optional Challenge

    Try simulating a weather monitor by adding a few LEDs that are triggered at certain temperature or humidity levels.

    ESP32 Lessons

    Lesson Complete

    You have successfully read temperature and humidity from the DHT11 using your ESP32. Next lesson: How to use the PIR motion sensor (HC-SR501) to detect movement.

    Leave a comment