- â Understanding the DHT22 sensor (features, accuracy, limitations)
- â Wiring DHT22 to ESP8266, ESP32, and Pico W
- â Installing the DHT library in Arduino IDE
- â Reading temperature and humidity data
- â Calibrating DHT22 readings with offset
- â Troubleshooting common DHT22 issues
Introduction to DHT22
The DHT22 (also known as AM2302) is a popular digital temperature and humidity sensor. It's known for its good accuracy, low cost, and ease of use with microcontrollers.
| Parameter | Value |
|---|---|
| Temperature Range | -40°C to +80°C (-40°F to +176°F) |
| Temperature Accuracy | ¹0.5°C (typical) |
| Humidity Range | 0% to 100% RH |
| Humidity Accuracy | Âą2% RH (typical) |
| Sampling Rate | Once every 2 seconds (max) |
| Supply Voltage | 3.3V to 5.5V |
| Current Draw | 1.5mA (measuring), 50ΞA (standby) |
| Interface | Digital (1-Wire-like protocol) |
| Price | $4-6 |
| Feature | DHT22 | DHT11 |
|---|---|---|
| Temperature Range | -40°C to +80°C | 0°C to +50°C |
| Temperature Accuracy | ¹0.5°C | ¹2°C |
| Humidity Range | 0-100% | 20-80% |
| Sampling Rate | Every 2 seconds | Every 1 second |
| Price | $4-6 | $2-3 |
DHT22 is better for most projects: wider range, better accuracy, and measures below freezing. DHT11 is cheaper but less accurate and can't measure below 0°C.
Pinout and Wiring
The DHT22 has 4 pins (some modules have 3 pins with built-in resistor):
| Pin | Name | Connection |
|---|---|---|
| Pin 1 | VCC | 3.3V or 5V power |
| Pin 2 | DATA | GPIO pin (with optional 4.7kÎĐ pull-up) |
| Pin 3 | NC | Not connected |
| Pin 4 | GND | Ground |
DHT22 Pinout: âââââââââââââââââââ â âââââââââââ â â â DHT22 â â â âââââââââââ â â â â âââââ âââââ â â â 1 â â 2 â â â Pin 1: VCC, Pin 2: DATA â âââââ âââââ â â âââââ âââââ â â â 3 â â 4 â â â Pin 3: NC, Pin 4: GND â âââââ âââââ â âââââââââââââââââââ
Wiring to ESP8266 (D1 Mini)
| DHT22 Pin | ESP8266 Pin | GPIO |
|---|---|---|
| VCC | 3.3V | - |
| DATA | D2 | GPIO4 |
| GND | GND | - |
ESP8266 D1 Mini + DHT22 Wiring: âââââââââââ ââââââââââââ â D1 Mini â â DHT22 â âââââââââââĪ ââââââââââââĪ â 3.3V ââââââââââââââââââââââĪ VCC (1) â â D2 ââââââââââââââââââââââĪ DATA (2) â â GND ââââââââââââââââââââââĪ GND (4) â âââââââââââ ââââââââââââ Optional: 4.7kÎĐ pull-up resistor between 3.3V and DATA
Wiring to ESP32
| DHT22 Pin | ESP32 Pin | GPIO |
|---|---|---|
| VCC | 3.3V | - |
| DATA | GPIO15 | 15 |
| GND | GND | - |
Wiring to Raspberry Pi Pico W
| DHT22 Pin | Pico W Pin | GPIO |
|---|---|---|
| VCC | 3.3V | - |
| DATA | GP15 | 15 |
| GND | GND | - |
Some DHT22 modules include a built-in 4.7kÎĐ pull-up resistor. If your readings are unstable, add an external 4.7kÎĐ resistor between DATA and VCC. OceanRemote firmware works with or without the resistor.
Installing the DHT Library
- Open Arduino IDE
- Go to Sketch â Include Library â Manage Libraries
- Search for "DHT sensor library"
- Find "DHT sensor library by Adafruit" and click Install
- It will ask to install dependencies. Click Install All (includes "Adafruit Unified Sensor")
If you followed Tutorial 08, you already have the DHT library installed. You can skip this step.
How OceanRemote Uses DHT22
When you select DHT22 in the device configuration, OceanRemote firmware automatically:
- Initializes the DHT sensor on the correct pin
- Reads temperature and humidity every 5 seconds (respects DHT22's 2-second minimum interval)
- Sends both values to the server with each poll
- Displays temperature and humidity on your dashboard
- Allows temperature calibration via offset (dashboard setting)
// How OceanRemote reads DHT22 (firmware snippet)
#include <DHT.h>
#define DHTPIN 4 // GPIO4 on ESP8266 (D2)
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void readSensor() {
static unsigned long lastDHTRead = 0;
unsigned long now = millis();
if (now - lastDHTRead >= 2000) {
lastDHTRead = now;
temperature = dht.readTemperature();
humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
temperature = -999;
humidity = -999;
}
}
}
Testing Your DHT22
Before flashing the full OceanRemote firmware, test your DHT22 with this simple sketch:
#include <DHT.h>
#define DHTPIN 4 // Change to your GPIO pin
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("DHT22 Test Started");
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("DHT22 read failed!");
} else {
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("°C, Humidity: ");
Serial.print(h);
Serial.println("%");
}
}
- If you see temperature/humidity values, your sensor is working!
- If you see "DHT22 read failed!", check your wiring and pull-up resistor
- If you see -999 or invalid readings, your sensor may be damaged
Calibrating DHT22 Readings
OceanRemote allows you to calibrate temperature readings using an offset. This is useful if your DHT22 consistently reads high or low.
How to Calibrate:
- Place a known-accurate thermometer next to your DHT22
- Note the difference between the two readings
- In your OceanRemote dashboard, find your device's temperature card
- Enter the offset value in the "Calibration Offset (¹°C)" field
- The formula is: Calibrated = Raw Reading + Offset
Example: Raw reading: 23.5°C Actual temperature: 22.8°C Difference: -0.7°C Enter offset: -0.7 Result: 23.5 + (-0.7) = 22.8°C â
- Offset persists even after device reboot
- Only affects displayed temperature (server-side), not sensor reading
- Can be changed anytime from the dashboard
- Range: -100°C to +100°C offset
Troubleshooting DHT22
Problem: No readings or "NaN" values
- Check wiring - DATA pin must be connected correctly
- Add a 4.7kÎĐ pull-up resistor between DATA and 3.3V
- Verify you selected the correct pin in configuration
- Try a different GPIO pin
Problem: Readings are 0 or -999
- Sensor may be damaged - try another DHT22
- Check power supply - DHT22 needs stable 3.3V or 5V
- Wiring may be loose - check connections
Problem: Temperature is inaccurate
- Use calibration offset in dashboard
- DHT22 has ¹0.5°C accuracy - some variation is normal
- Avoid placing sensor near heat sources (ESP8266 gets warm!)
- Keep sensor away from direct sunlight
Problem: Humidity readings are inaccurate
- DHT22 humidity accuracy is Âą2% - some variation is normal
- Sensor needs time to acclimate to environment (15-30 minutes)
- Avoid touching the sensor element with fingers (oils affect readings)
Many users connect DATA to A0 (analog pin). DHT22 is a digital sensor - it MUST be connected to a digital GPIO pin! On ESP8266, use D1-D8. On ESP32/Pico W, use any GPIO except input-only pins.
DHT22 Best Practices
- Mounting: Keep sensor away from the microcontroller (ESP8266/ESP32 generates heat)
- Ventilation: Ensure airflow around the sensor for accurate readings
- Cable Length: Keep wires under 20 meters for reliable communication
- Power: Use 3.3V or 5V consistently (don't switch between them)
- Sampling Rate: Don't read more than once every 2 seconds (OceanRemote respects this)
- Waterproof Version: Available for outdoor use (encapsulated in stainless steel probe)
You can connect multiple DHT22 sensors to the same board, but each needs its own GPIO pin. OceanRemote currently supports one sensor per device. For multiple sensors, create separate devices or modify the firmware.
Next Steps
Now that you understand the DHT22 sensor, continue with:
- Tutorial 17: DS18B20 Digital Temperature Sensor Guide
- Tutorial 18: NTC 10kÎĐ Thermistor Guide
- Tutorial 20: Calibrating Temperature Sensors with Offset
- Return to Tutorial 02: Generate firmware with DHT22 support!
Your DHT22 is now properly wired and tested. Generate new firmware with DHT22 selected, and your dashboard will show temperature and humidity readings!