Teach Your ESP32 to Feel the Weather
The Story
Your ESP32 already knows how to blink an LED. Now it needs a sense of the room around it.
The DHT11 is a beginner-friendly digital sensor that measures air temperature and relative humidity. It is used in small weather stations, room comfort monitors, greenhouse controllers, smart thermostats, and school science projects.
In this mission you will connect the DHT11 to one ESP32 GPIO pin, read live values in Serial Monitor, and learn why sensor timing, pull-up resistors, and clean power matter.
Explain Like I'm 12
The DHT11 is like a tiny weather reporter. It has power, ground, and one data wire. The ESP32 asks, "What is the room like right now?" and the sensor replies with two numbers: temperature and humidity.
Humidity means how much water vapor is in the air. High humidity can feel sticky. Low humidity can feel dry. Temperature tells you how warm the air is.
Watch the tutorial
This tutorial and guide use DHT11. Follow this guide’s GPIO4 wiring and DHT11 code when building.
Components Required
- ESP32 DevKit boardClassic ESP32-WROOM DevKit / ESP32 Dev Module. This guide uses GPIO4; other families require their own pin check.
- DHT11 temperature and humidity sensor moduleA 3-pin module is easiest because it usually includes the pull-up resistor.
- 10 k ohm resistorOnly required if you use a bare 4-pin DHT11 sensor without a module board.
- BreadboardHalf-size breadboard is enough.
- Jumper wiresUse short, firm male-to-male wires for stable readings.
- USB data cableCharge-only cables can power the board but cannot upload code.
How the DHT11 Actually Works
The DHT11 is a digital temperature and humidity sensor. Inside the package are a humidity sensing element, a temperature sensor, and a small controller that converts the measurement into a digital signal.
It does not behave like an analog sensor. The ESP32 cannot use analogRead() here. Instead, the ESP32 sends a start signal on one GPIO pin. The DHT11 replies by pulling that same data line HIGH and LOW in a timed pattern. The Arduino DHT library measures those pulses and converts them into humidity and temperature values.
The data line needs a pull-up resistor so it normally rests at 3.3 V. Many DHT11 modules already include this resistor. A bare DHT11 sensor usually needs an external 10 k ohm resistor between DATA and 3.3 V.
The linked Aosong DHT11 manual specifies a sampling period greater than 2 seconds and recommends more than 5 seconds between reads for accurate data. The supplied example uses a two-second delay, matching its recorded build. This is the contributor timing, not proof of compliance with the manual recommendation. Separately, Adafruit DHT 1.4.6 returns its cached result if another read is requested within 2000 ms. That library limit does not establish the sensor’s sampling specification or guarantee a new physical measurement on every line. The DHT11 provides coarse readings; showing a decimal digit does not imply greater sensor accuracy.
Wiring Diagram
Follow these steps in order. Unplug USB before you change any wires.
-
1
Unplug the ESP32 USB cable before wiring.
-
2
Place the DHT11 module on the breadboard so each pin has its own row. Follow its printed labels; module pin order varies.
-
3
For a bare 4-pin sensor viewed from the front grille, pins left to right are VCC, DATA, NC, and GND.
-
4
Connect DHT11 VCC or + to ESP32 3.3 V.
-
5
Connect DHT11 GND or - to ESP32 GND.
-
6
Connect DHT11 DATA or OUT to ESP32 GPIO4.
-
7
If you are using a bare 4-pin DHT11, add a 10 k ohm resistor from DATA to 3.3 V.
-
8
Leave the unused NC pin on a bare DHT11 disconnected.
-
9
In Arduino IDE, install "DHT sensor library" by Adafruit and "Adafruit Unified Sensor" from Library Manager.
Arduino Code
Copy this into Arduino IDE, then click Upload.
#include "DHT.h"
// DHT11 data pin connected to ESP32 GPIO4.
#define DHTPIN 4
// Tell the library which DHT sensor model you are using.
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
delay(2000);
dht.begin();
Serial.println("DHT11 ESP32 room sensor ready");
Serial.println("Reading every 2 seconds...");
}
void loop() {
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
float temperatureF = dht.readTemperature(true);
if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
Serial.println("Read failed. Check wiring, power, pull-up resistor, and library setup.");
delay(2000);
return;
}
float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
Serial.print("Temperature: ");
Serial.print(temperatureC, 1);
Serial.print(" C / ");
Serial.print(temperatureF, 1);
Serial.print(" F | Humidity: ");
Serial.print(humidity, 1);
Serial.print(" % | Heat index: ");
Serial.print(heatIndexC, 1);
Serial.println(" C");
delay(2000);
}
- DHTPIN must match the GPIO you wired to the DATA pin.
- DHTTYPE must be DHT11 to match the sensor used in this mission and video.
- Review baseline: classic ESP32-WROOM, Arduino-ESP32 3.3.2, Adafruit DHT sensor library 1.4.6 and Adafruit Unified Sensor. This is not a latest-release claim.
- The supplied example waits 2 seconds at startup and between read attempts. It matches the original contributor sketch. The library cache interval is a separate 2000 ms policy; printed lines do not guarantee fresh physical samples.
- Humidity and temperature calls use the same cached transaction; do not force repeated reads to bypass timing.
- Documentation review only; compilation and hardware testing were not performed.
- isnan() protects your program from printing bad values when a read fails.
Supplied DHT11 build and Serial readings
Explore the build photos and recorded results. Open any image to inspect the details.
About this recorded build
The photos and IDE captures document the supplied two-second sketch displayed above. No independent compilation, board upload or accuracy measurement was performed in this integration.
Owner-supplied build records. Photos can hide breadboard contacts; use the guide wiring and actual module labels. These captures are not an independent hardware retest or accuracy measurement.
Guide 19 uses the blue DHT11 module and DHTTYPE DHT11 on GPIO4. The existing URL is retained so previous links continue to work.
The library capture shows Adafruit DHT sensor library 1.4.7 installed. This records the supplied setup; the separate documentation review baseline remains 1.4.6. The installed ESP32 core and dependency versions are not shown.
Serial Monitor shows about 30.1 C, 47.0% RH and a calculated 30.7 C heat index at 115200 baud. Repeated displayed values do not establish fresh samples or calibrated accuracy.

DHT11 wiring overview for the supplied GPIO4 sketch; verify the actual module S, supply and ground labels.
View full-size image
The supplied sensor is DHT11, as specified in the owner note and sketch.
View full-size image
Supplied library setup capture: Adafruit DHT sensor library 1.4.7.
View full-size image
Supplied upload-completion capture for the DHT11 sketch.
View full-size image
Displayed results: 30.1 C, 47.0% RH and calculated heat index 30.7 C; these are supplied readings, not accuracy validation.
View full-size imageCode and files
Expected Behaviour
Open Serial Monitor at 115200 baud. After the two-second startup delay, readings appear about every 2 seconds. Illustrative output, not a recorded measurement:
Temperature: 24.0 C / 75.2 F | Humidity: 55.0 % | Heat index: 23.9 C
Warm the sensor gently with your hand or breathe near it from a short distance. Temperature or humidity should change slowly. Do not touch the sensor grill or expose the board to moisture.
Troubleshooting
Most ESP32 problems are wiring, power, library, or timing issues. Check these first.
Serial Monitor says "Read failed"
Likely cause: DATA is on the wrong GPIO, DHTTYPE does not match the sensor, the pull-up resistor is missing, or the sensor is being read too quickly.
Fix: Confirm DHTTYPE is DHT11 and DATA goes to GPIO4, use 3.3 V and GND, add a 10 k ohm pull-up for a bare sensor, and increase the sampling interval according to your sensor’s documentation if reads fail.
Code does not compile because DHT.h is missing
Likely cause: The Adafruit DHT library is not installed, or the wrong library was installed.
Fix: Open Arduino IDE Library Manager and install "DHT sensor library" by Adafruit plus "Adafruit Unified Sensor".
Values are unrealistic or stuck
Likely cause: Loose jumper wires, poor breadboard contact, heat from your fingers, or airflow directly onto the sensor.
Fix: Reseat the wires, keep the sensor away from the warm ESP32 board, and wait a few readings for the value to settle.
ESP32 uploads fail after wiring the sensor
Likely cause: The sensor may be connected to a boot strapping pin or a wiring mistake may pull a pin into the wrong state during reset.
Fix: Use GPIO4 for this beginner circuit and disconnect power before rewiring.
Sensor works on Arduino Uno examples but not ESP32
Likely cause: Some examples use 5 V assumptions or a different pin number.
Fix: Use 3.3 V logic, update DHTPIN to your ESP32 GPIO number, and select an ESP32 board in Arduino IDE.
Knowledge Check
Quick check — no grades, just confidence!
Q1. Does a two-second print interval guarantee a fresh, accurate DHT11 measurement?
Q2. What does NaN mean in the Serial output?
Q3. Why does the DATA pin need a pull-up resistor?
Mini Challenge
No wrong answers — experiment and have fun!
- Print "Too warm" when temperature is above 28 C.
- Print "Very humid" when humidity is above 70 percent.
- Average five readings before printing to make the output calmer.
- Combine this with the OLED mission and show the values on a display.
FAQs
Can I power the DHT11 from 5 V with an ESP32?
This circuit assumes a documented 3.3 V-compatible DHT11/module with DATA pulled up to 3.3 V. Some revisions specify a higher minimum supply; check the actual part. A 5 V-powered module may pull DATA to 5 V and require suitable signal-level interfacing. Do not simply change this circuit’s supply to 5 V.
Why does the DHT11 sometimes return NaN?
NaN means the library did not receive a valid reading. The usual causes are wrong pin number, missing pull-up resistor, loose jumper wires, incorrect library, or reading faster than the sensor can update.
Can I use a DHT22 instead?
Yes. Change DHTTYPE to DHT22 and follow the separate DHT22 component reference at /components/dht22.html. This mission and its code use DHT11.
Which ESP32 GPIO should I use for DHT11 data?
GPIO4 is a safe beginner choice. GPIO16, GPIO17, GPIO18, GPIO19, GPIO21, GPIO22, GPIO23, GPIO25, GPIO26, and GPIO27 can also work. Avoid boot strapping pins until you understand their startup behavior.
