ESP32 DHT22 Temperature Humidity Station: Live Build Guide
- Average speed
- km/h
- Result
- Finish time
- 50 min
- You entered
- Distance
- 10 km
- You entered
Yes, you can build a live ESP32 temperature and humidity station with a DHT22 sensor, and this guide walks you through the hardware, wiring, code, and data display options. You’ll connect the DHT22 to the ESP32, read sensor data, and send it to a local web page or a cloud dashboard like ThingSpeak, all with basic Arduino IDE skills.
What You Need
Before starting, gather these components:
- ESP32 development board (any common variant like the DevKitC or NodeMCU-32S)
- DHT22 (also sold as AM2302) temperature and humidity sensor
- 4.7kΩ resistor (optional, but recommended for stable readings)
- Breadboard and jumper wires
- Micro-USB cable for power and programming
- Arduino IDE (or PlatformIO) with ESP32 board support installed
The DHT22 is a digital sensor that outputs calibrated temperature and relative humidity readings over a single-wire protocol. It measures temperature from -40°C to 80°C (±0.5°C accuracy) and humidity from 0% to 100% (±2% accuracy), making it suitable for indoor weather stations, greenhouses, and server room monitoring.
How the DHT22 Works
The DHT22 uses a capacitive humidity sensor and a thermistor to measure the environment. The sensor sends a 40-bit data packet (16 bits humidity, 16 bits temperature, 8 bits checksum) over a single data line. The ESP32 reads this packet using a timing-sensitive protocol, so it’s best to use a library like DHT sensor library by Adafruit, which handles the low-level timing for you.
Unlike the cheaper DHT11, the DHT22 has better accuracy and a wider range, but it’s slower – you must wait at least 2 seconds between readings to get fresh data. If you need faster sampling, consider a BME280 or SHT31, which communicate over I2C and can read multiple times per second.
Wiring the DHT22 to the ESP32
Connect the DHT22 to the ESP32 as follows:
- DHT22 VCC (pin 1) → ESP32 3.3V (or 5V, but 3.3V is safer and works fine)
- DHT22 DATA (pin 2) → ESP32 GPIO4 (or any digital pin; we’ll use GPIO4 in the code)
- DHT22 GND (pin 4) → ESP32 GND
- Optional: 4.7kΩ resistor between VCC and DATA (pulls the data line high)
Some DHT22 modules (breakout boards) already include the pull-up resistor, so you only need three wires. Double-check the pinout of your specific module – the order of pins can vary between manufacturers.
Programming the ESP32
Install the ESP32 board in Arduino IDE (File → Preferences → Additional Boards Manager URLs: add the ESP32 JSON link, then Board Manager → install “esp32 by Espressif”). Then install the DHT sensor library and Adafruit Unified Sensor library via Library Manager.
Here’s a minimal sketch that reads the DHT22 and prints values to the Serial Monitor:
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: "); Serial.print(h); Serial.print("% ");
Serial.print("Temperature: "); Serial.print(t); Serial.println("°C");
}
Upload the code, open the Serial Monitor at 115200 baud, and you should see readings every 2 seconds. If you get “Failed to read”, check wiring, power, and the pull-up resistor.
Displaying Data Live
To make it a “live” station, you need to display the data somewhere. The simplest option is a local web server on the ESP32. Use the ESP32’s Wi-Fi capabilities to serve a small HTML page that shows current readings. You can also log data to a cloud service like ThingSpeak (free for non-commercial use) by sending HTTP requests at intervals.
For a local web server, include the WiFi library and create a simple HTTP server that returns JSON or HTML. Here’s a snippet to get you started:
#include <WiFi.h>
#include <WebServer.h>
// ... (DHT setup as above)
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
WebServer server(80);
void handleRoot() {
float h = dht.readHumidity();
float t = dht.readTemperature();
String html = "<h1>ESP32 Weather Station</h1>";
html += "<p>Temperature: " + String(t) + " °C</p>";
html += "<p>Humidity: " + String(h) + " %</p>";
server.send(200, "text/html", html);
}
void setup() {
// ... (DHT setup)
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); }
server.on("/", handleRoot);
server.begin();
}
void loop() {
server.handleClient();
delay(2000);
}
After uploading, open the Serial Monitor to find the ESP32’s IP address, then type that IP into a browser to see live data. For remote access, you’d need port forwarding or a cloud service.
Common Problems and Fixes
- No readings or “Failed to read”: Check wiring, especially the data pin. Ensure the sensor is powered with 3.3V (some modules need 5V). Add a 4.7kΩ pull-up resistor between VCC and DATA.
- Incorrect temperature (too high): The DHT22 can self-heat if powered continuously; add a 2-second delay between reads and place the sensor away from the ESP32’s heat.
- Wi-Fi connection drops: Use a stable power source (USB wall adapter) and consider adding a capacitor (100µF) across the power pins to smooth voltage spikes.
- Humidity readings stuck at 0% or 100%: The sensor may be damaged or the data line is shorted. Try another DHT22 or check solder joints.
Comparing DHT22 to Other Sensors
While the DHT22 is popular, it’s not the only option. The DHT11 is cheaper but less accurate (±2°C, ±5% RH) and slower. The BME280 measures temperature, humidity, and barometric pressure over I2C/SPI, with higher accuracy and faster response, but it costs more. The SHT31 is another I2C option with excellent accuracy (±0.2°C, ±2% RH) and a built-in heater for condensation removal. For most hobby projects, the DHT22 offers the best balance of cost and performance.
Frequently Asked Questions
Can I use a DHT11 instead of a DHT22?
Yes, but you’ll get less accurate readings and a narrower temperature range. Change the DHTTYPE in the code to DHT11 and adjust the delay if needed (DHT11 can read every 1 second).
How do I power the ESP32 outdoors?
Use a 5V USB power bank or a solar panel with a battery and a 5V regulator. The ESP32 consumes about 80mA during Wi-Fi transmission, so a 2000mAh battery can last several hours.
Can I connect multiple DHT22 sensors to one ESP32?
Yes, each sensor needs its own GPIO pin. Define multiple DHT objects with different pins and read them sequentially. Note that each sensor must be at least 2 meters apart to avoid interference.
How do I store historical data?
You can log data to an SD card module or send it to a cloud service like ThingSpeak, which stores data in channels and provides graphs. For local storage, use the ESP32’s SPIFFS or LittleFS filesystem.
Is the DHT22 accurate enough for a weather station?
For outdoor use, the DHT22 is adequate for personal weather stations, but it’s not as accurate as professional sensors. It can drift over time and is sensitive to condensation. For critical applications, consider a BME280 or a weather-proof probe.
Why does my temperature read 0°C or -999?
This usually means the sensor is not communicating. Check the data pin and pull-up resistor. Also, ensure you’re using the correct GPIO number in the code (GPIO4 is an example).
Related
Formula used
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.