← Back to Course
Rain Sensors - Detect Rainfall Automatically
🌧️ Rain Sensors - Automatic Rain Detection for Smart Irrigation
☔ What You'll Learn:
- 💧 Detect rainfall automatically to pause irrigation
- 📊 Measure rain intensity (light vs heavy rain)
- 💰 Save water by not irrigating during/after rain
- 🔌 Wire digital and analog outputs correctly
Rain sensors detect rainfall and can automatically pause irrigation - saving water, preventing over-watering, and protecting your crops from root rot. A $3 sensor can save thousands of liters of water annually!
🔌 Rain Sensor Wiring (FC-37 / YL-83)
═══════════════════════════════════════════════════════════════════════════════
RAIN SENSOR (FC-37) TO ESP32
═══════════════════════════════════════════════════════════════════════════════
Rain Sensor Module ESP32
┌─────────────────┐ ┌─────────┐
│ VCC (2-5V) │ ───────────► │ 3.3V │
│ GND │ ───────────► │ GND │
│ DO (Digital Out)│ ───────────► │ GPIO16 │ (LOW = rain, HIGH = dry)
│ AO (Analog Out) │ ───────────► │ GPIO32 │ (0-4095, lower = heavier rain)
└─────────────────┘ └─────────┘
Adjust potentiometer on module to set digital threshold!
- Turn clockwise = more sensitive (triggers with less water)
- Turn counter-clockwise = less sensitive
═══════════════════════════════════════════════════════════════════════════════
📖 Complete Rain Detection Code
#define RAIN_DIGITAL 16
#define RAIN_ANALOG 32
#define PUMP_RELAY 5
void setup() {
Serial.begin(115200);
pinMode(RAIN_DIGITAL, INPUT);
pinMode(RAIN_ANALOG, INPUT);
pinMode(PUMP_RELAY, OUTPUT);
digitalWrite(PUMP_RELAY, HIGH); // Pump OFF initially
Serial.println("☔ Rain Monitor Started");
}
void loop() {
int rainDigital = digitalRead(RAIN_DIGITAL);
int rainAnalog = analogRead(RAIN_ANALOG);
Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
if (rainDigital == LOW) { // Rain detected (active LOW)
Serial.println("🌧️ RAIN DETECTED!");
Serial.printf(" Analog value: %d (0-wet, 4095-dry)\n", rainAnalog);
// Determine rain intensity
if (rainAnalog < 500) {
Serial.println(" 💧 Heavy rain - Skip irrigation for 24 hours");
} else if (rainAnalog < 1500) {
Serial.println(" 💧 Moderate rain - Skip irrigation for 12 hours");
} else if (rainAnalog < 3000) {
Serial.println(" 💧 Light rain - Skip irrigation for 6 hours");
}
// Disable pump
digitalWrite(PUMP_RELAY, HIGH);
Serial.println(" 🚫 Pump DISABLED (rain detected)");
} else {
Serial.println("☀️ No rain detected");
Serial.printf(" Analog value: %d (dry)\n", rainAnalog);
// Normal irrigation logic here
// digitalWrite(PUMP_RELAY, LOW); // Enable pump if soil is dry
Serial.println(" ✅ Pump enabled based on soil moisture");
}
Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
delay(60000); // Check every minute
}
💡 Installation Tips:
- Location: Mount in open area - not under trees or building overhangs
- Angle: Install at slight angle (10-15°) to help water run off the sensor
- Height: Mount at 1-2 meters above ground to avoid splashing
- Cleaning: Clean sensor surface every few months - dirt reduces sensitivity
- Corrosion: Use gold-plated sensor for longer life in humid climates
⚠️ Common Issues & Solutions:
- Sensor always says "Raining": Adjust potentiometer (turn counter-clockwise)
- Sensor never detects rain: Turn potentiometer clockwise for more sensitivity
- False readings after rain: Sensor needs drying time - tilt it to help water run off
- Corrosion after months: Use AC voltage (not DC) or replace with gold-plated sensor
- Digital pin stays HIGH: Check pull-up resistor (enable INPUT_PULLUP)
📖 Case Study - Rain Sensor Saves 30% Water:
A vegetable farm in Uganda installed a rain sensor connected to their irrigation system:
- Problem: Automatic irrigation watered even during/after rain
- Solution: Rain sensor disables pump when rain detected
- Result: 30% water saved during rainy season
- Savings: $50/month + healthier crops (no over-watering)
"The rain sensor paid for itself in 2 months. Now my plants don't get over-watered when it rains." - Farmer, Uganda
🎉 Key Takeaways:
- ✅ Digital output (LOW = rain) for simple ON/OFF detection
- ✅ Analog output (0-4095) for rain intensity measurement
- ✅ Adjust potentiometer to set sensitivity threshold
- ✅ Always disable irrigation during/after rain to save water
- ✅ Clean sensor regularly and mount at slight angle
💡 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.
×