Automatic irrigation system with Arduino and Sensors


In this tutorial, we will be making an automatic irrigation system (AIS) with Arduino and the use of temperature, soil moisture and ultrasonic sensors. Based on the water level of the tank, temperature and moisture of the soil an automatic irrigation system is designed to keep the soil moisture.


In the irrigation system, normally we have issues with the soil moisture, the water tank empty with water and high temperature. So in this work, a smart irrigation system is being designed which will deal with these two parameters, temperature and soil moisture. These two prominent factors are measured through the sensors and based on the requirement take action to pump the land. But before that have to measure the level of the water in the tank from where the water is coming out.

Working Principle of the Automatic Irrigation System

This innovative automatic irrigation system works based on sensors which continuously measure the water level, and keep focus on the temperature and moisture of the soil. Case-1 the water level is full and soil moisture is low, the pump turns on to give water supply to the soil. Case-2 the water level is full and the temperature high, the pump turns on to give water supply to the soil. Case- 3 the water tank has limited water and soil moisture gets low or temperature becomes high, so the water pump doesn’t turn on.

What We Need

Arduino Uno R3 board

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that in the end you will be able to evaluate overall costs and decide if continue with the project or remove them from the shopping cart. So, hardware will be only:

Step-by-Step Procedure

Wiring Diagram of Irrigation system

The sensors, LCD and pump require these connection pins to connect to the microcontroller as given in the table. The pin 1 for each component starts from the left side for convenience.

Ultrasonic sensorArduinoPumpSoil Moisture sensorTemperature sensorLCD
VCC (PIN 1)5 VVCCVCC (left)VCC Pin 2
Trig (Pin 2)Pin 7
ECHO (Pin 3)Pin 8
Pin 6Pin 2A0 (through a resistor)A1
Pin 12Pin 4 Register select
Pin 11Pin 6 en
Pin 5Pin11 D4
Pin 4Pin 12 D5
Pin 3Pin 13 D6
VCCVEE Pin3 (Potentiometer)
GNDEn (pin 5)
Pin 2Pin 14 D7
VCCPin 15 LED +
GNDPin 16 LED –
Wiring connection description for irrigation system

Please find below a picture showing the vibration sensor and Arduino Uno wiring diagram:

connection diagram of the irrigation system
connection diagram of the irrigation system

Get the code for the irrigation system

Connect your PC to Arduino and open Arduino IDE. For the very first steps, you can refer to the Connecting Windows PC with Arduino tutorial. You can get the .ino code from my download area with the following link:

Code Explanation

Section 1: Before setup

In this section, we attach the pump, temperature sensor, moisture sensor, ultrasonic sensors and LCD pins. The threshold for each case limit is defined to compare it during the system work. It is convenient to see the limits at the start of the code to change it with time.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int pumpPin = 6;
const int temperatureSensorPin = A1;
const int moistureSensorPin = A0;
const int ultrasonicTrigPin = 7;
const int ultrasonicEchoPin = 8;

const int temperatureThreshold = 25;     // Temperature threshold in Celsius
const int moistureThreshold = 20;        // Moisture threshold
const int tanktotalheight = 100;     // Water level threshold (adjust as needed)
const int waterDistanceThreshold = 80;   // Water distance threshold in centimeters

Section 2: In the setup

In the setup, we set the baud rate of the serial monitor to 9600. Then, we initialised the sensor pin, and pump and displayed the “” on the first line, while the pumping system was on the second line. At the end cleard the screen to display more.

void setup() {
  lcd.begin(16, 2);
  pinMode(pumpPin, OUTPUT);
  pinMode(ultrasonicTrigPin, OUTPUT);
  pinMode(ultrasonicEchoPin, INPUT);
  lcd.setCursor(0, 0);
  lcd.setCursor(0, 1);
  lcd.print("Pump System");

Section 3: Loop section

This is the section for the main loop.

The code measures continuously the temperature, moisture level of the soil and the water level of the tank. The temperature pin is connected to the analogue input, we mapped the moisture level to a percentage and measured the water distance with the ultrasonic sensor. To show the result, LCD is displayed, to print the value of each measurement for monitoring.

void loop() {
  float temperature = -40 + 0.488155 * (analogRead(temperatureSensorPin) - 20);
  int moistureLevel = analogRead(moistureSensorPin);
  moistureLevel = map(moistureLevel, 0, 1023, 0, 100);
  // Read water distance from ultrasonic sensor
  long duration, distance;
  digitalWrite(ultrasonicTrigPin, LOW);
  digitalWrite(ultrasonicTrigPin, HIGH);
  digitalWrite(ultrasonicTrigPin, LOW);
  duration = pulseIn(ultrasonicEchoPin, HIGH);
  distance = duration * 0.034 / 2;
  lcd.setCursor(0, 0);
  lcd.print("C ");
  lcd.setCursor(0, 1);
  delay(1000); // Adjust delay according to your needs

At this stage, we control the pump based on the comparison of the values to the predefined threshold of temperature, moisture level and water distance. In the case of the condition met or exceeded the threshold the pump gets activated otherwise it remains turned off. The status of the pump gets displayed on the LCD when it gets turned ON or OFF.

  // Control pump based on conditions
  if (temperature > temperatureThreshold || moistureLevel < moistureThreshold || distance > waterDistanceThreshold) {
    // Conditions met, turn on pump
    digitalWrite(pumpPin, HIGH);
    lcd.setCursor(5, 0);
    lcd.print("Pump ON ");
  } else {
    // Conditions not met, turn off pump
    digitalWrite(pumpPin, LOW);
    lcd.setCursor(5, 0);
    lcd.print("Pump OFF");
  delay(1000); // Adjust delay according to your needs


The results demonstrate the condition of the temperature and the normal case when the temperature increases and the tank is available with the water then the pump turns on to moist the soil. The pump stops when the temperature is reduced to the limit.

Pump turn on based on the moisture sensor
Pump turn on based on the moisture sensor
water pumping during high temperature
water pumping during high temperature
pumping at low moisture
pumping at low moisture

What’s Next

Please find more tutorials on Arduino in peppe8o Arduino archives.


Umar Jamil

For any queries and help with work, please contact me at:
Whatsapp: +92-346-661-7017/ Link Fiverr for order custom work
Email: [email protected]

How useful was this post?

Click on a star to rate it anonymously!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?