BeginnerSensors
ESP32 Room Temperature and Humidity Monitor
Read a DHT22 and print temperature and humidity to the Arduino serial monitor.
Parts from the database
- ESP32-WROOM-32 ร 1Use on an ESP32 development board
- DHT22 ร 1Temperature and humidity sensor
Other supplies
- ESP32 development board containing an ESP32-WROOM-32Common build supply
- DHT22 breakout (or bare sensor with a 4.7โ10 kฮฉ DATA pull-up)Common build supply
- USB data cableCommon build supply
- jumper wiresCommon build supply
Amazon links may be affiliate links. As an Amazon Associate, Electronics Hobby Hub earns from qualifying purchases.
Connections
- DHT22 VCC to 3V3, GND to GND, DATA to ESP32 GPIO 4. Use a 3.3 V-compatible breakout.
- For a bare four-pin sensor, identify pins from its datasheet and add a 4.7โ10 kฮฉ resistor from DATA to 3V3.
- Do not power the sensor from 5 V if its DATA line would then pull the ESP32 input above 3.3 V.
Build steps
- Install Arduino IDE support for your ESP32 board and select the matching board/port.
- Install Adafruit DHT sensor library and Adafruit Unified Sensor from Library Manager.
- Wire the sensor with power disconnected, then upload the included sketch.
- Open Serial Monitor at 115200 baud and let the sensor settle; compare readings after breathing near it and then waiting for the air to clear.
Software and firmware setup
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() { Serial.begin(115200); dht.begin(); }
void loop() {
float h = dht.readHumidity();
float c = dht.readTemperature();
if (isnan(h) || isnan(c)) Serial.println("Read failed: check DATA wiring");
else { Serial.printf("Temperature: %.1f C Humidity: %.1f %%\n", c, h); }
delay(2000);
}Test your build
You should see a new temperature and relative-humidity reading every two seconds. DHT22 updates are slow; do not poll it rapidly.
Troubleshooting
- If readings fail, confirm GPIO 4 matches the code and DATA has a pull-up.
- If values are implausible, check sensor pin order and avoid touching the sensing slots.
- If USB upload fails, select the actual board and serial port and use a data-capable cable.
Safety and limitations
Power from USB or a regulated 3.3 V-compatible development board. The sensor is for hobby room monitoring, not medical or safety-critical decisions.
Manufacturer and platform documentation
Community Notes for This Project
These are unverified visitor contributions, separate from the build guide and manufacturer specifications. Verify technical advice against the linked datasheets before relying on it.
Set up the comments service to load community notes.