OC
OceanRemote
Low-code IoT platform
← Back to Course

DHT22 Temperature and Humidity Sensor

DHT22 Temperature and Humidity Sensor

🌑️ DHT22 Temperature and Humidity Sensor for ESP8266

🌑️ What You'll Learn:

  • πŸ”Œ Wire DHT22 to ESP8266 (3 wires + pull-up resistor)
  • πŸ“Š Read temperature (-40 to 80Β°C) and humidity (0-100%)
  • ⚠️ Get alerts for heat waves and high humidity
  • πŸ’» Complete code with error handling

πŸ”Œ Wiring Diagram

DHT22 Sensor              ESP8266 (NodeMCU)
══════════════            ════════════════
VCC (Pin 1)     ─────►    3.3V
GND (Pin 4)     ─────►    GND
DATA (Pin 2)    ─────►    GPIO4 (D2)

⚠️ CRITICAL: Add 10kΩ pull-up resistor between DATA and 3.3V!

    3.3V ──┬── 10kΞ© ──┬── DHT22 DATA
           β”‚          β”‚
           └──────────┴── ESP8266 GPIO4

DHT22 pins (front view, pins facing you):
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ (1) VCC     β”‚
    β”‚ (2) DATA    β”‚
    β”‚ (3) NC      β”‚
    β”‚ (4) GND     β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    
πŸ’‘ Library Installation:
  • Open Arduino IDE β†’ Sketch β†’ Include Library β†’ Manage Libraries
  • Search for "DHT sensor library" by Adafruit
  • Click Install
  • Also install "Adafruit Unified Sensor Library"

πŸ“– Complete Code

#include <DHT.h>

#define DHTPIN 4      // GPIO4 (D2 on NodeMCU)
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    dht.begin();
    Serial.println("🌑️ DHT22 Weather Monitor Started");
}

void loop() {
    delay(2000);  // DHT22 needs 2 seconds between readings!
    
    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    
    if (isnan(temp) || isnan(hum)) {
        Serial.println("❌ Sensor read failed! Check wiring.");
        return;
    }
    
    Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    Serial.printf("🌑️ Temperature: %.1f°C | %.1f°F\n", temp, temp * 9/5 + 32);
    Serial.printf("πŸ’§ Humidity: %.0f%%\n", hum);
    
    // Crop recommendations
    if (temp > 35) {
        Serial.println("⚠️ HEAT WARNING! Increase irrigation, open vents");
    } else if (temp > 30) {
        Serial.println("β˜€οΈ Warm - Monitor moisture levels");
    } else if (temp < 10 && temp > 0) {
        Serial.println("❄️ Cool - Delay planting warm-season crops");
    } else if (temp < 0) {
        Serial.println("🚨 FROST WARNING! Protect crops immediately!");
    }
    
    if (hum > 85) {
        Serial.println("⚠️ HIGH HUMIDITY! Risk of fungal disease");
        Serial.println("   β†’ Increase ventilation, reduce watering");
    } else if (hum < 30) {
        Serial.println("⚠️ LOW HUMIDITY! Plants stressed");
        Serial.println("   β†’ Misting recommended");
    }
    
    Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
}
    
⚠️ Troubleshooting:
  • ❌ "Sensor read failed": Add delay(2000) between readings (DHT22 needs 2 seconds!)
  • ❌ Constant 0% humidity: Missing 10kΞ© pull-up resistor
  • ❌ Temperature stuck at -40Β°C: Bad connection on DATA pin
  • ❌ ESP8266 resets: Add 100ΞΌF capacitor across VCC and GND
🎯 Quick Reference:
  • πŸ”Œ Pins: VCCβ†’3.3V, GNDβ†’GND, DATAβ†’GPIO4
  • πŸ”§ 10kΞ© pull-up resistor REQUIRED between DATA and 3.3V
  • ⏱️ Delay 2000ms between readings (critical!)
  • 🌑️ Temp >35Β°C = heat stress, <10Β°C = frost risk
  • πŸ’§ Humidity >85% = fungus risk
πŸ’‘ Key Takeaways:
  • Apply these concepts directly to your farm or project.
  • Take notes on important details for the quiz.
  • Use the button below to track your progress.