DHT22 Reference
A digital temperature and humidity sensor communicating over a single-wire protocol.
Pin names
| Name | Description |
|---|---|
| VCC | Power (3.3V–5V) |
| SDA | Data — single-wire digital output, connect to a GPIO |
| NC | Not connected |
| GND | Ground |
Attributes
This component has no configurable attributes.
Usage
Connect VCC to 5V, GND to GND, and SDA to a GPIO pin. On real hardware, add a 10 kΩ pull-up resistor between SDA and VCC. Read the sensor with the DHT sensor library.
#include <DHT.h>
#define DHT_PIN 2
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println("Failed to read from DHT22");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temp: ");
Serial.print(tempC);
Serial.println("C");
delay(2000); // DHT22 supports at most ~0.5 Hz sampling
}
Examples
| Description | Attrs |
|---|---|
| Default DHT22 | {} |
See also
- NTC Temperature Sensor — analog temperature-only alternative
- HC-SR04 — another common digital sensor for beginner projects