The Capacitive Soil Moisture Sensor is widely used in smart farming and gardening projects. Unlike the resistive type, it does not corrode easily, making it more durable for long-term use. The sensor measures the dielectric constant of soil: wetter soil has a higher dielectric constant, which changes the sensor’s output voltage.
The sensor provides an analog output, which we can read using the ESP32’s ADC pin (GPIO 32 in our setup). By reading these values, we can determine whether the soil is dry, moist, or wet and then automate irrigation systems accordingly.
// Capacitive Soil Moisture Sensor with ESP32 DOIT DevKit v1
#define SOIL_PIN 32 // Analog pin connected to sensor
void setup() {
Serial.begin(115200);
Serial.println("Capacitive Soil Moisture Sensor with ESP32");
}
void loop() {
int soilValue = analogRead(SOIL_PIN); // Read analog value (0 - 4095)
Serial.print("Soil Moisture Value: ");
Serial.println(soilValue);
// Simple soil condition check
if (soilValue > 3000) {
Serial.println("Soil is DRY");
} else if (soilValue > 1500) {
Serial.println("Soil is Moist");
} else {
Serial.println("Soil is WET");
}
delay(2000); // Wait 2 seconds
}
Define Sensor Pin
#define SOIL_PIN 32
The sensor is connected to GPIO 32 (ADC pin).
Setup
Serial.begin(115200);
Initializes serial communication for monitoring.
Read Analog Value
int soilValue = analogRead(SOIL_PIN);
Reads soil moisture level as a raw ADC value (0–4095).
Print Values
Serial.print("Soil Moisture Value: ");
Serial.println(soilValue);
Displays sensor readings on Serial Monitor.
Soil Condition Check
if (soilValue > 3000) { ... }
Uses threshold values to categorize soil as Dry, Moist, or Wet.
Capacitive Soil Moisture Sensor with ESP32
Soil Moisture Value: 3500
Soil is DRY
Soil Moisture Value: 2200
Soil is Moist
Soil Moisture Value: 900
Soil is WET
Problem | Cause | Solution |
---|---|---|
Readings always high | Wrong wiring | Check VCC, GND, AO connections |
Fluctuating values | Poor power supply | Use stable 3.3V / external regulated supply |
Dry/Wet threshold not accurate | Soil type variation | Calibrate values for your soil |
No data in Serial Monitor | Wrong baud rate | Set Serial Monitor to 115200 |
Capacitive sensors don’t corrode and last longer, while resistive ones are cheaper but degrade over time.
Yes ✅, most modules support 3.3V–5V, but since we are using ESP32, 3.3V is recommended.
Yes ✅, sandy soil, clay, and loamy soil give different raw values. Calibration is required.
Yes, but it should be waterproofed or placed inside a casing for durability.
Yes ✅, by connecting a relay and water pump, you can build a smart irrigation system.