Soil Moisture Sensor with Arduino – Automatic Plant Irrigation System.

Soil moisture sensor

A soil moisture sensor is one of the components needed in a smart garden to monitor the amount of water in soil. In this tutorial I will give details on the working of this sensor and how to connect it to Arduino to make an automatic plant irrigation system.

Soil Moisture Sensor Hardware Overview.

The sensor contains a fork-shaped probe with two exposed conductors that goes into the soil. The sensor is connected to an electronic module for converting the signals from the probe into analog and digital values that can be read by a microcontroller like Arduino.

Soil moisture sensor pinout

The electronic module produces an output voltage depending on the resistance of the probe. This is read at the Analog Output pin AO.

The LM393 High Precision Comparator is for converting the analog signal to to digital form which is output at the Digital Output pin DO.

  • VCC – supplies power for the sensor. It is recommended to power the sensor with between 3.3V – 5V.
  • GND – ground connection
  • AO – This is the Analog Output pin and will be connected to one of the analog inputs on your Arduino. This value varies depending on what voltage is supplied to the sensor.
  • DO – Digital Output pin which you can connect to any digital pin on an Arduino or directly to a 5V relay.

There is a sensitivity adjustment potentiometer for setting a threshold value for moisture at the digital output DO so that when the moisture level exceeds the threshold value, the module will output LOW otherwise it will remain HIGH.

The module also has a Power LED which turns on when the module is powered and a Status LED that lights up when the digital output goes LOW.

How does a soil moisture sensor work.

The fork-shaped probe with two exposed conductors acts as a variable resistor whose resistance varies inversely proportional to the moisture content in the soil. The more water available in soil gives better conductivity which results in a lower resistance and the less the water in soil the lower the conductivity and hence a higher resistance.

The soil moisture sensor produces an output voltage depending on the resistance of the probe which is then used to determine the moisture level in soil.

Connecting the soil moisture sensor to Arduino.

Connecting a soil moisture sensor to Arduino is simple. The GND pin is connected to GND of Arduino and the analog output AO is connected to Arduino pin A0 as shown below.

The VCC pin on the electronic module can be connected to 5V on the Arduino. However if the soil moisture sensor is powered up all the time, the probe will eventually dissolve due to electrolysis reducing it’s lifespan significantly. This is a very common challenge with this type of sensors.

To overcome the above problem, it is recommended to connect the VCC pin to a digital pin of Arduino so that the probe is only powered when take readings by setting the digital pin HIGH or LOW depending on your requirements.

Calibration of the Soil Moisture sensor

Before using a soil moisture sensor, you need to first calibrate it using the type of soil that you are going to monitor. This is because different types of soil have different water retention properties which can affect the sensitivity of your sensor if not properly calibrated.

After connecting the soil moisture sensor to Arduino as detailed in the previous section, you can use the code sketch below and place the sensor in different samples of the soil with varying levels of water.

#define sensorPower 6
#define sensorPin A0

void setup() {
	pinMode(sensorPower, OUTPUT);
	digitalWrite(sensorPower, LOW);
	Serial.begin(9600);
}

void loop() {
	Serial.print("Analog output: ");
	Serial.println(readSensor());
	delay(1000);
}

int readSensor() {
	digitalWrite(sensorPower, HIGH);	
	delay(10);							
	int val = analogRead(sensorPin);	
	digitalWrite(sensorPower, LOW);		
	return val;					
}

The code above will output analog values from the sensor on the serial monitor. You can then note the values when the soil is dry and when it is completely saturated with moisture then choose a threshold value which will later be used as a trigger for an action.

Automatic Plant Irrigation System Using Soil Moisture Sensor And Arduino.

One factor that has to be put into consideration in smart farming is monitoring of soil moisture content so that plants have the optimum water needed for their growth. A soil moisture sensor can be used with a microcontroller to achieve an automatic plant watering system.

The setup below shows how a soil moisture sensor can be connected to Arduino and a submersible water pump to achieve an automatic irrigation system.

The water pump is connected to Arduino digital pin 3 using a 5V relay module since this submersible water pump is supplied from a different power source of 5~12V.

The code for the automatic plant irrigation system using a soil moisture sensor and Arduino is given below.

#define pump 3
#define sensorpin A0
#define threshold 700
#define waterdelay 750
#delay waterpostdelay 5000

void setup(){
     pinMode(sensorPin, INPUT);
     pinMode(pump, OUTPUT);
}

void loop(){
     //if soil is too dry start watering for 3/4 a second then
     //wait for 5 seconds before monitoring again
     int SensorValue = analogRead(sensorpin);
     if(SensorValue >= threshold){
             digitalWrite(pump, HIGH);
             delay(waterdelay);
             digitalWrite(pump, LOW);
             delay(waterpostdelay); 
     }
     delay(50);
}

Code description

First we define the pins where the water pump and soil moisture sensor are attached as 3 and A0 respectively. Then the threshold soil moisture value got during calibration of the sensor. In my case I used 700.

Then define the time delays for how long the pump should be turned on to water the plants and how long to take before monitoring again to check if the plants need watering.

In the setup section, the sensor pin is initialed as an input and the pump as output

In the loop section, the analogRead() method is used to first read the signal from the soil moisture sensor and then compare the sensor reading to the threshold value. If the value is less than the threshold, then the pump is turned on to water the plants for a given period of time otherwise the pump is off for some time before again monitoring the state of soil moisture.