DHT22 Temperature & Humidity Sensor
Read live temperature and humidity data with a DHT22 sensor and print it straight to the Serial Monitor.
The Hardware Setup
The DHT22 has three pins that matter: power, ground, and a single data line. Most breakout boards already include the required pull-up resistor built in — if you're using a bare DHT22 sensor (not a breakout module), add a 10kΩ resistor between the data pin and 3.3V.
The Arduino Source Code
Install Adafruit's DHT sensor library via the Library Manager (it will prompt you to also install Adafruit Unified Sensor — accept that too).
// ESP32-S3 DHT22 Temperature & Humidity Tutorial - CircuitHub.pro
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println("Failed to read from DHT sensor!");
delay(2000);
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(tempC);
Serial.println("°C");
delay(2000); // DHT22 can only be read reliably about once every 2 seconds
}
Required Components
A classic beginner sensor project:
🚀 Take It Further
Pair this with the Temperature Converter calculator on the Calculators page — feed your live Celsius reading into that same C + 273.15 formula to display Kelvin, or push readings to the OLED from the Hello World tutorial for a standalone weather station.