DHT22 Temperature & Humidity Sensor
What is the DHT22 Temperature & Humidity Sensor?
The DHT22 is a digital temperature and humidity sensor that’s a step up from the common DHT11. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and outputs a calibrated digital signal via a single-wire bus. You’ll get readings with better accuracy and a wider range than the DHT11, making it perfect for weather stations, greenhouse monitoring, or any project where you want reliable environmental data.
It comes as a small module with four pins, often with a built-in pull-up resistor, so you can interface it directly with a microcontroller’s digital input pin. Just note that it needs a bit of timing precision in your code — the communication protocol is custom and slightly timing-sensitive, but there are libraries available for most platforms to handle that for you.
Key Specifications
| Specification | Value |
|---|---|
| Supply Voltage | 3.3 – 6.0 V |
| Output Signal | Digital single-bus (data pin) |
| Operating Temperature Range | -40 °C to +80 °C |
| Humidity Range | 0 – 100% RH |
| Temperature Accuracy | ±0.5 °C |
| Humidity Accuracy | ±2% RH (typical) |
| Resolution | 0.1 °C / 0.1% RH |
| Sampling Rate | 0.5 Hz (once every 2 seconds) |
| Interface / Protocol | Single-wire serial (custom timing) |
| Package Type | Module (4-pin, through-hole / male header) |
| Maximum Current (during conversion) | 1.5 mA |
Pinout
| Pin | Name | Type | Description |
|---|---|---|---|
| 1 | VCC | PWR | Power supply input — connect to 3.3V or 5V. |
| 2 | DATA | I/O | Single-bus data line for communication with the microcontroller. |
| 3 | NC | NC | No connection on most modules; may be used for optional external pull-up. |
| 4 | GND | GND | Ground connection — connect to common ground with the microcontroller. |
Note: Pin numbering and labeling can vary between module manufacturers and revisions. Always double-check against the datasheet for your specific batch.
Common Use Cases
- Indoor weather stations and temperature/humidity monitors for your workshop or home.
- Greenhouse or plant terrarium automation — trigger fans or misters based on readings.
- Server room or electronics enclosure environmental logging.
- Smart HVAC control prototypes (e.g., turn on a heater when temperature drops).
- Data logging experiments for science fair or IoT projects.
Wiring Example
For the most common setup with an Arduino Uno, connect pin 1 (VCC) to the 5V rail, and pin 4 (GND) to ground. Connect pin 2 (DATA) to any digital I/O pin, for example Arduino pin D2. If your module doesn’t have an onboard pull-up resistor, add a 4.7 kΩ resistor between the DATA pin and VCC. That’s it — just power and one data wire, and you’re ready to read sensor values with a DHT22 library.
Tips and Gotchas
- Wait two seconds between readings. The DHT22’s maximum sampling rate is 0.5 Hz — if you try to read it too fast, the sensor will ignore you or return stale data.
- Mind the pull-up resistor. Some modules include one, others don’t. If your data line doesn’t have a 4.7 kΩ to 10 kΩ pull-up to VCC, communication will fail intermittently.
- Keep the sensor dry during soldering. The humidity sensing element is exposed and sensitive to heat and moisture — soldering the pins or cleaning with excessive water can damage it.
- Don’t rely on it for sub-second readings. The DHT22 is great for slow environmental logging, but it’s not suitable for real-time applications like wind speed measurement or fast HVAC control loops.
Now go wire it up and start measuring your world — happy making!
Tutorial
With the DHT22 sensor and a microcontroller, you can build temperature and humidity monitors, data loggers, weather stations, and HVAC control systems.
Wiring
| Component Pin | Board Pin | Notes |
|---|---|---|
| VCC | 5V | Power |
| GND | GND | |
| DATA | D2 | Data line |
Required Libraries
- DHT sensor library — Tools > Manage Libraries > search DHT sensor library
Steps
- Wire the DHT22 as shown above. Connect VCC to 5V, GND to GND, and DATA to digital pin 2.
- Install the DHT sensor library by Adafruit via Library Manager.
- Upload the sketch.
Code
// Complete working sketch for DHT22 on Arduino Uno
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHT22 test!"));
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C"));
}
PlatformIO config platformio.ini
[env:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps =
adafruit/DHT sensor libraryA 10kΩ pull-up resistor between DATA and VCC is recommended for reliable communication.
Wiring
| Component Pin | Board Pin | Notes |
|---|---|---|
| VCC | 3.3V | Power (works at 3.3V) |
| GND | GND | |
| DATA | GPIO4 | Data line |
Required Libraries
- DHT sensor library — Tools > Manage Libraries > search DHT sensor library
Steps
- Wire the DHT22 as shown above. Connect VCC to 3.3V, GND to GND, DATA to GPIO4.
- Install the DHT sensor library by Adafruit via Library Manager.
- Upload the sketch.
Code
// Complete working sketch for DHT22 on ESP32
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println(F("DHT22 test on ESP32!"));
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C"));
}
PlatformIO config platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/DHT sensor libraryDHT22 can be powered directly from 3.3V on ESP32 — no level shifter needed. Use GPIO numbers as shown.
// Mentioned in these projects
Join the Community on Discord
Ask questions, share your builds, and hang out with fellow makers.