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

Connecting a Rain Sensor

Connecting a Rain Sensor

๐Ÿ”Œ Connecting a Rain Sensor to ESP32

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

  • ๐Ÿ”Œ Wire a rain sensor (FC-37/YL-83) to ESP32/ESP8266
  • ๐Ÿ“Š Read both digital (rain yes/no) and analog (rain intensity) signals
  • ๐Ÿ’ง Automatically skip irrigation when rain is detected
  • ๐Ÿ’ฐ Save 20-40% water by not watering before storms

๐Ÿ› ๏ธ Components You Need

  • ๐Ÿ”น Rain Sensor Module (FC-37 or YL-83) โ€” $5-8 ยท Detects rain/water droplets
  • ๐Ÿ”น ESP32 or ESP8266 board โ€” $8-12 ยท Brain of the system
  • ๐Ÿ”น 3x Jumper wires (Female-to-Female) โ€” $2 ยท For connections
  • ๐Ÿ”น Optional: Waterproof enclosure โ€” $5 ยท Protects electronics outdoors
๐Ÿ’ก Why Add a Rain Sensor?

Soil moisture sensors alone can't predict rain. A $5 rain sensor prevents over-watering before storms, saving 20-40% more water (up to 500,000 liters/year on a 10-acre farm).

๐Ÿ”— Wiring Diagram

Rain Sensor FC-37 โ†’ ESP32
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
VCC (red)     โ†’ 3.3V (or 5V on ESP8266)
GND (black)   โ†’ GND
DO (digital)  โ†’ GPIO16 (any digital pin)
AO (analog)   โ†’ GPIO34 (ADC pin - ESP32 only)

โš ๏ธ ESP8266 users: AO not available (only one ADC pin A0)
   Use DO only, or connect AO to A0 on ESP8266
    
โš ๏ธ Sensor Logic Alert:

Different rain sensors use opposite logic. Most FC-37 modules output LOW (0) when rain is detected. Some output HIGH. Always test your specific sensor first!

๐Ÿ“– Basic Rain Detection Code (Digital Only)

#define RAIN_SENSOR_PIN 16

void setup() {
    Serial.begin(115200);
    pinMode(RAIN_SENSOR_PIN, INPUT_PULLUP);
    Serial.println("๐ŸŒง๏ธ Rain Sensor Ready");
    Serial.println("=================================");
}

void loop() {
    int rainDetected = digitalRead(RAIN_SENSOR_PIN);
    
    // Most sensors output LOW when rain is detected
    // If yours outputs HIGH, swap LOW/HIGH in the if statement
    if (rainDetected == LOW) {
        Serial.println("๐ŸŒง๏ธ RAIN DETECTED! Skip irrigation.");
        // Add your irrigation skip logic here
        // digitalWrite(RELAY_PIN, HIGH); // Keep pump OFF
    } else {
        Serial.println("โ˜€๏ธ No rain. Check soil moisture normally.");
        // Normal irrigation logic here
    }
    
    delay(60000); // Check every minute
}
    

๐Ÿ“– Advanced Code (Digital + Analog - Rain Intensity)

#define RAIN_DIGITAL_PIN 16
#define RAIN_ANALOG_PIN 34

void setup() {
    Serial.begin(115200);
    pinMode(RAIN_DIGITAL_PIN, INPUT_PULLUP);
    Serial.println("๐ŸŒง๏ธ Advanced Rain Sensor Ready");
}

void loop() {
    int rainDigital = digitalRead(RAIN_DIGITAL_PIN);
    int rainAnalog = analogRead(RAIN_ANALOG_PIN);
    
    // Convert analog reading to intensity percentage
    // Dry: 4095, Wet: 0 (adjust based on your sensor)
    int intensity = map(rainAnalog, 0, 4095, 100, 0);
    intensity = constrain(intensity, 0, 100);
    
    Serial.println("=================================");
    Serial.printf("๐ŸŒง๏ธ Rain detected: %s\n", rainDigital == LOW ? "YES" : "NO");
    Serial.printf("๐Ÿ’ง Rain intensity: %d%%\n", intensity);
    
    if (rainDigital == LOW) {
        if (intensity > 70) {
            Serial.println("โš ๏ธ HEAVY RAIN! Skip irrigation for 24 hours.");
        } else if (intensity > 30) {
            Serial.println("๐ŸŒง๏ธ Light rain. Skip irrigation for 6 hours.");
        } else {
            Serial.println("๐Ÿ’ง Light drizzle. Check soil before deciding.");
        }
    } else {
        Serial.println("โ˜€๏ธ No rain. Proceed with normal irrigation.");
    }
    
    delay(60000);
}
    
๐Ÿ’ก Understanding Rain Sensor Output:
  • Digital (DO): Trigger threshold adjustable via potentiometer on module
  • Analog (AO): Gives continuous reading (0 = wet, 4095 = dry on ESP32)
  • Intensity mapping: 0-30% = light rain, 30-70% = moderate, 70-100% = heavy
  • Adjust threshold: Turn the blue potentiometer with a small screwdriver
๐ŸŒŸ Integration with Irrigation System:
// Add to your irrigation code
if (rainDetected == LOW) {
    Serial.println("๐ŸŒง๏ธ Rain detected - skipping irrigation");
    digitalWrite(RELAY_PIN, HIGH);  // Keep pump OFF
    delay(3600000);  // Wait 1 hour before checking again
    return;  // Exit loop early
}
// Normal irrigation logic continues here...
        
๐Ÿ“– Case Study โ€” Rain Sensor Saves $600/Year, Zambia:

A 15-acre vegetable farm installed rain sensors on their automated irrigation system:

  • ๐ŸŒง๏ธ Rainy season savings: Skipped 18 irrigation cycles (2 weeks of watering)
  • ๐Ÿ’ฐ Water savings: 360,000 liters saved during rainy months
  • โšก Pump electricity saved: $180/year
  • ๐Ÿ’ง Prevented over-watering: No runoff or nutrient leaching
  • ๐ŸŒฑ Healthier plants: Reduced fungal diseases from over-watering

"The $6 rain sensor paid for itself in the first month! I used to water every Tuesday regardless of weather. Now the system knows when it's raining." โ€” Farmer, Lusaka Province

โš ๏ธ Common Problems & Solutions:
  • False triggers from morning dew: Add a 30-60 minute delay before acting on rain detection
  • Sensor corrosion: The exposed copper traces corrode over time. Use a capacitive rain sensor or replace yearly
  • Digital output always LOW/HIGH: Adjust the potentiometer or swap the logic in your code
  • ESP8266 analog limitation: Only A0 pin supports analog. Use DO only or connect AO to A0
  • Outdoor durability: Mount sensor at a slight angle for water runoff, clean monthly
๐Ÿ’ก Pro Tips for Reliable Rain Detection:
  • ๐Ÿ“ Mount sensor in an open area away from trees/roof overhangs
  • ๐Ÿ“ Angle slightly (15ยฐ) so water drains off, not pools
  • ๐Ÿงน Clean monthly with soft brush to remove dust/bird droppings
  • ๐Ÿ”‹ Add a 1-hour delay before resuming irrigation after rain stops
  • ๐ŸŒง๏ธ Combine with weather forecast API for even better predictions
๐ŸŽฏ Key Takeaways:
  • โœ… Rain sensors cost only $5-8 and save 20-40% water during rainy seasons
  • โœ… Digital output (DO) tells you YES/NO rain; Analog (AO) tells you rain intensity
  • โœ… Most sensors output LOW when rain is detected โ€” test yours first!
  • โœ… Always add a delay after rain stops (30-60 min) to prevent false triggers from dew
  • โœ… Mount sensor at an angle outdoors for proper drainage
  • โœ… A single rain sensor typically pays for itself in 1-2 months
๐Ÿ’ก 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.