ESP32Medium
DHT22 Temperature Monitor
Connect a DHT22 sensor to the ESP32 and send live temperature and humidity readings over the Serial port.
Target AI Prompt
"Read temperature and humidity from DHT22 on GPIO 4 every 2 seconds, printing values over Serial."
Hardware Connections
DHT22 VCCSensor power input (Strictly 3.3V)
3.3VDHT22 DataConnect with a 10kΩ pullup resistor to VCC
GPIO 4DHT22 GNDGround pin connection
GNDLogic Explanation
Uses the Adafruit DHT sensor library. It configures GPIO 4 as an input. Due to ESP32 constraint requirements, it queries the sensor only once every 2000ms to allow sensor internal state stabilization.
Verified AuthorEmbedino Hardware FleetVerified expert compilation & hardware validation.
main.cpp
#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("% Temperature: ");
Serial.print(t);
Serial.println("C");
}Compile & Flash Online
Connect your ESP32 and flash this exact example directly inside our web console.
Launch App Workspace →