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

Irrigation Basics - Part 2

Irrigation Basics - Part 2

๐Ÿ› ๏ธ Irrigation Basics - Part 2: Practical Implementation

๐Ÿ’ง What You'll Learn:

  • โš™๏ธ Build an automatic irrigation system that waters only when needed
  • ๐Ÿ”Œ Wire relay modules safely to control water pumps
  • ๐Ÿ“Š Set custom soil moisture thresholds for different crops
  • ๐Ÿ’ง Save 40-60% water compared to timer-based irrigation

๐Ÿ›’ Components You Need

  • ๐Ÿ”น ESP32 microcontroller โ€” $8 ยท Brain of the system
  • ๐Ÿ”น 1-Channel Relay Module โ€” $5 ยท Controls pump on/off
  • ๐Ÿ”น Soil Moisture Sensor (Capacitive) โ€” $8 ยท Measures soil water content
  • ๐Ÿ”น 12V Water Pump โ€” $10 ยท Pumps water to crops
  • ๐Ÿ”น Water tubing & drip fittings โ€” $5 ยท Distribute water to plants
  • ๐Ÿ”น 12V Power Supply โ€” $8 ยท Powers pump (2A minimum)
๐Ÿ’ก Total System Cost: ~$44

Pays for itself in 2-3 months through water savings! A typical 1-acre farm saves $120-180/year on water bills.

๐Ÿ”Œ Wiring Diagram

ESP32 โ†’ Relay Module
GPIO26 โ†’ IN pin (control signal)
3.3V   โ†’ VCC (relay power)
GND    โ†’ GND

Relay โ†’ Water Pump (12V circuit - HIGH VOLTAGE!)
COM    โ†’ Pump positive (+)
NO     โ†’ 12V power supply positive (+)
Pump negative โ†’ 12V power supply negative (-)

ESP32 โ†’ Soil Moisture Sensor (Capacitive)
GPIO34 (ADC) โ†’ AO (analog out)
3.3V         โ†’ VCC
GND          โ†’ GND
    
โš ๏ธ CRITICAL SAFETY WARNING:

Never connect the water pump directly to ESP32 pins! The pump draws 200-500mA (up to 2A startup), which will destroy your ESP32. ALWAYS use a relay module to isolate high-power circuits.

๐Ÿ“– Complete Irrigation Code (with Fail-Safe)

#include <WiFi.h>

#define RELAY_PIN 26
#define SOIL_PIN 34

// Moisture thresholds (adjust based on your soil type)
const int DRY_THRESHOLD = 30;       // Water when below 30%
const int WET_THRESHOLD = 70;       // Stop if already wet
const unsigned long PUMP_RUN_TIME = 300000;  // 5 minutes max
const unsigned long CHECK_INTERVAL = 3600000; // Check every hour
const unsigned long MIN_WATER_TIME = 60000;   // Minimum 1 minute run

unsigned long lastCheck = 0;
bool pumpRunning = false;
unsigned long pumpStartTime = 0;

void setup() {
    Serial.begin(115200);
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, HIGH);  // Relay HIGH = Pump OFF
    Serial.println("๐Ÿ’ง Smart Irrigation System Started");
    Serial.println("=================================");
}

int getSoilMoisturePercent() {
    int raw = analogRead(SOIL_PIN);
    // Dry: 4095 (air), Wet: 1500 (saturated)
    int percent = map(raw, 4095, 1500, 0, 100);
    percent = constrain(percent, 0, 100);
    return percent;
}

void startPump() {
    digitalWrite(RELAY_PIN, LOW);   // Relay LOW = Pump ON
    pumpRunning = true;
    pumpStartTime = millis();
    Serial.println("๐Ÿ’ง PUMP ON - Watering crops");
}

void stopPump() {
    digitalWrite(RELAY_PIN, HIGH);  // Relay HIGH = Pump OFF
    pumpRunning = false;
    Serial.println("๐Ÿ’ง PUMP OFF - Watering complete");
}

void loop() {
    unsigned long now = millis();
    
    // Auto-stop after max run time (fail-safe)
    if (pumpRunning && (now - pumpStartTime >= PUMP_RUN_TIME)) {
        Serial.println("โš ๏ธ Max run time reached - stopping pump (fail-safe)");
        stopPump();
    }
    
    // Check soil moisture every hour (only when pump is off)
    if (!pumpRunning && (now - lastCheck >= CHECK_INTERVAL)) {
        lastCheck = now;
        
        int moisture = getSoilMoisturePercent();
        Serial.printf("๐Ÿ“Š Soil moisture: %d%%\n", moisture);
        
        if (moisture < DRY_THRESHOLD) {
            Serial.println("โš ๏ธ Soil too dry! Starting irrigation...");
            startPump();
        } else if (moisture > WET_THRESHOLD) {
            Serial.println("โœ… Soil moisture is good. No watering needed.");
        } else {
            Serial.printf("๐ŸŒฟ Soil moisture optimal (%d%%). No action needed.\n", moisture);
        }
    }
}
    
๐Ÿ’ก Adjusting Moisture Thresholds by Crop:
  • ๐Ÿ… Tomatoes: Water at 25% dry, stop at 65% (optimal: 40-55%)
  • ๐ŸŒฝ Maize/Corn: Water at 20% dry, stop at 60% (optimal: 35-50%)
  • ๐Ÿฅฌ Leafy greens (kale, spinach): Water at 35% dry, stop at 75%
  • ๐Ÿฅ” Potatoes: Water at 30% dry, stop at 70% (critical during tuber formation)
  • ๐ŸŒต Succulents/Drought crops: Water at 15% dry, stop at 50%

๐Ÿ’ก Pro tip: Test your soil type first. Sandy soil needs 20% lower thresholds (drains faster). Clay soil needs 15% higher (holds water longer).

๐Ÿ“– Case Study โ€” Smart Irrigation Saves 55% Water, Kenya:

A 5-acre tomato farm replaced timer-based irrigation with soil moisture sensors:

  • ๐Ÿ’ง Water savings: 55% reduction (from 8,000L to 3,600L/day)
  • ๐Ÿ’ฐ Cost savings: $95/month on water bills
  • ๐Ÿ“ˆ Yield increase: 28% better tomato production (no over/under watering)
  • โšก Power: Solar-powered ESP32 runs 6+ months on deep sleep
  • ๐Ÿ“Š ROI: System paid for itself in 6 weeks!

"My tomatoes never looked better. I used to water every day whether needed or not. Now the system waters only when the soil is dry. I've cut my water bill in half!" โ€” Farmer, Kiambu County, Kenya

๐ŸŒŸ Advanced Features to Add (Next Lesson):
  • ๐Ÿ“ฑ Remote monitoring: Add WiFi to see moisture from your phone
  • ๐ŸŒง๏ธ Rain delay: Add a rain sensor to skip watering when raining
  • โฐ Time restrictions: Water only in morning/evening (prevents evaporation)
  • ๐Ÿ“Š Data logging: Track moisture trends over weeks/months
  • ๐Ÿ”‹ Deep sleep: Add deep sleep to run for months on battery
โš ๏ธ Common Mistakes to Avoid:
  • โŒ Using resistive soil sensors: They corrode in weeks! Use capacitive sensors (last years)
  • โŒ No fail-safe timer: Always add max run time in case sensor fails
  • โŒ Watering at noon: 50% of water evaporates! Water at sunrise or sunset
  • โŒ Wrong pump voltage: ESP32 is 3.3V logic, relay handles 12V/24V pump power
๐ŸŽฏ Key Takeaways:
  • โœ… Relay modules protect your ESP32 from high-power devices (pumps, valves, lights)
  • โœ… Capacitive soil moisture sensors save 40-60% water compared to timers
  • โœ… Different crops need different moisture thresholds โ€” adjust for your farm
  • โœ… Always add a max run time (fail-safe) to prevent flooding
  • โœ… This $44 system pays for itself in 4-6 weeks through water savings
  • โœ… Capacitive sensors last years; resistive sensors corrode in weeks
๐Ÿ’ก 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.