Interfacing Capacitive Soil Moisture Sensor with ESP32 DOIT DevKit v1

adminESP3213 hours ago7 Views

Introduction

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.

Components Required

  • ESP32 DOIT DevKit v1
  • Capacitive Soil Moisture Sensor
  • Breadboard & jumper wires
  • USB cable

Circuit Diagram

  • Sensor VCC → 3.3V
  • Sensor GND → GND
  • Sensor AO (Analog Output) → GPIO 32

Arduino Code

// 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
}

Step-by-Step Code Explanation

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.

    Output

    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
    

    Real-Life Applications

    • Smart Irrigation Systems → Automatically water plants when soil is dry.
    • Greenhouse Monitoring → Maintain soil moisture for crops.
    • Home Gardening → Monitor houseplants with IoT dashboards.
    • Agriculture IoT Projects → Send soil data to the cloud for analysis.

    Troubleshooting

    ProblemCauseSolution
    Readings always highWrong wiringCheck VCC, GND, AO connections
    Fluctuating valuesPoor power supplyUse stable 3.3V / external regulated supply
    Dry/Wet threshold not accurateSoil type variationCalibrate values for your soil
    No data in Serial MonitorWrong baud rateSet Serial Monitor to 115200

    What is the difference between capacitive and resistive soil moisture sensors?

    Capacitive sensors don’t corrode and last longer, while resistive ones are cheaper but degrade over time.

    Can I power the sensor with 5V?

    Yes ✅, most modules support 3.3V–5V, but since we are using ESP32, 3.3V is recommended.

    Do soil types affect readings?

    Yes ✅, sandy soil, clay, and loamy soil give different raw values. Calibration is required.

    Can I use this sensor outdoors?

    Yes, but it should be waterproofed or placed inside a casing for durability.

    Can I automate irrigation with this sensor?

    Yes ✅, by connecting a relay and water pump, you can build a smart irrigation system.

    Leave a reply

    Loading Next Post...
    Follow
    Search Trending
    Popular Now
    Loading

    Signing-in 3 seconds...

    Signing-up 3 seconds...