โ Back to Course
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.
×