Smart Irrigation System with Soil Moisture Sensor using ESP32 wowki simulation



Automate your garden watering based on real soil moisture levels, saving water and ensuring optimal plant health.

Key Features

  • Automated watering control
  • Soil moisture monitoring
  • Weather API integration
  • Mobile notifications
  • Water usage tracking
Components Required
  • ESP32 Board
  • Soil Moisture Sensor
  • Water pump/Solenoid valve
  • Relay module
  • Power supply

Applications

  • Home gardening
  • Agriculture automation
  • Greenhouse management
  • Urban farming

Difficulty Level

Beginner to Intermediate

Quick Setup (2 Steps):

Diagram.json:
{
  "version": 1,
  "author": "Smart Irrigation System",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-esp32-devkit-v1",
      "id": "esp",
      "top": 0,
      "left": 0,
      "attrs": {}
    },
    {
      "type": "wokwi-potentiometer",
      "id": "pot1",
      "top": -60,
      "left": 250,
      "attrs": {
        "label": "Soil Moisture"
      }
    },
    {
      "type": "wokwi-relay-module",
      "id": "relay1",
      "top": 100,
      "left": 300,
      "attrs": {}
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -80,
      "left": -100,
      "attrs": {
        "color": "red",
        "label": "DRY"
      }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -40,
      "left": -100,
      "attrs": {
        "color": "green",
        "label": "WET"
      }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 0,
      "left": -100,
      "attrs": {
        "color": "blue",
        "label": "PUMP"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -70,
      "left": -160,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -30,
      "left": -160,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": 10,
      "left": -160,
      "attrs": {
        "value": "220"
      }
    },
    {
      "type": "wokwi-dc-motor",
      "id": "motor1",
      "top": 200,
      "left": 450,
      "attrs": {
        "label": "Water Pump"
      }
    }
  ],
  "connections": [
    [
      "esp:TX0",
      "$serialMonitor:RX",
      "",
      []
    ],
    [
      "esp:RX0",
      "$serialMonitor:TX",
      "",
      []
    ],
    [
      "pot1:VCC",
      "esp:3V3",
      "red",
      [
        "v0"
      ]
    ],
    [
      "pot1:GND",
      "esp:GND.1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "pot1:SIG",
      "esp:34",
      "green",
      [
        "v0"
      ]
    ],
    [
      "relay1:VCC",
      "esp:3V3",
      "red",
      [
        "v0"
      ]
    ],
    [
      "relay1:GND",
      "esp:GND.2",
      "black",
      [
        "v0"
      ]
    ],
    [
      "relay1:IN",
      "esp:25",
      "orange",
      [
        "v0"
      ]
    ],
    [
      "r1:1",
      "esp:26",
      "red",
      [
        "v0"
      ]
    ],
    [
      "r1:2",
      "led1:A",
      "",
      [
        "v0"
      ]
    ],
    [
      "led1:C",
      "esp:GND.1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "r2:1",
      "esp:27",
      "green",
      [
        "v0"
      ]
    ],
    [
      "r2:2",
      "led2:A",
      "",
      [
        "v0"
      ]
    ],
    [
      "led2:C",
      "esp:GND.1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "r3:1",
      "esp:33",
      "blue",
      [
        "v0"
      ]
    ],
    [
      "r3:2",
      "led3:A",
      "",
      [
        "v0"
      ]
    ],
    [
      "led3:C",
      "esp:GND.1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "relay1:COM",
      "motor1:A+",
      "purple",
      [
        "v0"
      ]
    ],
    [
      "relay1:NO",
      "esp:VIN",
      "red",
      [
        "v0"
      ]
    ],
    [
      "motor1:A-",
      "esp:GND.2",
      "black",
      [
        "v0"
      ]
    ]
  ],
  "dependencies": {}
}


Step 1: Go to https://wokwi.com and create a new ESP32 project

Step 2:

  • Paste smart_irrigation.ino into the code editor
  • Paste diagram.json into the diagram.json tab
  • Click Play ▶️

That's it! The circuit auto-builds and starts working!

Code:

/*
 * Smart Irrigation System with Soil Moisture Sensor
 * ESP32 based automatic plant watering system
 *
 * Components:
 * - ESP32 DevKit
 * - Soil Moisture Sensor (Analog)
 * - Relay Module
 * - Water Pump
 * - LED indicators
 */

// Pin Definitions
#define SOIL_MOISTURE_PIN 34  // Analog pin for soil moisture sensor
#define RELAY_PIN 25          // Digital pin to control relay/pump
#define LED_DRY 26            // LED indicator for dry soil (Red)
#define LED_WET 27            // LED indicator for wet soil (Green)
#define LED_PUMP 33           // LED indicator for pump status (Blue)

// Threshold values (adjust based on your sensor)
#define DRY_THRESHOLD 40      // Below this value, soil is dry (0-100 scale)
#define WET_THRESHOLD 70      // Above this value, soil is wet enough

// Timing constants
#define PUMP_ON_TIME 5000     // Pump runs for 5 seconds
#define READ_INTERVAL 2000    // Read sensor every 2 seconds

// Variables
int soilMoistureValue = 0;
int soilMoisturePercent = 0;
bool pumpActive = false;
unsigned long lastReadTime = 0;
unsigned long pumpStartTime = 0;

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  Serial.println("Smart Irrigation System Starting...");
 
  // Configure pins
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(LED_DRY, OUTPUT);
  pinMode(LED_WET, OUTPUT);
  pinMode(LED_PUMP, OUTPUT);
 
  // Initialize outputs to OFF
  digitalWrite(RELAY_PIN, LOW);
  digitalWrite(LED_DRY, LOW);
  digitalWrite(LED_WET, LOW);
  digitalWrite(LED_PUMP, LOW);
 
  Serial.println("System Ready!");
  Serial.println("Monitoring soil moisture...");
  Serial.println("-------------------------------");
}

void loop() {
  unsigned long currentTime = millis();
 
  // Read soil moisture at regular intervals
  if (currentTime - lastReadTime >= READ_INTERVAL) {
    readSoilMoisture();
    lastReadTime = currentTime;
  }
 
  // Control irrigation based on moisture level
  if (!pumpActive && soilMoisturePercent < DRY_THRESHOLD) {
    // Soil is dry, start watering
    startPump();
  }
 
  // Stop pump after designated time
  if (pumpActive && (currentTime - pumpStartTime >= PUMP_ON_TIME)) {
    stopPump();
  }
 
  // Update LED indicators
  updateLEDIndicators();
}

void readSoilMoisture() {
  // Read analog value from sensor
  soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
 
  // Convert to percentage (0-100%)
  // ESP32 ADC: 0-4095 (12-bit)
  // Higher value = more moisture
  soilMoisturePercent = map(soilMoistureValue, 0, 4095, 0, 100);
 
  // Display readings
  Serial.print("Soil Moisture: ");
  Serial.print(soilMoisturePercent);
  Serial.print("%  (Raw: ");
  Serial.print(soilMoistureValue);
  Serial.print(")  Status: ");
 
  if (soilMoisturePercent < DRY_THRESHOLD) {
    Serial.println("DRY - Needs watering!");
  } else if (soilMoisturePercent > WET_THRESHOLD) {
    Serial.println("WET - No watering needed");
  } else {
    Serial.println("MODERATE - Monitoring");
  }
}

void startPump() {
  if (!pumpActive) {
    pumpActive = true;
    pumpStartTime = millis();
    digitalWrite(RELAY_PIN, HIGH);
    digitalWrite(LED_PUMP, HIGH);
   
    Serial.println(">>> PUMP STARTED - Watering plants...");
  }
}

void stopPump() {
  if (pumpActive) {
    pumpActive = false;
    digitalWrite(RELAY_PIN, LOW);
    digitalWrite(LED_PUMP, LOW);
   
    Serial.println(">>> PUMP STOPPED - Watering complete");
    Serial.println("-------------------------------");
  }
}

void updateLEDIndicators() {
  // Update soil moisture indicator LEDs
  if (soilMoisturePercent < DRY_THRESHOLD) {
    digitalWrite(LED_DRY, HIGH);
    digitalWrite(LED_WET, LOW);
  } else if (soilMoisturePercent > WET_THRESHOLD) {
    digitalWrite(LED_DRY, LOW);
    digitalWrite(LED_WET, HIGH);
  } else {
    // Moderate moisture - both LEDs dim or off
    digitalWrite(LED_DRY, LOW);
    digitalWrite(LED_WET, LOW);
  }
}

✨ Features:

  • ✅ Automatic watering when soil is dry
  • ✅ 3 LED indicators (Dry/Wet/Pump status)
  • ✅ Real-time Serial Monitor data
  • ✅ Adjustable thresholds
  • ✅ Fully commented code for learning

The joystick simulates a soil moisture sensor - move it left for dry soil, right for wet soil. Watch the LEDs and motor respond automatically!

Comments