Site icon Hackatronic

Smart IoT-Based Automatic Irrigation System Using ESP32

Automatic Irrigation System

Automatic Irrigation System

With water scarcity becoming a global concern and agriculture demanding optimal water usage, smart irrigation systems have emerged as a sustainable solution. This article presents a Smart IoT-Based Automatic Irrigation System built using an ESP32 microcontroller, soil moisture sensor, water pump, and Blynk IoT platform for real-time monitoring and control.

This system automates irrigation by monitoring soil moisture and controlling a water pump accordingly. It also allows manual pump control through a mobile app, giving farmers or gardeners flexibility and peace of mind.

๐Ÿ“ก Features of IoT-Based Irrigation System

๐Ÿ” Automatic Irrigation System Overview

The system consists of:

๐Ÿ”ฉ Required Components

Smart Irrigation System Components

Component Quantity
ESP32 Dev Board 1
Soil Moisture Sensor 1
1-Channel Relay Module 1
Submersible Water Pump 1
Power Supply (5V) 1
Connecting Wires โ€“
Smartphone with Blynk 1
Mini Water Pump
Mini Water Pump

๐Ÿ“˜ Automatic Irrigation System Circuit Diagram

Hereโ€™s a simplified circuit representation:

Smart Irrigation System Using ESP32

๐Ÿงฉ Component Pin Connections

Component ESP32 Pin
Soil Moisture Sensor GPIO34 (Analog In)
Relay Module (Pump) GPIO2 (Digital Out)
VCC (Sensors/Relay) 3.3V/5V (As needed)
GND GND

Note: Use a transistor or opto-isolator with the relay if switching high currents.

๐Ÿ› ๏ธ Assembly Instructions

Smart IoT-Based Irrigation System
Smart IoT-Based Irrigation System

โš™๏ธ Working of Smartย Irrigation System

๐Ÿ’ป Code Explanation

#define BLYNK_TEMPLATE_ID "TMPL3yY1GKAGn"
#define BLYNK_TEMPLATE_NAME "Smart Irrigation System with ESP32"
#define BLYNK_AUTH_TOKEN "wNUHEqkHCdbaOMYdShu2xRt3wUsopzcc"

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

#define SOIL_MOISTURE_PIN 34 // Analog pin for soil moisture sensor
#define THRESHOLD_MOISTURE 60 // Adjust this value based on your sensor readings (0-100)
#define PUMP_PIN 2 // Digital pin for controlling the pump
#define PUMP_SWITCH V6 // Virtual pin for controlling the pump manually

char auth[] = BLYNK_AUTH_TOKEN; // Replace with your Blynk auth token
char ssid[] = "WiFi Username"; // Replace with your WiFi credentials
char pass[] = "WiFi Password";

BlynkTimer timer;

bool isPumpOn = false; // Variable to track pump status

// Function to smooth the sensor readings (average over 10 readings)
int readSoilMoisture() {
int totalMoisture = 0;
for (int i = 0; i < 10; i++) {
totalMoisture += analogRead(SOIL_MOISTURE_PIN);
delay(10); // Small delay to stabilize readings
}
return totalMoisture / 10; // Average reading
}

void sendSensorData() {
int soilMoisture = readSoilMoisture();
Serial.print("Raw Soil Moisture: ");
Serial.println(soilMoisture);

// Map the analog sensor values to a percentage (0-100)
int soilMoisturePercentage = map(soilMoisture, 4095, 0, 0, 100); // Adjust this mapping based on your sensor's characteristics
Serial.print("Soil Moisture Percentage: ");
Serial.println(soilMoisturePercentage);

// Send soil moisture data to Blynk
Blynk.virtualWrite(V5, soilMoisturePercentage);

// Check if the pump should be on based on manual switch or soil moisture level
if (isPumpOn || soilMoisturePercentage < THRESHOLD_MOISTURE) {
// Turn on the pump
digitalWrite(PUMP_PIN, HIGH);
// Check if the pump is turned on automatically (not manually)
if (!isPumpOn) {
// Send alert notification to Blynk app if the pump is turned on automatically
Blynk.logEvent("moisture_alert", "Soil moisture is below the threshold!");
Serial.println("Soil moisture is below the threshold!");
}
} else {
// Turn off the pump only if it was not turned on manually
if (!isPumpOn) {
digitalWrite(PUMP_PIN, LOW);
}
}
}

BLYNK_WRITE(PUMP_SWITCH)
{
isPumpOn = param.asInt();
if (isPumpOn) {
Serial.println("Pump manually turned ON");
} else {
Serial.println("Pump manually turned OFF");
}
}

void setup()
{
Serial.begin(9600);
pinMode(PUMP_PIN, OUTPUT); // Set pump pin as an output

Blynk.begin(auth, ssid, pass);

timer.setInterval(3000L, sendSensorData); // Set the interval for checking soil moisture (every 3 seconds)

// Setup switch widget
Blynk.virtualWrite(PUMP_SWITCH, isPumpOn);
Blynk.syncVirtual(PUMP_SWITCH);
}

void loop()
{
Blynk.run();
timer.run();
}

This code is for a Smart Irrigation System using an ESP32 and Blynk. It monitors soil moisture and controls a water pump automatically or manually through the Blynk mobile app.

Hereโ€™s a breakdown of the code:

๐Ÿ“Œ 1. Definitions and Library Inclusions

#define BLYNK_TEMPLATE_ID "TMPL3yY1GKAGn"
#define BLYNK_TEMPLATE_NAME "Smart Irrigation System with ESP32"
#define BLYNK_AUTH_TOKEN "wNUHEqkHCdbaOMYdShu2xRt3wUsopzcc"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

๐Ÿ“Œ 2. Pin Definitions and Constants

#define SOIL_MOISTURE_PIN 34
#define THRESHOLD_MOISTURE 60
#define PUMP_PIN 2
#define PUMP_SWITCH V6

๐Ÿ“Œ 3. WiFi and Blynk Credentials

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "WiFi Username";
char pass[] = "WiFi Password";

๐Ÿ“Œ 4. Global Variables

BlynkTimer timer;
bool isPumpOn = false;

๐Ÿ“Œ 5. Reading Soil Moisture

int readSoilMoisture() {
  int totalMoisture = 0;
  for (int i = 0; i < 10; i++) {
    totalMoisture += analogRead(SOIL_MOISTURE_PIN);
    delay(10);
  }
  return totalMoisture / 10;
}

๐Ÿ“Œ 6. Sending Data to Blynk & Controlling Pump

void sendSensorData() {
  int soilMoisture = readSoilMoisture();
  int soilMoisturePercentage = map(soilMoisture, 4095, 0, 0, 100);
  Blynk.virtualWrite(V5, soilMoisturePercentage);
  if (isPumpOn || soilMoisturePercentage < THRESHOLD_MOISTURE) {
    digitalWrite(PUMP_PIN, HIGH);
    if (!isPumpOn) {
      Blynk.logEvent("moisture_alert", "Soil moisture is below the threshold!");
    }
  } else {
    if (!isPumpOn) {
      digitalWrite(PUMP_PIN, LOW);
    }
  }
}
IoT-Based Automatic Irrigation System
IoT-Based Automatic Irrigation System

๐Ÿ“Œ 7. Manual Pump Control

BLYNK_WRITE(PUMP_SWITCH)
{
  isPumpOn = param.asInt();
  if (isPumpOn) {
    Serial.println("Pump manually turned ON");
  } else {
    Serial.println("Pump manually turned OFF");
  }
}

๐Ÿ“Œ 8. Setup

void setup()
{
  Serial.begin(9600);
  pinMode(PUMP_PIN, OUTPUT);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(3000L, sendSensorData);
  Blynk.virtualWrite(PUMP_SWITCH, isPumpOn);
  Blynk.syncVirtual(PUMP_SWITCH);
}

๐Ÿ“Œ 9. Loop

void loop()
{
  Blynk.run();
  timer.run();
}

DIY Smart Irrigation System using ESP32 & Blynk | Capacitive Soil Sensor Project

โœ… Advantages

โŒ Disadvantages

๐Ÿšœ Applications

๐Ÿง  Future Improvements

๐Ÿงพ Conclusion

This Smart IoT-Based Irrigation System using ESP32 is a powerful DIY project that merges IoT, automation, and agricultural innovation. It offers a sustainable, smart, and convenient way to ensure that plants receive the right amount of water at the right time. This project:

By combining ESP32, Blynk, and basic sensors, you can create a reliable system to support your garden or farm โ€“ all while monitoring everything from the palm of your hand.

Water Quality Monitoring System: TDS Sensor + ESP32/Arduino

DIY 433MHz RF Remote Control Car Circuit Diagram and Working

BME680 and ESP8266 Based Indoor Air Quality Monitoring System

1S, 2S, 3S, 4S BMS Circuit Diagram for Li-ion Batteries

Exit mobile version