Logo

Interfacing Soil Moisture Sensor with ESP32

What is a Soil Moisture Sensor?

A soil moisture sensor measures the water content in the soil. It’s widely used in smart irrigation systems, agriculture, and environmental monitoring. The sensor typically has two probes to detect moisture levels by measuring electrical conductivity in the soil.

Working Principle of Soil Moisture Sensor

When water is present in the soil, it conducts electricity between the sensor’s probes. The sensor outputs an analog or digital signal depending on the model. Dry soil gives low conductivity (high resistance), while wet soil offers high conductivity (low resistance). The ESP32 reads this signal to estimate soil moisture.

  • Power the soil moisture sensor with 3.3V or 5V.
  • Insert the sensor probes into the soil.
  • Read the analog value from the sensor's AOUT pin using the ESP32.
  • Convert the analog value to a moisture percentage.
  • Trigger actions like turning on a pump or sending alerts based on the reading.

Formula: Moisture (%) = map(analogValue, dryValue, wetValue, 0, 100)

Components Required

  • ESP32 board
  • Soil Moisture Sensor (analog type)
  • 10KΩ resistor (optional, for stability)
  • Breadboard and jumper wires
  • USB cable

Pin Configuration of Soil Moisture Sensor

  • VCC: Connects to 3.3V or 5V on ESP32 (check sensor specs)
  • GND: Connects to ESP32 ground
  • AOUT: Analog output that varies with soil moisture
  • DOUT (optional): Digital output for threshold detection (not used in this project)

Always calibrate the sensor readings for accurate soil moisture mapping in your specific soil type.

Wiring Soil Moisture Sensor to ESP32

  • VCC -> 3.3V (or 5V if supported)
  • GND -> GND
  • AOUT -> GPIO 34

Arduino Code for ESP32 + Soil Moisture Sensor

1const int sensorPin = 34;
2int sensorValue = 0;
3
4void setup() {
5  Serial.begin(115200);
6}
7
8void loop() {
9  sensorValue = analogRead(sensorPin);
10  int moisturePercent = map(sensorValue, 4095, 0, 0, 100);
11  Serial.print("Soil Moisture: ");
12  Serial.print(moisturePercent);
13  Serial.println(" %");
14  delay(1000);
15}

Code Explanation (Line-by-Line)

  • const int sensorPin = 34;: Sets GPIO 34 as the analog input pin to read moisture sensor output.
  • analogRead(sensorPin);: Reads raw analog value representing soil moisture level.
  • map(sensorValue, 4095, 0, 0, 100);: Converts the raw analog value to a percentage (0–100%).
  • Serial.print(...): Displays the moisture percentage on Serial Monitor.

Applications

  • Smart irrigation systems
  • Home gardening automation
  • Agricultural monitoring
  • Environmental research
  • Soil testing in remote locations

Conclusion

By interfacing a soil moisture sensor with ESP32, you can monitor soil wetness in real-time and automate irrigation based on actual moisture levels. This simple yet powerful setup helps build smart agriculture projects with accurate water usage.