DHT22 Temperature & Humidity Sensor
The DHT22 (AM2302) is a popular digital temperature and humidity sensor with good accuracy and a single-wire digital interface.
Key idea:
The DHT22 requires a 4.7kΩ–10kΩ pull-up resistor on its data line. Use the Adafruit DHT library for reliable readings.
Wiring
DHT22 Pin 1 (VCC) ────── 5V
DHT22 Pin 2 (DATA) ────── Arduino Pin 2 (with 4.7kΩ pull-up to 5V)
DHT22 Pin 3 (NC) ──────── Not connected
DHT22 Pin 4 (GND) ────── GND
Using the Adafruit DHT Library
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("DHT22 test!");
dht.begin();
}
void loop() {
// Wait 2 seconds between readings
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Celsius
float temperatureF = dht.readTemperature(true); // Fahrenheit
// Check if any reads failed
if (isnan(humidity) || isnan(temperature) || isnan(temperatureF)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index
float heatIndexC = dht.computeHeatIndex(temperature, humidity, false);
float heatIndexF = dht.computeHeatIndex(temperatureF, humidity, true);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.print("°C ");
Serial.print(temperatureF);
Serial.print("°F Heat index: ");
Serial.print(heatIndexC);
Serial.print("°C ");
Serial.print(heatIndexF);
Serial.println("°F");
}Key Specifications
| Parameter | Value |
|---|---|
| Temperature range | -40°C to 80°C |
| Temperature accuracy | ±0.5°C |
| Humidity range | 0–100% RH |
| Humidity accuracy | ±2% RH |
| Sampling rate | 0.5 Hz (one reading every 2 seconds) |
| Power | 3.3–5V |
Key Takeaways
- DHT22 measures temperature (-40°C to 80°C) and humidity (0–100% RH)
- Requires 4.7kΩ pull-up resistor on data line
- Use Adafruit DHT library for reliable communication
- Read temperature with
dht.readTemperature() - Read humidity with
dht.readHumidity() - Minimum 2 seconds between readings
Resources
- Adafruit DHT Sensor Library: https://github.com/adafruit/Adafruit_DHT_sensor_library
- Video: DHT22 Arduino Tutorial - https://www.youtube.com/watch?v=98Fz2x2P4cU
- Adafruit DHT22 Product Page: https://www.adafruit.com/product/385