Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 19: Temperature and Humidity Sensor Module (DHT11)
In this lesson, you will learn how to read temperature and humidity from a DHT11 sensor using an ESP32 Development Board. We’ll also cover interpreting these readings and calculating the heat index in both Celsius and Fahrenheit. This project is ideal for beginners in environmental sensing, providing hands-on experience with sensor data acquisition and basic concepts of climate monitoring on the ESP32 platform.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
Wiring
Note
The kit may contain different versions of the DHT11 module. Please confirm the wiring method according to the module you have.
module |
diagram |
|---|---|
Code
Note
To install the library, use the Arduino Library Manager and search for “DHT sensor library” and install it.
Code Analysis
Inclusion of necessary libraries and definition of constants. This part of the code includes the DHT sensor library and defines the pin number and sensor type used in this project.
Note
To install the library, use the Arduino Library Manager and search for “DHT sensor library” and install it.
#include <DHT.h> #define DHTPIN 25 // Define the pin used to connect the sensor #define DHTTYPE DHT11 // Define the sensor type
Creation of DHT object. Here we create a DHT object using the defined pin number and sensor type.
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object
This function is executed once when the ESP32 Development Board starts. We initialize the serial communication and the DHT sensor in this function.
void setup() { Serial.begin(9600); Serial.println(F("DHT11 test!")); dht.begin(); // Initialize the DHT sensor }
Main loop. The
loop()function runs continuously after the setup function. Here, we read the humidity and temperature values, calculate the heat index, and print these values to the serial monitor. If the sensor read fails (returns NaN), it prints an error message.Note
The heat index is a way to measure how hot it feels outside by combining the air temperature and the humidity. It is also called the “felt air temperature” or “apparent temperature”.
void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); float f = dht.readTemperature(true); if (isnan(h) || isnan(t) || isnan(f)) { Serial.println(F("Failed to read from DHT sensor!")); return; } float hif = dht.computeHeatIndex(f, h); float hic = dht.computeHeatIndex(t, h, false); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.print(F("°C ")); Serial.print(f); Serial.print(F("°F Heat index: ")); Serial.print(hic); Serial.print(F("°C ")); Serial.print(hif); Serial.println(F("°F")); }



