OC
OceanRemote
Low-code IoT platform
โ† Back to Course

Using DHT22 for Weather Monitoring

Using DHT22 for Weather Monitoring

๐Ÿ”Œ Connecting DHT22 for Weather Data

๐ŸŒก๏ธ What You'll Learn:

  • ๐Ÿ”Œ Wire DHT22 temperature and humidity sensor to ESP32/ESP8266
  • ๐Ÿ“Š Read accurate temperature (ยฑ0.5ยฐC) and humidity (ยฑ2-5%)
  • ๐ŸŒพ Use weather data for crop-specific irrigation decisions
  • ๐Ÿ’ง Save 15-25% more water by factoring in humidity and temperature

๐Ÿ› ๏ธ Components You Need

  • ๐Ÿ”น DHT22 Temperature & Humidity Sensor โ€” $8-12 ยท High accuracy (ยฑ0.5ยฐC, ยฑ2% RH)
  • ๐Ÿ”น ESP32 or ESP8266 board โ€” $8-12 ยท Any digital GPIO pin works
  • ๐Ÿ”น 10kฮฉ Resistor โ€” $1 ยท Pull-up resistor (some modules include it)
  • ๐Ÿ”น 4x Jumper wires โ€” $2 ยท Female-to-female or male-to-female
  • ๐Ÿ”น Optional: Weather shield โ€” $10 ยท Protects from sun/rain outdoors
๐Ÿ’ก DHT22 vs DHT11 Comparison:
  • DHT22 (Recommended): ยฑ0.5ยฐC accuracy, ยฑ2% humidity, 0.5Hz reading (every 2 seconds)
  • DHT11 (Cheaper): ยฑ2ยฐC accuracy, ยฑ5% humidity, 1Hz reading (every second)
  • Our choice: DHT22 for farming โ€” better accuracy for crop decisions

๐Ÿ”— Wiring Diagram

DHT22 Pinout (front view, pins down):
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ (1) VCC     โ”‚ โ†’ 3.3V (or 5V)
โ”‚ (2) DATA    โ”‚ โ†’ GPIO15 + 10kฮฉ resistor to 3.3V
โ”‚ (3) NC      โ”‚ โ†’ Not connected
โ”‚ (4) GND     โ”‚ โ†’ GND
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

ESP32 Connection:
DHT22 VCC  โ†’ ESP32 3.3V
DHT22 DATA โ†’ ESP32 GPIO15 + 10kฮฉ pull-up to 3.3V
DHT22 GND  โ†’ ESP32 GND

โš ๏ธ Some modules have built-in pull-up resistors (3 pins only)
    Check your module โ€” you may not need the external resistor!
    
โš ๏ธ CRITICAL: Pull-Up Resistor Required!

DHT22 needs a 10kฮฉ pull-up resistor between DATA and VCC. Without it, readings will fail or be incorrect. Some pre-built modules include this resistor (3-pin version). For 4-pin bare sensor, you MUST add it!

๐Ÿ“– Basic DHT22 Code

#include "DHT.h"

#define DHTPIN 15
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    dht.begin();
    Serial.println("๐ŸŒก๏ธ DHT22 Weather Sensor Ready");
    Serial.println("=================================");
}

void loop() {
    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    
    // Check for read errors
    if (isnan(temp) || isnan(hum)) {
        Serial.println("โŒ Sensor read failed! Check wiring.");
    } else {
        Serial.printf("๐ŸŒก๏ธ Temperature: %.1fยฐC\n", temp);
        Serial.printf("๐Ÿ’ง Humidity: %.1f%%\n", hum);
        
        // Crop-specific recommendations
        if (temp > 35) {
            Serial.println("โš ๏ธ HEAT WARNING! Increase irrigation by 30-50%");
        } else if (temp < 15) {
            Serial.println("โš ๏ธ COLD WARNING! Reduce irrigation, risk of root rot");
        }
        
        if (hum < 30) {
            Serial.println("โš ๏ธ LOW HUMIDITY! Plants stressed, water immediately");
        } else if (hum > 80) {
            Serial.println("โš ๏ธ HIGH HUMIDITY! Watch for fungal diseases");
        }
    }
    
    delay(60000); // Read every minute
}
    

๐Ÿ“– Advanced Code with Heat Index (Feels Like Temperature)

#include "DHT.h"

#define DHTPIN 15
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
float lastValidTemp = 25.0;
float lastValidHum = 50.0;

float calculateHeatIndex(float temp, float humidity) {
    // Simplified heat index formula for < 40ยฐC
    float hi = temp;
    if (temp >= 27) {
        hi = -8.784695 + 1.61139411 * temp + 2.338549 * humidity;
        hi += -0.14611605 * temp * humidity;
        hi += -0.01230809 * temp * temp;
        hi += -0.01642482 * humidity * humidity;
        hi += 0.00221173 * temp * temp * humidity;
        hi += 0.00072546 * temp * humidity * humidity;
        hi += -0.00000358 * temp * temp * humidity * humidity;
    }
    return hi;
}

void setup() {
    Serial.begin(115200);
    dht.begin();
    Serial.println("๐ŸŒก๏ธ Advanced DHT22 Weather Monitor");
    Serial.println("=================================");
}

void loop() {
    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    
    // Use last valid values if read fails
    if (isnan(temp)) temp = lastValidTemp;
    else lastValidTemp = temp;
    
    if (isnan(hum)) hum = lastValidHum;
    else lastValidHum = hum;
    
    float heatIndex = calculateHeatIndex(temp, hum);
    
    Serial.printf("๐ŸŒก๏ธ Actual Temp: %.1fยฐC\n", temp);
    Serial.printf("๐Ÿ”ฅ Feels Like: %.1fยฐC\n", heatIndex);
    Serial.printf("๐Ÿ’ง Humidity: %.1f%%\n", hum);
    
    // Irrigation decision logic
    int waterIncrease = 0;
    
    if (heatIndex > 35) waterIncrease += 40;
    else if (temp > 35) waterIncrease += 30;
    else if (temp > 30) waterIncrease += 15;
    
    if (hum < 30) waterIncrease += 25;
    else if (hum < 45) waterIncrease += 10;
    
    if (waterIncrease > 0) {
        Serial.printf("๐Ÿ’ง Increase watering by %d%% (heat + low humidity)\n", waterIncrease);
    } else if (hum > 80 || temp < 15) {
        Serial.println("๐Ÿ’ง Reduce watering by 30% (high humidity or cold)");
    } else {
        Serial.println("โœ… Conditions optimal. Normal irrigation.");
    }
    
    delay(120000); // Read every 2 minutes
}
    
๐Ÿ’ก Understanding Crop-Specific Thresholds:
  • ๐Ÿ… Tomatoes: Optimal 20-28ยฐC, humidity 50-70% | Stop growing above 35ยฐC
  • ๐ŸŒฝ Maize/Corn: Optimal 18-32ยฐC, humidity 40-60% | Heat stress above 35ยฐC
  • ๐Ÿฅฌ Leafy greens: Optimal 15-22ยฐC, humidity 60-80% | Bolt (flower) above 28ยฐC
  • ๐ŸŒถ๏ธ Peppers: Optimal 20-30ยฐC, humidity 50-70% | Thrives in heat
  • ๐Ÿฅ” Potatoes: Optimal 15-25ยฐC, humidity 60-80% | High humidity = blight risk
๐Ÿ“– Case Study โ€” DHT22 Saves 25% Water, Uganda:

A 8-acre tomato farm added DHT22 sensors to their irrigation controller:

  • ๐ŸŒก๏ธ Heat wave detection: Automatically increased watering by 40% during 38ยฐC days
  • ๐Ÿ’ง Water savings: 25% reduction on normal days (didn't over-water when cool)
  • ๐Ÿ’ฐ Cost savings: $240/year on water + $180/year on pump electricity
  • ๐Ÿ“ˆ Yield increase: 18% better production (reduced heat stress)
  • ๐Ÿ›ก๏ธ Disease prevention: Reduced watering when humidity >80% (blight prevention)

"The DHT22 tells me when it's too hot or too humid. My tomatoes don't get stressed anymore, and I'm using less water!" โ€” Farmer, Wakiso District

๐ŸŒŸ Integration with OceanRemote Irrigation System:
// Add weather logic to your irrigation code
float temp = dht.readTemperature();
float hum = dht.readHumidity();

if (temp > 35) {
    // Heat wave - increase watering
    irrigationDuration = baseDuration * 1.5;
} else if (hum < 30) {
    // Low humidity - plants stressed
    irrigationDuration = baseDuration * 1.25;
} else if (hum > 80 || temp < 15) {
    // Cool/humid - reduce watering
    irrigationDuration = baseDuration * 0.7;
} else {
    irrigationDuration = baseDuration;
}
        
โš ๏ธ Common DHT22 Problems & Solutions:
  • Readings return NaN: Check wiring, add 10k pull-up resistor, or increase delay between reads (minimum 2 seconds)
  • Inaccurate temperature: Sensor self-heats if powered continuously. Read every 2-60 seconds only
  • Humidity stuck at 100%: Sensor may be contaminated. Clean gently with isopropyl alcohol
  • Slow readings: DHT22 takes 2-3 seconds per read โ€” normal behavior!
  • Outdoor use: Sensor is not waterproof. Use a weather shield or mount under cover
  • ESP8266 note: Works fine, but avoid using GPIO16 (D0) which has special functions
๐Ÿ’ก Pro Tips for Accurate Readings:
  • ๐Ÿ“ Mount sensor at crop canopy height (not ground level) for accurate microclimate
  • ๐ŸŒณ Place in open area away from walls, concrete, or direct afternoon sun
  • ๐Ÿ’จ Ensure good airflow around sensor for humidity accuracy
  • โฑ๏ธ Read every 5-15 minutes for irrigation โ€” no need for second-by-second data
  • ๐Ÿ“Š Average 3-5 readings to reduce noise: temp = (t1+t2+t3)/3;
๐ŸŽฏ Key Takeaways:
  • โœ… DHT22 provides accurate temperature (ยฑ0.5ยฐC) and humidity (ยฑ2-5%) for $8-12
  • โœ… A 10kฮฉ pull-up resistor between DATA and VCC is REQUIRED for reliable readings
  • โœ… High temperature (>35ยฐC) = increase irrigation by 30-50%
  • โœ… Low humidity (<30%) = increase irrigation by 20-30% (plant stress)
  • โœ… High humidity (>80%) + cool temps = reduce watering (fungus risk)
  • โœ… DHT22 pays for itself in 1-3 months through water and electricity savings
  • โœ… Use last valid values in code to handle occasional read failures
๐Ÿ’ก 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.