Learn how to use a soil moisture sensor with Arduino to maintain the ideal moisture levels for healthier plants.

how to operate soil moisture sensor

The soil moisture sensor is the most amazing sensor used to measure the quantity of water present in the soil and defines the moisture level of the soil as the output. Because of its amazing operating principle, this sensor is used in applications such as automatic irrigation systems, agriculture research applications, landscape irrigation, indoor planting systems, and lawns. I have also made an advanced-level project “Smart Plant Watering System” using a soil moisture sensor.

In this article, I will explain to you some basic fundamentals that will help you to understand the workings of the soil moisture sensor. The soil moisture sensor can be operated in both analog and digital modes. Here I will explain to you how soil moisture sensors can be operated in both modes.  

Interfacing soil moisture sensor with Arduino

Soil moisture sensor contains two parts one is the sensor part and the other one is the control board. The sensor part is made up of two conductive probes which are used to measure the moisture content in the soil. The Control board contains LM393 IC (voltage comparator), power LED, output LED, and potentiometer such as important components that help conductive probes to measure the moisture level of the soil.

Data Transmission Modes

Data transmission modes in soil moisture sensors refer to how these sensors convey information about soil moisture levels to external systems or devices. The data transmission modes typically depend on the type of soil moisture sensor you’re using and the communication capabilities it offers. Here are the common data transmission modes with Arduino:

1. Analog mode

In this mode, you can connect analog soil moisture sensors to one of the Arduino Uno’s analog input pins (A0 to A5). Arduino reads the analog voltage output from the sensor using its built-in analog-to-digital converter (ADC). You can then convert this analog reading into a soil moisture value and use it in your project.

In this mode, I will show the real-time soil moisture sensor values on the serial monitor. In this project, you will learn how sensor value varies with the change in the moisture level of the soil.

Required components

Connection diagram

How to operate soil moisture sensor with Arduino

Circuit Description

According to the connection diagram, the Vcc pin of the soil moisture sensor is connected to the 5V power supply pin of the Arduino board. The GND pin of the soil moisture sensor is connected to one of the GND pins of the Arduino board. The A0 analog pin of the soil moisture sensor is connected with the analog pin A2 of the Arduino board.

Code

In analog mode, I used the analog pin of the sensor instead of the digital pin. In this mode, the sensor provides values from 0 to 1023 and these values will be shown on the serial monitor. As the soil is dry then we will get higher values. When we add some water to it due to which soil will get wet then we will achieve lower values on the serial monitor.

/*
*How to operate soil moisture in analog mode sensor using Arduino Uno   create on: 25/2/2019
*
*for more detail please visit-https://arduinounomagic.com/2019/02/how-to-operate-soil-moisture-sensor.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com
*/
const int sensor_input=A2;// Connect sensor at analog pin A2

int sensor_value; // variable to store sensor value

void setup()
{
  Serial.begin(9600);// Initalize serial communication
}

void loop()
{
  sensor_value=analogRead(sensor_input);//Read sensor data
  Serial.print("sensor moisture value="); // Print information on serial monitor
  Serial.println(sensor_value); // Display sensor value

  delay(1000); //delay
  
}

Output video

We can also measure the moisture level of the soil in percentage using the map function with the same connection circuit. In this case, I have mapped the values of the soil moisture sensor between 0 to 100 to calculate the moisture level as a percentage.

While using the analog mode experiment I got 840 values on the serial monitor for dry soil and 120 value for wet soil. I used these values in the map function to calculate the moisture level in percentage. These values may vary according to the soil. You can change them according to your needs. 

Code to calculate sensor values in percentage (click here)

/*
*How to operate soil moisture sensor in digital mode using Arduino Uno   create on: 25/2/2019
*
*for more detail please visit-https://arduinounomagic.com/2019/02/how-to-operate-soil-moisture-sensor.html
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*/
int sensor_input=7;// Connect sensor at analog pin A2
int BUZZER=8; // connect buzzer at pin 8

void setup()
{
  pinMode(sensor_input, INPUT); //set sensor as input
  pinMode(BUZZER, OUTPUT); //set buzzer as output 
}

void loop() //if sensor value is high only than operate buzzer other wise turn off the buzzer 
{
  if(digitalRead(sensor_input)==HIGH)

  {
    digitalWrite(BUZZER, HIGH);
  }

  else
  {
    digitalWrite(BUZZER, LOW);
  }

  delay(1000); //delay
  
}

2. Digital mode

Some soil moisture sensors provide digital output pins that change from LOW to HIGH (or vice versa) based on a predefined moisture threshold. You can connect these sensors to any of the Arduino Uno’s digital input pins (D2 to D13). Arduino can read the digital signal to determine whether the soil moisture level has crossed the threshold.

In digital mode, the soil moisture sensor will provide only two sensor output values HIGH or LOW (1 or 0). In this mode, I will operate the buzzer with the help of sensor values.

Required components

Connection diagram

soil2Bmoisture2Bsensor2Bwith2Bdigital2Bmode How to operate soil moisture sensor with Arduino: 2 modes

Circuit Description

According to the circuit diagram, the VCC and GND pins of the soil moisture sensor are connected to the 5V power supply and GND pins of the Arduino Uno board respectively. Digital pin D0 of the soil moisture sensor is connected with digital pin 8 of the Arduino Uno board.

The buzzer is a two-terminal device one is longer and another one is shorter. The longer pin is connected to the output pin 8 and the shorter pin is connected to the GND pin of the Arduino board.

Code

/*
*How to operate soil moisture sensor in digital mode using Arduino Uno   create on: 25/2/2019
*
*for more detail please visit-https://arduinounomagic.com/2019/02/how-to-operate-soil-moisture-sensor.html
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*/
int sensor_input=7;// Connect sensor at analog pin A2
int BUZZER=8; // connect buzzer at pin 8

void setup()
{
  pinMode(sensor_input, INPUT); //set sensor as input
  pinMode(BUZZER, OUTPUT); //set buzzer as output 
}

void loop() //if sensor value is high only than operate buzzer other wise turn off the buzzer 
{
  if(digitalRead(sensor_input)==HIGH)

  {
    digitalWrite(BUZZER, HIGH);
  }

  else
  {
    digitalWrite(BUZZER, LOW);
  }

  delay(1000); //delay
  
}

According to the code I have used the soil moisture sensor as an input device and the buzzer as the output device. Sensor values are compared with the specified threshold value (HIGH). If the sensor value is equal to the threshold value then the buzzer will generate a beep sound otherwise buzzer will be off.

This concept can be used in interior home-based plant watering systems. If the soil is dry then the buzzer will generate a beep sound and remind us to water the plant. As we provide water to the plant then the sensor will observe low value and stop the buzzer automatically. I have made an advanced-level project using a soil moisture sensor working in digital mode.

Hope you like the article.

If you have any queries, please let me know in the comments.

2 Comments

  1. I had already provided a separate article to understand the basics of LCD module (https://www.arduinounomagic.com/2019/01/how-to-use-lcd-with-arduino-uno.html?m=1). You will find solution for your issue after understanding the basics of LCD.

    I had also provided the article named "How to use temperature sensor with Arduino Uno" (https://www.arduinounomagic.com/2019/01/how-to-use-temperature-sensor-with.html?m=1) in which you will find all the basic concepts.

    Hope these articles will help you.

  2. I have gone through this article and its code with interest; I want to incorporate a pH (atlas Scientific pH sensor)and Temperature and humidity Sensor probe DHT 22 with LCD readout so that the parameters can be displayed. How to incorporate in the above programme ? Can some one help me with codes so that I can try to automate my kitchen garden (presently watered by manually by me). I have with me the soil moisture sensor, DHT22,16×2 LCD module( bought for distance sensor) with the Arduino R3 board ; only I have to go in for the atlas scientific pH sensor.
    Please kindly help me (I am a retired electronic teacher ans service personal ).
    V.Ramakrishnan
    [email protected]
    +91 8105843368

Leave a Reply

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