Contact Us
Blog >> Blog Details Page

Smart Irrigation System using ESP32

Posted:10:08 AM January 17, 2024 writer: The Engineering Projects

Smart Irrigation System using ESP32

Hello learners! I hope you are doing great. Today, we are going to discuss one of the most basic and important projects in agriculture and soil domains using the Internet of Things (IoT). The smart irrigation system using ESP32 is a simple project that automatically takes care of the optimization of the basic soil conditions. The heart of this project is the ESP32, as it continuously checks for values from different sensors and sends commands to the other components to work accordingly.

 

The article provides a comprehensive walkthrough on how to design and code the project with  ESP32 using the Arduino IDE, covering all necessary steps in detail. This project is done on a small amount of soil, but it can be enhanced on a large scale by increasing the number of components. In such cases, the basics of the project will remain the same, but things will be designed at a higher level. We’ll understand the scope of the project and study the basic workings and roles of each component in the project. The code and its explanation are given in simple words, and in the end, the details of the procedure are mentioned. Let’s start with the scope of the project to understand it well. 

Get Instant PCB Online Quote

What is a Water Irrigation System?

An irrigation system is an artificial application to water the soil on different levels using multiple systems such as tubes, sprays, or pumps. These systems fulfill the water needs of large agricultural areas where natural water is inadequate for the land. The scope of the irrigation project may vary from large projects of crop watering to small and simple watering systems for plants at home or offices. The project we are discussing is a smart irrigation system, so let’s discuss it in detail. 

Smart Irrigation System using ESP32

The Internet of Things (IoT) is a highly demanding field that is applied in multiple domains of life such as agricultural projects it can be used for the automation of multiple tasks. It is the most dominant field that has been revolutionized by integrating sensors, machines, data analytics, etc. It has the potential to increase efficiency and performance, improve resource management, enhance crop quality, and, as a result, empower farmers. The smart irrigation system is one such project that is extensively used in this field. 

 

The smart irrigation system using ESP32 is a smart way to nurture the greenery at a small level with intelligence and efficiency. It is the best way to make sure that your plants are not overwatering or undervaluing but are at their best moisture level. Once this system is set to your place, there is no need to worry about watering your plants manually, and you can monitor the plant's health in terms of moisture and humidity. 

Why Use a Smart Irrigation System?

In the technical world, efficiency and sustainability reign supreme, and the smart irrigation system using ESP32 has made the lives of both humans and plants easy. There are multiple advantages that highlight the importance of this project, and some of these are discussed below:

Smart Irrigation System Conserves Water

The wastage of water is one of the most serious issues worldwide, and smart irrigation systems are a small step against this wastage. The sensors of this system continuously sense the values of soil moisture and deliver water only when required. This is not only cost-effective but it also saves water. Moreover, the excessive water given to the plant is harmful to its health. 

The water schedule does not remain the same all the time, but multiple factors affect the required amount of water for the plant, such as humidity in the air and temperature. These factors are regularly monitored by the sensors, and the display screen shows the values as well. As a result, the plant gets the water exactly according to its needs. 

Smart Irrigation System Results in Better Yield

The frequency of watering is an important factor in the plant's health and yield. With the help of this system, the optimal watering level leads to thriving plants with healthy growth. The regular and appropriate amount also helps the plant fight issues related to improper watering, such as fungal growth, water-loving pests, etc. In short, a healthy plant has fewer chances of attack and provides more yield. 

Smart Irrigation System using ESP32 is a Time Saver

The most prominent advantage of this watering system is that it saves time. This system can be controlled remotely via a smartphone; therefore, the owner does not have to go to a particular place in the home to water their plants. This can be done from any corner of the house, or even if the owner is outside the house, he or she can have a look at their plant health and other parameters. 

The plants have a unique nourishment system that requires watering early in the morning or at the optimal time. This is usually different from the human routine, and the automatic watering system is a stress-free solution in such situations. 

Smart Irrigation System using ESP32 Project Description

  • This is a simple project that automatically waters the plans by checking the values of the sensors used in the project. 
  • The basic components of this project are an ESP32, a DHT11 sensor for humidity and temperature, two moisture sensors for moisture, a fan, a pump, and valves. 
  • The ESP32 takes the values from the sensors and turns the components on/off according to the values obtained by these sensors. 

  • The user can see these values on the LCD of the project as well as on the web page designed to examine the values and turn the relay 4 ON/OFF through a button. 
  • There are two temperature sensors placed at different points on the soil, and these provide the values from two different parts of the soil. As a result, the whole soil is under examination. 
  • The ESP32 is coded in the Arduino IDE, and each detail is mentioned in this article. 
  • The project works automatically; the ESP32 continuously checks the information from the sensors, and when the threshold values mentioned in the code are encountered, the ESP32 sends the signal to the respective components through the pins to act accordingly.
  • Whenever the moisture of the plants is lower than the set values, the pump starts, and the steady flow of water starts on the soil. 
  • Similarly, when the temperature sensed by the DHT11 is lower than a particular level, the small fan connected to it starts to optimize the temperature. 
  • As a result, this project is used to provide the necessary optimized conditions to the soil and is programmed easily using the ESP32. 

Role of ESP32 in Smart Irrigation System

The ESP32 is a microcontroller, and it is one of the most extensively used computers in IoT projects. In the smart irrigation system, the ESP32 acts as the heart, controlling the flow of information from the sensor to the output devices and also initiating action according to the required conditions. 

 

The plus point of this microcontroller is that it is easy to install and can be coded on the lightweight Arduino IDE. It is the central point of the system that takes the information from the sensors, connects the components with the internet, and controls the actuators. 

In simple words, the ESP32 is the middle point where all the components are connected. It is coded in such a way that it takes the values from the sensors and shows them on the LCD and dashboard of the website. Once any value reaches the threshold value, it sends the signals to the respective device to optimize the required parameter. 

Relay in the Smart Irrigation System

The water flow in the smart irrigation system using ESP32 is controlled with the help of relays. It has multiple duties, such as:

  • The relay controls the water pump or water valve in the smart irrigation system. It takes the values from the moisture sensors and allows the water to flow from different systems connected to them. In short, it activates the water pump for the irrigation process. 
  • These are the electrically operated switches, and whenever the current flows through their coil, it creates a magnetic field, and as a result, the liver is attracted. This attraction results in a change in the state of the switch from open to closed. The same principle is applied to the closing of the switch. 

  • It acts as the interface between the ESP32 and water pumps. The ESP32 sends the data to the relays, and it allows the pump to start or stop according to the data. 
  • This is the source for energy management. It not only controls the water flow but the electricity supply as well.
  • The relays are responsible for the scheduling of the water irrigation system. It acts on the information from the ESP32 and works accordingly. 

Moisture Sensor

Free Components Worldwide Shipping

The moisture sensor reads the volumetric water content in the soil. The main domains where this sensor has scope are given next:

  • Soil science
  • Agricultural science
  • Environmental science
  • Horticulture
  • Botany
  • Biology

The moisture sensors do not calculate the amount of water content directly; instead, these sensors measure the soil properties related to the water content in a predictable way. You will encounter multiple types of moisture sensors, such as Capacitance Sensors, Time Domain Reflectometry (TDR) Sensors, Neutron Attenuation Sensors, Tensiometers, etc. 

Here, I would like to gain an understanding of the fundamental principles of the moisture sensor employed in this project. This moisture sensor consists of two parts:

  1. Soil Moisture Sensor Module
  2. Capacitive plates

The basic properties of the soil moisture module that will help you understand their functionality in the project are mentioned here:

  • The soil moisture module measures different parameters in the soil, such as humidity, and represents the values of the moisture as an output. 
  • This module consists of the LM393 design.
  • It represents the digital as well as the analogue output. 
  • It has four pins and details of each of it are given in the table below:

Pin Name

Description

Additional Details

VCC

Voltage Supply

Provides power to the module. Typically operates at +5V.

GND

Ground

Common reference point for the circuit. Connects to ground potential.

DO

Digital Output Pin

Outputs digital signals like HIGH (3.3V) or LOW (0V). It can be used for driving LEDs, relays, or other digital devices.

AO

Analog Output Pin

Outputs analogue voltage signals within a specific range (e.g., 0-3.3V). Allows interfacing with analogue devices like sensors and actuators.

  • The modules have LEDs that indicate the output according to the code. 
  • It is a small, cheap, and easy-to-use module. 

Now, let’s understand the features of the capacitive plates attached to this module:

  • The two capacitive plates are attached to the module that sends the sensed data to the module, and after the calculations, the module provides the output. 
  • The capacitance of these plates changes with respect to their surrounding environment, and this capacitance depends on the dialect material between the plates. 
  • The air is the poor dialect, whereas the water is the best dialect; therefore, whenever the soil has even a small amount of moisture, there is a visible change in the moisture sensor readings, and these are indicated in the project. 
  • These plates are placed in direct contact with the soil and in our project, these plates are used in the image given next:

Role of DHT11 in Smart Irrigation System

The DHT11 is a temperature and humidity sensor that has a wide range of applications in similar fields, as discussed before in the moisture sensor. To provide the required calculations, it has two types of sensors in it:

  1. A capacitive sensitivity sensor to measure the humidity. 
  2. A thermometer to detect the surrounding air temperature.

As a result, it provides the digital reading on its digital output pin. This sensor has four pins and the name and description of each of it is given here:

Pin Name

Description

Additional Information

VCC

Power Supply

It provides +5V DC to the sensor.

GND

Ground

It connects to the circuit ground potential.

Data

Digital Output

It outputs serial data representing temperature and humidity readings. Moreover, it requires a pull-up resistor (typically 10 kΩ) for stable operation.

NC

Not Connected

Usually, this pin is left unconnected. Some newer versions might use it for future features.

The plus point of using this instrument in our project is, that it provides the values of humidity and temperature at the same output pin so that it makes the project more simplified. 

In this project, this sensor is controlled with ESP32 coding. Here, the triggered values are set through the code and as a result, the ESP32 is able to turn the fans ON/OFF according to the values sensed by the DHT11. 

The Fan in the Smart Irrigation System

A small fan is attached to the ESP32 that is turned on only when the temperature of the system rises to a particular value. These values are programmed in the ESP32 code. 

Pumps in the Smart Irrigation System

The compact-sized pumps are used in the system that is used to get the water from the resource and supply it to the field. These get the signals from the ESP32 that turn the pump only when required. 

Smart Irrigation System Dashboard

The information on the smart irrigation system is seen on the smart device's web browser. This information is obtained from the ESP32 and can be controlled and monitored continuously. The information on the dashboard includes the following four factors:

  1. Moisture 1
  2. Moisture 2
  3. Temperature
  4. Humidity

The details of each of them are discussed below:

Moisture 1 and 2 in Irrigation System Dashboard

The irrigation system consists of two moisture sensors labelled sensor 1 and sensor 2, and the values obtained are numbered according to their respective sensors. Collectively, both provide information from the different areas of the soil. The values of these sensors depend on different environmental factors, such as:

  • Sun exposure
  • Drainage
  • Plant types 

Humidity in the Irrigation System Dashboard

The values of the humidity in the system are continuously sensed by the DHT11 sensor, and the values from the output pins are fed into the ESP32. This enables the user to see humidity values through the dashboard. 

Smart Irrigation System’s Temperature Value

Similar to the humidity values, the temperature is obtained from the output pin of DHT11. These calculations are fed into the ESP32, and as a result, the temperature of the smart irrigation system can be examined through the dashboard.

Get Free PCB Assembly

Smart Irrigation System Code

#include

#include

#include

#include

#include

#include

#define MOISTURE_SENSOR1_PIN 34

#define MOISTURE_SENSOR2_PIN 35

#define DHT_PIN 2

#define RELAY1_PIN 4

#define RELAY2_PIN 5

#define RELAY3_PIN 18

#define RELAY4_PIN 19

int lcdColumns = 16;

int lcdRows = 2;

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

DHT dht(DHT_PIN, DHT11);

const char* ssid = "TP";

const char* password = "12345678";

WebServer server(80);

void setup() {

  Serial.begin(115200);

  pinMode(MOISTURE_SENSOR1_PIN, INPUT);

  pinMode(MOISTURE_SENSOR2_PIN, INPUT);

  pinMode(RELAY1_PIN, OUTPUT);

  pinMode(RELAY2_PIN, OUTPUT);

  pinMode(RELAY3_PIN, OUTPUT);

  pinMode(RELAY4_PIN, OUTPUT);

  lcd.init();

  lcd.backlight();

  lcd.setCursor(0, 0);

  lcd.print("Smart Farm");

  dht.begin();

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println("WiFi connected");

  Serial.print("IP Address: ");

  Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, handleRoot);

  server.on("/relay4/on", HTTP_GET, handleRelay4On);

  server.on("/relay4/off", HTTP_GET, handleRelay4Off);

  server.begin();

}

void loop() {

  server.handleClient();

  float moisture1 = analogRead(MOISTURE_SENSOR1_PIN);

  float moisture2 = analogRead(MOISTURE_SENSOR2_PIN);

  float temperature = dht.readTemperature();

  float humidity = dht.readHumidity();

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Moisture1: " + String(moisture1));

  lcd.setCursor(0, 1);

  lcd.print("Moisture2: " + String(moisture2));

  delay(2000);

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Temp: " + String(temperature) + "C");

  lcd.setCursor(0, 1);

  lcd.print("Humidity: " + String(humidity) + "%");

  delay(2000);

  Serial.print(moisture1);

  Serial.print("\t\t");

  Serial.print(moisture2);

  Serial.print("\t\t");

  Serial.print(temperature);

  Serial.print("\t\t");

  Serial.println(humidity);

  // Auto-control relays based on sensor readings

  if (moisture1 <= 3000) {

    digitalWrite(RELAY1_PIN, HIGH);

  } else {

    digitalWrite(RELAY1_PIN, LOW);

  }

  if (moisture2 <= 3000) {

    digitalWrite(RELAY2_PIN, HIGH);

  } else {

    digitalWrite(RELAY2_PIN, LOW);

  }

  if (temperature <= 22.0) {

    digitalWrite(RELAY3_PIN, HIGH);

  } else {

    digitalWrite(RELAY3_PIN, LOW);

  }

}

void handleRoot() {

  float temperature = dht.readTemperature();

  float humidity = dht.readHumidity();

  String html = "";

  html += "

  html += "";

  html += "Smart Farm Dashboard";

  html += "Moisture 1: " + String(analogRead(MOISTURE_SENSOR1_PIN)) + "";

  html += "Moisture 2: " + String(analogRead(MOISTURE_SENSOR2_PIN)) + "";

  html += "Temperature: " + String(temperature) + " °C";

  html += "Humidity: " + String(humidity) + " %";

  // Relay 4 control button

  html += "

  html += digitalRead(RELAY4_PIN) == HIGH ? "on" : "off";

  html += "" id="relay4Button" onclick="toggleRelay()">Relay 4: ";

  html += digitalRead(RELAY4_PIN) == HIGH ? "on" : "off";

  html += "" id="relay4Button" onclick="toggleRelay()">

  html += digitalRead(RELAY4_PIN) == HIGH ? "on" : "off";

  html += "" id="relay4Button" onclick="toggleRelay()">  html += digitalRead(RELAY4_PIN) == HIGH ? "ON" : "OFF";

  html += "";

  html += "";

  html += "";

  html += "";

  server.send(200, "text/html", html);

}

// ... [previous code remains the same]

void handleRelay4On() {

  digitalWrite(RELAY4_PIN, LOW); // Turn on Relay 4

  server.send(200, "text/plain", "Relay 4 turned ON");

}

void handleRelay4Off() {

  digitalWrite(RELAY4_PIN, HIGH); // Turn off Relay 4

  server.send(200, "text/plain", "Relay 4 turned OFF");

}

// ... [rest of the code remains the same]

Smart Irrigation System Code Explanation

If, at the moment, you are unable to understand the code, then let me divide it into different parts and explain them one by one. 

Including libraries

The first step is to include all the libraries that will help us avoid long codes because these have pre-defined codes and the user does not have to repeat the lines. In this code, these libraries handle features of wifi connection with the ESP32, LED display, connection with the web server, read the values from the DHT11 sensor, and include the values of the sensors in the code. 

Pins Definition

The next step is to define the pins of the ESP32 with respect to the physical connection used in the project. It is important to define the right pins because these will be used throughout the codes. The simple #define function is used for this purpose, and the user can have any name for the pin. Usually, user-friendly names are considered. 

Setting the Values

The LCD library included in the code will help us set up the real-time LCD in the project. In these lines, the numbers of rows and columns are defined and are used in the pre-defined function in the library. It is because the LCDs come in different versions in which the size and specifications vary. Through this part of the code, the ESP32 is understood to show the output that perfectly aligns on the screen.

Similarly, the DHT library is used, and we define the pin in its pre-defined function. In the same part, the password and name of the WiFi are specified. The right data type for this information is “character.” When the compiler knows the basic settings, the server is ready to run; therefore, we are using the line of code to run the server at port 80. 

void setup() in Smart Irrigation System Code

This is the part of the code that will run only once. When the compiler reaches these lines, the output on the project starts showing. The defined pins are used here, and there are two possibilities for these pins, that is, they are either input or output. The state of the pins has been mentioned here according to their functions. 

This is the point where the label of our project, that is, “Smart irrigation system,” is shown on the screen. The DHT11 sensor starts its work, and the server starts and checks if the right credentials are provided by the users. The WiFi library starts its work, and the server is set so that the user may see the information. 

void loop() in Smart Irrigation System

The void loop is the one that will run continuously; therefore, here, we are using the values that have to be checked all the time when the project is running. In simple words, the basic motto of this project is to examine the humidity, moisture, and temperature without any break; therefore, we are using such settings in this portion of the code. 

With this part of the code, the LED continuously takes the values from the ESP32 and shows the analog values on it.  

In every step, the ESP32 refreshes the LCD through the setCursor() function, and every time, it shows the required readings of moisture 1, moisture 2, humidity, and temperature. The threshold values are set here through an if-else loop so that the pins work according to the values sensed by the sensors. 

void handleRoot() in Irrigation System Code

This is the part of the code that handles the interface of the webpage. Here, the settings of the colour scheme, fonts, style, and string are present. This is the HTML code that sets the body and head of the webpage. A button is designed here for the relay 4 and the user can turn it ON/OFF according to the requirement. 

Smart Irrigation System Steps

Now, that you know all the basic information about the smart irrigation system, it is easy for you to create one by yourself. Simply follow the given steps:

Components Required

  • ESP32
  • Pumps
  • Valves
  • DHT11 sensor
  • Moisture sensors (2)
  • Fan
  • Relay
  • Motor
  • LCD (16 X 2)
  • Connecting wires
  • Prototype breadboard
  • Power switch
  • Arduino IDE

Procedure

  • Fire up your Arduino IDE. 

 

  • Download and install the required libraries. 
  • Paste the code into the new sketch.

  • Connect the ESP32 via a cable to the system. 
  • Verify the code to check for any errors. 
  • Click on the upload button to install it into ESP32.

  • Once the code is uploaded, press the boot button. 
  • If it is successfully uploaded, remove the ESP32 from the system. 
  • Now, go to the serial monitor, and press the EN button to see the information. 

  • It will provide the IP address where the dashboard of the project will be shown.  
  • A prototype breadboard is used to fix the ESP32 and small components in the project. 

  • Now, connect all the components physically with the help of the following diagram:
  • Use the soil to complete the project. 
  • Now, start the project by providing it with power. The following switch is the main source of electricity for the project:


  • If everything is going well, the LCD of the project will start showing the welcome message and the values from each sensor, one by one. 
  • Now, let’s get toward the remote connection with this project through the browser. 
  • Once the ESP32 is turned on, it will start emitting the signals. Use any device with the browser to connect with the ESP32. 
  • Turn the WiFi on, and the SSID specified in the code will be shown there. If you have used the same code, then it must be “TEP.” 
  • Connect with the SSID by using the password mentioned in the code. In our case, it is 12345678.
  • Once you are connected, go to the browser, and in a new tab, type the IP address obtained from the serial monitor. 
  • The serial monitor shows information at every step about whether the user is connected and other statuses about the connection. 

  • At this IP address, the values of temperature, humidity, relay status, and moisture are mentioned. Users can change the state of the relay button. 

Free Components Worldwide Shipping

Limitations of Smart Irrigation System

This project has multiple advantages, but right now, it does not provide the perfect performance. The following are some important limitations in this regard:

  • The system consumes electricity continuously, which may be expensive. 
  • The hardware components have to bear the harsh weather conditions, so there are chances of failure.
  • Stable internet is required all the time. 
  • The user must have the technical information. 

Hence, today we have created the smart irrigation system using ESP32. This system can be incorporated in houses and offices, and if made at a higher level, can be used in the fields and crops as well. This was just a small idea to present you with the working of different components. We have understood the scope of this project and started learning its production step by step. I hope your project is working fine as mine but if you are stuck at any point, you can contact us. 

 

You may also be interested in...

ESP32 BLE(Bluetooth Low Energy) Control in Arduino IDE

Getting Started with ESP32

Free Worldwide Shipping on Over 600,000 Electronics Components with HQ Online

Free PCB Assembly Offer is Now Live: Experience Reliable PCB Assembly from HQ NextPCB

HQ NextPCB Introduces New PCB Gerber Viewer: HQDFM Online Lite Edition

  • PCB
    Prototype
  • PCB
    Assembly
  • SMD
    Stencil

Dimensions: (mm)

×

Quantity: (pcs)

5
5
10
15
20
25
30
40
50
75
100
120
150
200
250
300
350
400
450
500
600
700
800
900
1000
1500
2000
2500
3000
3500
4000
4500
5000
5500
6000
6500
7000
7500
8000
9000
10000

Other Quantities:(quantity*length*width is greater than 10㎡)

OK

Layers:

Thickness:

Quote now