How Soil Moisture Sensor Works and Interface it with Arduino

Our next project is about “Smart Garden” in which we made a system that measures soil moisture and irrigates your plants automatically.
By using this system, you can water plant when they need and avoid over-watering or under-watering. You will need a soil moisture sensor to build this type of system.

How Soil Moisture Sensor works?

The working of the soil moisture sensor is very easy.
The fork-shaped probe with two exposed conductors, react as a variable resistor (just like a potentiometer). The resistance changes according to the water level in the soil.
This resistance is inversely proportional to the soil moisture:
• When the more water in the soil, conductivity will increase and resistance will decrease.
• When less amount of water in the soil, conductivity will decrease and resistance will increase.
The sensor gives the output voltage according to the resistance, by which we can measure the moisture level in soil.

Hardware Overview

A typical soil moisture sensor has two components.

The Probe

The sensor consist on a fork-shaped probe with two exposed conductors that goes into the soil where the water level is to be measured.
It works as a variable resistor whose resistance changes according to the soil moisture.

The Module

The sensor can also contain an electronic module that connects the probe with the Arduino.
This module generate output voltage according to the resistance of these two probe and read output from analog pin (A0).

A potentiometer is also built on with module for adjust the sensitivity. By using potentiometer you can set a threshold. So, when the moisture level is exceed from the adjusted value, the module will give output LOW otherwise HIGH.
This setup is very useful when you want to trigger an action when certain threshold is reached. For example, when the moisture level in the soil crosses a threshold, you can activate a relay to start pumping water.
The module has two LEDs. One is power LED and other is status LED. The Power LED will ON when the module is powered. When the digital output is LOW, status LED will ON.

Pinout of Soil Moisture Sensor

The soil moisture sensor has four pins to connect.

AO (Analog Output) pin provide us an analog signal and it is connected to one of the analog inputs on your Arduino.
DO (Digital Output) pin gives Digital output. Connect it to any digital pin on an Arduino or directly to a 5V relay or similar device.
VCC pin supplies power for the sensor. It is recommended to power the sensor with between 3.3V – 5V. Please note that the analog output will vary depending on what voltage is provided for the sensor.
GND is a ground connection. It is connected to the GND of Arduino.

Sensing Soil Moisture using Analog Output

The Soil moisture module provide both analog and digital outputs. So, first of all we will calculate soil moisture value by reading analog output.

Wiring

The wiring is very simple. Frist of all you need to connect the VCC pin to the 5V of Arduino. As we know that the module is very sensitive and cannot run continuously. So, we connect the VCC pin to any digital pin on Arduino. So, we can LOW and HIGH the module as per the requirement. The power usage by the module is 8 mA. So, it’s ok to connect the VCC pin to the pin # 7 on Arduino. Connect the GND pin of module to the GND pin on Arduino. At the end connect the AO pin of module to A0 on Arduino.

Calibration

It is recommended to measure the particular type of soil to get accurate reading. Different types of soil’s can effect the sensor.
You should see what readings you are actually getting from your sensor before you start storing data or triggering events
Use the below code to note the output values of sensor when the soil is as dry or when it is completely wet through with moisture.

// Sensor pins
#define sensorPower 7
#define sensorPin A0

void setup() {
	pinMode(sensorPower, OUTPUT);
	
	// Initially keep the sensor OFF
	digitalWrite(sensorPower, LOW);
	
	Serial.begin(9600);
}

void loop() {
	//get the reading from the function below and print it
	Serial.print("Analog output: ");
	Serial.println(readSensor());
	
	delay(1000);
}

//  This function returns the analog soil moisture measurement
int readSensor() {
	digitalWrite(sensorPower, HIGH);	// Turn the sensor ON
	delay(10);							// Allow power to settle
	int val = analogRead(sensorPin);	// Read the analog value form sensor
	digitalWrite(sensorPower, LOW);		// Turn the sensor OFF
	return val;							// Return analog moisture value
}

When you run this code, you’ll see the following readings in the serial monitor:
• when the soil was dry (~850)
• when the soil was completely wet (~400)

This test may take some trial and error. Once you get a good handle on these readings, you can use them as threshold if you intend to trigger an action.

Final Build

Based on the calibration values, the program below defines the following ranges to determine the status of the soil:


• < 500 is too wet

• 500-750 is the target range

• > 750 is dry enough to be watered

/* Change these values based on your calibration values */
#define soilWet 500   // Define max value we consider soil 'wet'
#define soilDry 750   // Define min value we consider soil 'dry'

// Sensor pins
#define sensorPower 7
#define sensorPin A0

void setup() {
	pinMode(sensorPower, OUTPUT);
	
	// Initially keep the sensor OFF
	digitalWrite(sensorPower, LOW);
	
	Serial.begin(9600);
}

void loop() {
	//get the reading from the function below and print it
	int moisture = readSensor();
	Serial.print("Analog Output: ");
	Serial.println(moisture);

	// Determine status of our soil
	if (moisture < soilWet) {
		Serial.println("Status: Soil is too wet");
	} else if (moisture >= soilWet && moisture < soilDry) {
		Serial.println("Status: Soil moisture is perfect");
	} else {
		Serial.println("Status: Soil is too dry - time to water!");
	}
	
	delay(1000);	// Take a reading every second for testing
					// Normally you should take reading perhaps once or twice a day
	Serial.println();
}

//  This function returns the analog soil moisture measurement
int readSensor() {
	digitalWrite(sensorPower, HIGH);	// Turn the sensor ON
	delay(10);							// Allow power to settle
	int val = analogRead(sensorPin);	// Read the analog value form sensor
	digitalWrite(sensorPower, LOW);		// Turn the sensor OFF
	return val;							// Return analog moisture value
}

The output on serial monitor is:

Sensing Soil Moisture using Digital Output

For our second experiment we will find the status of the soil by using the digital output.

Wiring

The wiring is similar as previous circuit. But this time we will DO pin of module to the digital pin # 4 on Arduino. And remove the AO pin of module.

Calibration

For calibrating the digital output (DO) the module has a built-in potentiometer.
Turn the knob of the potentiometer, you can set a threshold. So that when the moisture level exceeds the threshold value, the module output will LOW and the Status LED will ON.

To calibrate the sensor, insert the probe into the soil when your plant is ready to be watered and adjust the potentiometer clockwise so that the Status LED is ON and then adjust the potentiometer back anticlockwise until the LED goes OFF.
Your sensor is now calibrated and ready for use.

Arduino Code

// Sensor pins
#define sensorPower 7
#define sensorPin 8

void setup() {
	pinMode(sensorPower, OUTPUT);

	// Initially keep the sensor OFF
	digitalWrite(sensorPower, LOW);

	Serial.begin(9600);
}

void loop() {
	//get the reading from the function below and print it
	int val = readSensor();
	Serial.print("Digital Output: ");
	Serial.println(val);

	// Determine status of our soil moisture situation
	if (val) {
		Serial.println("Status: Soil is too dry - time to water!");
	} else {
		Serial.println("Status: Soil moisture is perfect");
	}

	delay(1000);	// Take a reading every second for testing
					// Normally you shoul take reading perhaps every 12 hours
	Serial.println();
}

//  This function returns the analog soil moisture measurement
int readSensor() {
	digitalWrite(sensorPower, HIGH);  // Turn the sensor ON
	delay(10);              // Allow power to settle
	int val = digitalRead(sensorPin); // Read the analog value form sensor
	digitalWrite(sensorPower, LOW);   // Turn the sensor OFF
	return val;             // Return analog moisture value
}

The output on serial monitor is

Leave a Reply

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

Facebook
YouTube
× Contact us