Smart Farming : ESP32 with Soil Moisture Sensor
How Soil Moisture Sensor Works and Interface it with Esp32
When you hear the term “smart garden,” one of the first things that might come to mind is a setup that automatically keeps your plants healthy—measuring how dry the soil is and watering plants only when they need it. No more guessing, no more soggy roots, and no more thirsty plants!
With the help of a simple Soil Moisture Sensor, you can build a system that knows exactly when your garden needs water and takes care of it. Whether you’re just curious about automation or planning to build a complete smart irrigation system, learning how to hook up and use a soil moisture sensor with Arduino is a great place to start.
In this tutorial, we’ll guide you through everything you need to know—from wiring to reading sensor data—so you can start building your own plant-friendly tech today.
Hardware Overview
A soil moisture sensor consists of two main parts: the sensor probes and the LM393 comparator module.
The Probe
The sensor has two large, exposed metal pads that function as probes. You stick these probes into the soil to measure moisture.

These probes work as a variable resistor, which means their resistance changes depending on how wet or dry the soil is. When the soil is wet, the resistance is lower. When the soil is dry, the resistance is higher.![]()
The LM393 Comparator Module
Along with the probes, the sensor includes an LM393 comparator module that provides both digital and analog outputs.

The analog output (AO pin) comes directly from the changing resistance of the probes. The wetter the soil, the lower the resistance and the lower the signal at this pin.
This analog signal also goes to the LM393 comparator, which compares it to a reference voltage set by a potentiometer on the module.

- If the soil is drier than the set level, the comparator gives a HIGH digital output signal at the digital pin (DO).
- If the soil is wetter than the set level, it outputs a LOW digital signal.
This digital output can be used to trigger actions—like turning on a relay to water plants when the soil gets too dry.
You can change the moisture level threshold by turning the potentiometer. Turn it clockwise to increase the threshold (requiring drier soil to trigger) and counterclockwise to decrease the threshold (triggering when the soil is less dry).

The module also has two indicator LEDs:
- The Power LED lights up when the module receives power
- The Status LED turns on when the moisture level goes above the threshold

How Does a Soil Moisture Sensor Work?
The soil moisture sensor measures how much water is in the soil by checking the electrical resistance between the two metal probes placed in the soil.
When the sensor is powered (through its VCC and GND pins), a small electric voltage is applied to these probes.
- In dry soil: The soil has high resistance, making it difficult for electricity to flow between the probes.
- In wet soil: The soil contains more water, which helps electricity flow more easily. This means the soil has low resistance, and more current can flow between the probes.
The sensor detects this change in resistance and converts it into an electrical signal (voltage).
The module processes this signal in two ways:
Analog Output (AO pin): It provides a continuous voltage that corresponds to the moisture level. You can connect this to a microcontroller like Arduino to get a detailed reading of how wet or dry the soil is.
Digital Output (DO pin): The module compares the analog output to the threshold set by the potentiometer. If the soil moisture is lower (dry) than the threshold, the module’s digital output (DO) goes HIGH. If it’s higher (wet), it goes LOW.
Soil Moisture Sensor Pinout
The soil moisture sensor is extremely simple to use and only requires four pins to connect.
AO is the analog output pin. This pin gives a variable voltage that corresponds to the moisture level in the soil—the wetter the soil, the lower the output voltage. You connect this to an analog input pin on the Arduino (like A0).
DO is the digital output pin. This pin outputs LOW when the soil moisture exceeds the threshold value set by the potentiometer, and HIGH otherwise. You connect this to a digital input pin on the Arduino.
VCC pin provides power to the sensor. It’s best to power the sensor with 3.3V to 5V. Keep in mind that the analog output value will change based on the voltage you supply.
GND is the ground pin.
Experiment 1 – Measuring Soil Moisture using Analog Output (AO)
In our first experiment, we’re going to measure how much moisture is in the soil by reading the analog output from the sensor.
Wiring
Let’s start by connecting the soil moisture sensor to the Arduino.
First, connect the VCC pin on the sensor module to the 5V pin on the Arduino, and connect the GND pin on the sensor to one of the GND pins on the Arduino.
However, there’s an important thing to keep in mind. A common issue with these sensors is that they don’t last very long because they are always in contact with moisture. In addition, if the sensor is constantly powered while inserted in the soil, it tends to corrode more quickly.
To avoid this problem, it’s a good idea to power the sensor only when you need to take a reading. A simple way to do this is by connecting the sensor’s power pin to one of the Arduino’s digital output pins, and then turning that pin HIGH (on) or LOW (off) in your code as needed.
So instead of connecting the VCC pin directly to 5V, we’ll connect it to digital pin #7 on the Arduino. This way, the Arduino can control when the sensor is powered.
Finally, connect the AO pin of the sensor to the A0 analog input pin on the Arduino.
Here’s a quick reference table for the pin connections:
HOW TO OPERATE
// #include <LiquidCrystal_I2C.h>
#define MOI 15
// LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
pinMode(MOI,INPUT);
Serial.begin(9600);
Serial.println("MOISTURE START");
// lcd.init();
// lcd.backlight();
// lcd.setCursor(3,0);
// lcd.print("moisture data");
// lcd.setCursor(3, 1);
}
void loop() {
int data=analogRead(MOI);
Serial.println(data);
// lcd.print(data);
delay(1000);
// lcd.clear();
}

Leave a Reply
You must be logged in to post a comment.