Introduction
In this lesson, the ESP32 will read real environmental data.
We will measure temperature and humidity using a DHT11 or DHT22 sensor. This is a common beginner project, but it also introduces an important idea: many sensors do not simply output an analog voltage. Instead, they communicate using a digital signal and a library handles the timing.
This is the first step toward weather stations, smart rooms, greenhouses, and IoT monitoring systems.
What you will learn
By the end of this tutorial, you should understand:
- What temperature and humidity sensors measure
- Difference between analog sensors and digital sensors
- How to wire a DHT11 or DHT22 sensor
- How to install an Arduino library
- How to read temperature and humidity
- How to detect failed readings
- How this project connects to IoT dashboards
DHT11 vs DHT22
Both sensors measure temperature and relative humidity.
The DHT11 is usually cheaper and less accurate. The DHT22 usually has a wider measurement range and better accuracy.
For beginner code, the wiring and library usage are almost the same. The main difference is the sensor type you select in code.
#define DHTTYPE DHT11
or:
#define DHTTYPE DHT22
Digital sensor concept
In the LDR and potentiometer lessons, the ESP32 measured voltage with the ADC.
A DHT sensor works differently. It measures temperature and humidity internally, then sends the result as digital data on its DATA pin.
The timing of that signal is specific, so we use a library instead of manually reading the pin.
Hardware required
- ESP32 DevKit
- DHT11 or DHT22 sensor module
- Breadboard
- Jumper wires
- USB cable
If you are using a bare DHT sensor instead of a module, add a 10k pull-up resistor between VCC and DATA.
Wiring
Connect the sensor:
DHT VCC -> 3.3V
DHT GND -> GND
DHT DATA -> GPIO4
Many DHT modules already include the required pull-up resistor. Bare sensors usually need an external pull-up resistor.
Install the library
In Arduino IDE:
- Open Library Manager
- Search for
DHT sensor library - Install the Adafruit DHT sensor library
- Install dependencies if Arduino IDE asks
Libraries are useful because they hide low-level timing details and give us simple functions like readTemperature() and readHumidity().
Source code
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
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");
delay(2000);
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000);
}
If you are using a DHT22, change:
#define DHTTYPE DHT11
to:
#define DHTTYPE DHT22
Code explanation
First, include the library:
#include "DHT.h"
Then define the data pin and sensor type:
#define DHTPIN 4
#define DHTTYPE DHT11
Create the sensor object:
DHT dht(DHTPIN, DHTTYPE);
Start the sensor in setup():
dht.begin();
Read values in loop():
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
The DHT library returns NaN, or “not a number”, when the reading fails. That is why we check:
if (isnan(humidity) || isnan(temperature))
This prevents the program from treating invalid data as real sensor data.
Test the sensor
Open Serial Monitor at 115200 baud.
You should see temperature and humidity values every two seconds.
If you place your hand near the sensor, the temperature or humidity may change slightly. Avoid heating the sensor strongly or breathing directly on it for a long time, because the readings may become slow to recover.
Common mistakes
If the reading fails:
- Check VCC and GND
- Check that the data pin matches the code
- Confirm the selected sensor type is correct
- Add a pull-up resistor if using a bare sensor
- Do not read the sensor too quickly
DHT sensors are slow compared with many other sensors. A two-second delay is a reasonable beginner value.
Real engineering use cases
Temperature and humidity sensing is used in:
- Weather stations
- Smart room monitors
- Server room monitoring
- Greenhouses
- Storage rooms
- HVAC systems
- IoT dashboards
The value of this project becomes much larger when combined with WiFi, displays, logging, and alerts.
Engineering challenge
Add a warning condition:
- If temperature is above a chosen limit, print
Temperature warning - If humidity is above a chosen limit, print
Humidity warning
This is the first step toward monitoring and alert systems.
Next lesson
Next, we will connect the ESP32 to WiFi. Once the board is on the network, sensor projects can become web dashboards, IoT devices, and remote monitoring systems.