DHT11 & DHT22 Sensors

Complete guide to temperature and humidity sensing with DHT series sensors

DHT11 and DHT22 Sensors

DHT Series Overview

DHT sensors are basic, low-cost digital temperature and humidity sensors that use a capacitive humidity sensor and a thermistor to measure surrounding conditions.

  • Digital Output: Single-wire serial interface
  • Low Power: 3-5V DC power supply
  • Integrated Sensing: Measures both temperature and humidity
  • Pre-calibrated: Factory calibrated for ease of use
  • Long-term Stability: Reliable over extended periods
  • Cost Effective: Very affordable compared to alternatives
  • Easy to Use: Simple digital interface
  • Good for Prototyping: Quick integration into projects
  • Wide Availability: Commonly stocked by electronics suppliers
  • Arduino Compatible: Well-supported by libraries
  • Weather Stations: Home environmental monitoring
  • HVAC Systems: Climate control feedback
  • Agricultural Monitoring: Greenhouse conditions
  • Home Automation: Smart thermostats
  • Industrial Applications: Equipment monitoring

DHT11 vs DHT22 Comparison

Parameter DHT11 DHT22 (AM2302)
Temperature Range 0°C to 50°C -40°C to 80°C
Temperature Accuracy ±2°C ±0.5°C
Humidity Range 20% to 80% RH 0% to 100% RH
Humidity Accuracy ±5% RH ±2% RH
Sampling Rate 1Hz (1 reading per second) 0.5Hz (1 reading every 2 seconds)
Power Supply 3-5.5V DC 3-6V DC
Current Consumption 0.5mA (measuring), 60μA (standby) 1.5mA (measuring), 40μA (standby)
Price $1-$3 $4-$10

When to Choose Which Sensor

Choose DHT11 When:

  • Basic temperature/humidity monitoring is sufficient
  • Project budget is very tight
  • Measuring within normal room conditions (not extreme environments)
  • Higher sampling rate is needed

Choose DHT22 When:

  • Higher accuracy is required
  • Measuring in extreme temperatures (below 0°C or above 50°C)
  • Measuring very low or very high humidity levels
  • Project can accommodate the higher cost

Pinout and Wiring

DHT11 Pinout

DHT11 Pinout

  1. VCC (3-5V): Power supply
  2. DATA: Serial data output
  3. NC: Not connected
  4. GND: Ground
DHT22 Pinout

DHT22 Pinout

  1. VCC (3-6V): Power supply
  2. DATA: Serial data output
  3. NC: Not connected
  4. GND: Ground

Typical Wiring Diagram

DHT Wiring Diagram

Important Wiring Notes

DHT Sensor Libraries

Popular Libraries

DHT Sensor Library

Standard library for Arduino by Adafruit

Arduino Simple API Supports DHT11/22
GitHub

Adafruit_CircuitPython_DHT

CircuitPython library for DHT sensors

CircuitPython Raspberry Pi Python API
GitHub

SimpleDHT

Alternative Arduino library

Arduino Minimalist No dependencies
GitHub

Adafruit DHT Library Functions

// Library initialization
DHT dht(DHTPIN, DHTTYPE); // Create DHT object

// In setup():
dht.begin(); // Initialize sensor

// Reading values:
float h = dht.readHumidity();      // Read humidity (%RH)
float t = dht.readTemperature();   // Read temp in Celsius
float f = dht.readTemperature(true); // Read temp in Fahrenheit
float hif = dht.computeHeatIndex(f, h); // Compute heat index

// Error checking:
if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
}

Library Installation

// Arduino IDE:
1. Sketch > Include Library > Manage Libraries
2. Search for "DHT sensor library"
3. Install "DHT sensor library by Adafruit"

// PlatformIO:
1. Open platformio.ini
2. Add dependency:
lib_deps = adafruit/DHT sensor library@^1.4.4

// Manual installation:
1. Download ZIP from GitHub
2. Sketch > Include Library > Add .ZIP Library
3. Select downloaded file

Code Examples

Basic Arduino Example

// Basic DHT11/DHT22 Example
#include <DHT.h>

#define DHTPIN 2      // Digital pin connected
#define DHTTYPE DHT22 // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(9600);
    Serial.println("DHTxx test!");
    dht.begin();
}

void loop() {
    delay(2000); // Wait between measurements
    
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    
    if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }
    
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print("%\tTemperature: ");
    Serial.print(t);
    Serial.println("°C");
}

ESP8266/ESP32 Example with WiFi

// ESP8266/ESP32 DHT Web Server
#include <WiFi.h>
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

DHT dht(DHTPIN, DHTTYPE);
WiFiServer server(80);

void setup() {
    Serial.begin(115200);
    dht.begin();
    
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    
    server.begin();
    Serial.println("Server started");
    Serial.println(WiFi.localIP());
}

void loop() {
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    
    WiFiClient client = server.available();
    if (client) {
        String html = "<html><body>";
        html += "<h1>ESP DHT Server</h1>";
        html += "<p>Temperature: " + String(t) + "°C</p>";
        html += "<p>Humidity: " + String(h) + "%</p>";
        html += "</body></html>";
        
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();
        client.println(html);
        client.stop();
    }
}

Raspberry Pi Python Example

# Raspberry Pi DHT22 Example
import Adafruit_DHT
import time

# Set sensor type and pin
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    
    if humidity is not None and temperature is not None:
        print(f"Temp={temperature:.1f}°C Humidity={humidity:.1f}%")
    else:
        print("Sensor failure. Check wiring.")
    
    time.sleep(3)

Controller Integration

Arduino Integration

  1. Install DHT library via Library Manager
  2. Connect sensor to 5V, GND, and a digital pin
  3. Add 4.7KΩ-10KΩ pull-up resistor between DATA and VCC
  4. Use the basic example code provided earlier
  5. Add 2-second delays between readings

ESP8266/ESP32 Integration

  1. Same wiring as Arduino (3.3V works despite 5V recommendation)
  2. For battery operation, use deep sleep between readings
  3. Consider using WiFiManager for easy WiFi configuration
  4. For MQTT projects, use PubSubClient to send data to broker

Raspberry Pi Integration

  1. Install required packages: sudo apt-get install python3-pip
  2. Install library: sudo pip3 install Adafruit_DHT
  3. Connect sensor to 3.3V (Pin 1), GND (Pin 6), and GPIO4 (Pin 7)
  4. Use the Python example provided earlier
  5. For headless operation, log data to file or database

Common Issues and Solutions

Issue Possible Cause Solution
Failed to read Loose connection, no pull-up resistor Check wiring, add 4.7KΩ resistor
NaN values Timing issues, power fluctuations Add delay between reads, use stable power
Incorrect values Sensor placed near heat source Relocate sensor, add ventilation
Sensor not responding Wrong pin specified, damaged sensor Double-check pin, try another sensor