The DHT22 (also known as AM2302) is a digital sensor that measures temperature and humidity accurately. It’s commonly used in IoT projects, weather stations, and environmental monitoring systems.
Specifications
- Temperature range: -40°C to +80°C (±0.5°C accuracy)
- Humidity range: 0% to 100% RH (±2–5% accuracy)
- Operating voltage: 3.3V – 6V
- Output: Digital (single-wire communication)
- Sampling rate: 0.5 Hz (one reading every 2 seconds)
Pin Configuration
Pin | Function |
1 | VCC (3.3V or 5V) |
2 | Data (to microcontroller) |
3 | NC (Not Connected) |
4 | GND |
(Sometimes sensors come with only 3 pins: VCC, Data, GND)
Wiring with NodeMCU
- DHT22 → NodeMCU
- VCC → 3.3V
- GND → GND
- DATA → D4 (GPIO2) (with a 10k pull-up resistor to VCC)
Code for NodeMCU
First, install the DHT sensor library from Arduino Library Manager:
- Adafruit Unified Sensor
- DHT sensor library by Adafruit
#include "DHT.h"
// Define DHT type and pin
#define DHTPIN D4 // Data pin connected to D4
#define DHTTYPE DHT22 // Sensor type DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("DHT22 Sensor Reading");
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Celsius
// Check if any readings failed
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000); // Update every 2 seconds
}
Applications
- Smart Agriculture
- Weather Stations
- IoT-based Home Automation
- Greenhouse Monitoring





















