IOT Projects

Automatic Plant watering system using Arduino or ESP8266

Learn how to create an automatic watering system for plants and crops using Arduino and ESP8266 to monitor on your mobile with Blynk application.

Had to ever planted a plant and forgot to water it and it was dried and dead? yes it happens many times as we are involved in our tight daily routine but we are highly interested in growing plants. in the other case, if you are a farmer and growing a crop it is hard to know the status of the soil from home.

Taking it into consideration we’ve come up with a solution! We’re excited to introduce our state-of-the-art Automated Plant Watering System. This innovative system leverages the power of Arduino or ES8266 interfaced with a capacitive soil moisture sensor. By connecting this smart technology to a water pump, we’ve created a seamless solution: when the soil becomes too dry, our system automatically kicks in, activating the water pump. And when the soil is adequately moist, the pump shuts off, all without any manual intervention. Here we are going to show this procedure in different ways :

  • Interfacing with Arduino without monitoring
  • Interfacing with ESP8266(NodeMCU) to monitor status on mobile.

Before getting into work lets learn how this smart plant watering device works and what components are required to build this device.

Related project: Measure Soil NPK values using Soil NPK Sensor with Arduino

Working of smart plant watering system:

We are connecting a single MCU (Arduino, Node MCU, Wemos D1, ESP 32) to a Soil moisture sensor here we are using a capacitive sensor instead of resistance sensor because those sensors has DC current wiith two metallic electrodes printed on the silicon board which cause the electrolysis and the metals corrode by damaging the device permanently.

With capacitive sensor it outputs analog signals and the metallic parts are not exposed to water which tends to long time run of this device. as per the code our MCU fetches the sensor data through the sensor which is placed in the soil. if the soil is dry or wet the sensor will send values accordingly so the MCU will analyse the values and send the signal to the relay module and triggers the water pump accordingly.

With the same logic we can add blynk library to make this a real time monitoring device.

Required components

  • Arduino Uno R3 (or) Node MCU (or) Wemos D1 (or) ESP 32 (or) any compatible board
  • Capacitive soil moisture sensor
  • Water pump ( according to requirement)
  • single channel relay module
  • wires
  • 5V power supply

Features of capacitive soil moisture sensor:

  • Works from 3.3v to 5.5v
  • Output Voltage: 0 ~ 3.0 VDC

Method 1 : Using Arduino without real time monitoring

in this method we are using Arduino with sensor and relay with no display of data , everything runs internally. follow the below schematic diagram to connect components correctly.

Schematic diagram:

Automatic Plant watering system using Arduino schematic diagram

In the above circuit we Interfaced Arduino, Soil moisture sensor, Single channel relay and water pump. Here we had used two types of power supplies one for powering arduino (5v) and another for water pump (according to the pump requirement).

Moisture sensor connection

Connect the analog pin of the capacitive Soil moisture sensor to the A0 pin of the Arduino and + pin to the 3.3v and – pin to the GND.

Relay Module Connection

Connect the Trigger pin (IN) to one of the digital pin on Arduino here we used 13 pin, and connect the VCC to 5v from Arduino and GND to GND of Arduino. Also attach pipes for water input and output.

Water pump connection

Connect the + from the pump power supply to the relay normally open socket and then to the pump + wire, and connect the – from the supply directly to the pump – wire.

Source Code:

/***************************************************
 This example reads Capacitive Soil Moisture Sensor and automates the water pump.
 ****************************************************/
const int AirValue = 616;   //replace the value from calibration in air
const int WaterValue = 335;  //replace the value from calibration in water
int soilMoistureValue = 0;
int soilmoisturepercent=0;
void setup() {
  pinMode(13,OUTPUT); // pin where relay trigger connected
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop() {
soilMoistureValue = analogRead(A0);  //Mention where the analog pin is connected on arduino
Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent < 10)  // change this at what level the pump turns on
{
  Serial.println("Nearly dry, Pump turning on");
  digitalWrite(13,HIGH);  // Low percent high signal to relay to turn on pump
}
else if(soilmoisturepercent >85) // max water level should be
{
  Serial.println("Nearly wet, Pump turning off");
  digitalWrite(13,low);  // high percent water high signal to relay to turn on pump
}

   delay(400); //Wait for few milliseconds and then continue the loop.
}

In the above code  const int air and const int water can be obtained from calibrating the capacitive soil moisture sensor, source code for calibration can be found at the bottom of the article.

Here we used map function to calculate soil moisture percentage the syntax of map function in Arduino  is given below

map(value, fromLow, fromHigh, toLow, toHigh)

Code Explaination

This Arduino code is designed to read data from a Capacitive Soil Moisture Sensor and control a water pump based on the moisture level in the soil. Here’s a breakdown of how the code works:

Calibration Values:

const int AirValue = 616; // Replace this value with the calibration reading in air
const int WaterValue = 335; // Replace this value with the calibration reading in water

These constants store the calibration values obtained for the soil moisture sensor when it is placed in the air (AirValue) and submerged in water (WaterValue). These values help the code determine moisture levels.

Variable Declarations:

int soilMoistureValue = 0; // Stores the current soil moisture sensor reading
int soilmoisturepercent = 0; // Stores the calculated moisture percentage

These variables are used to store the current sensor reading and the calculated moisture percentage.

Setup Function:

In the setup function, pin 13 is configured as an OUTPUT, which is typically used to control a relay that turns the water pump on and off. Serial communication is also initialized for debugging purposes.

Loop Function:

  • In the loop function, the code reads the analog value from the soil moisture sensor connected to pin A0 and prints it to the serial monitor.
  • It then maps the sensor reading to a percentage value using the provided calibration values.
  • If the moisture percentage is below 10 (indicating dry soil), it turns on the water pump and prints a message to the serial monitor.
  • If the moisture percentage is above 85 (indicating wet soil), it turns off the water pump and prints a different message.
  • There is also a delay of 400 milliseconds between readings to prevent rapid cycling of the pump due to minor fluctuations in sensor readings.

This code essentially creates an automated irrigation system where the water pump is controlled based on soil moisture levels. When the soil gets too dry (below 10%), the pump is turned on, and when it becomes sufficiently wet (above 85%), the pump is turned off. The values 10 and 85 can be adjusted to suit specific soil and plant needs.

Method 2 : Using node MCU with real time monitoring using Blynk

This has same logic as method 1 but in this method we are using node MCU ( ESP 8266) instead of Arduino because it does not have network connectivity. Here we are adding blynk library to monitor the sensor data and control it over the mobile application. The circuit diagram for automatic plant watering system over network using ESP8266 is given below

Circuit Diagram:

smart automatic plant watering system using esp8266Here we just replaced the arduino with ESP8266 nodeMCU, you can use wemos D1 or ESP32 in place of nodeMCU. We connected the soil moisture sensor to the 5v power supply because the 3v supply from nodemcu is not sufficient.

Source Code:

/***************************************************
 This example reads Capacitive Soil Moisture Sensor and automates the water pump.
 ****************************************************/

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

const int AirValue = 616;   //replace the value from calibration in air
const int WaterValue = 335;  //replace the value from calibration in water
int soilMoistureValue = 0;
int soilmoisturepercent=0;
SimpleTimer timer;

void MainFunction() { 
  soilMoistureValue = analogRead(A0); //Mention where the analog pin is connected on NodeMCU 
  Serial.println(soilMoistureValue); 
  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); 
  if(soilmoisturepercent < 10) // change this at what level the pump turns on 
  { 
  Serial.println("Nearly dry, Pump turning on"); 
  digitalWrite(D5,HIGH); // Low percent high signal to relay to turn on pump 
  } 
  else if(soilmoisturepercent >85) // max water level should be 
  { 
  Serial.println("Nearly wet, Pump turning off"); 
  digitalWrite(D5,low); // high percent water high signal to relay to turn off pump 
  } 
  Blynk.virtualWrite(V2, soilmoisturepercent); //display the moisture percent. 
}

void setup() {
  pinMode(D5,OUTPUT); // pin where relay trigger connected
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L,MainFunction);

}
void loop() {
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

You can Add the button to manually turn on and off the pump from blynk application.

Capacitive soil moisture sensor calibration code:

void setup() {
  Serial.begin(9600); // open serial port, set the baud rate as 9600 bps
}
void loop() {
  int value;
  value = analogRead(0); //connect sensor to Analog 0
  Serial.println(value); //print the value to serial port
  delay(100);
}

while calibrating follow the rules as shown in the below picture

capacitive soil moisture sensor calibration

Useful ideas:

These are indeed useful ideas for protecting and prolonging the life of soil moisture sensors and their associated circuits. Here’s some additional information on each idea:

Apply Nail Polish Enamel for Water Protection:

Applying nail polish enamel or a similar waterproof sealant to the sides of the open silicon chip on the soil moisture sensor is a practical way to protect it from water damage. This protective coating can help prevent water infiltration and corrosion of the sensor’s electronics. Make sure to allow the enamel to dry thoroughly before using the sensor.

Use Silicon or Tubing for Circuit Protection Above the Warning Line:

Many soil moisture sensors have a “warning line” or a reference point above which the sensor and its circuits are not waterproof. To protect these components from moisture, you can use silicone sealant or flexible tubing to create a barrier. Seal the area above the warning line with silicone or slide a piece of tubing over the sensor and secure it in place. This will help ensure that the critical electronics remain dry and functional.

By implementing these protective measures, you can extend the lifespan of your soil moisture sensors and ensure they continue to provide accurate readings for a longer period, especially in outdoor or high-moisture environments.

If you find this project helpful please share it with your friends, and if you have any doubts or got any issues regarding this project please feel free to comment below. Your interest makes us happy and help to add more projects. Thanks and happy creations.

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button