Skip to main content

Capacitive Soil Sensor - How to Use

This page provides basic usage instructions for the Capacitive Soil Sensor, including how to connect it to your microcontroller (e.g., Dasduino Connect Plus), and how to write a simple sketch to start reading soil moisture levels.


How to Connect​

The sensor has three pins:

PinFunctionDescription
VCCPowerConnect to 3.3V or 5V on your board.
GNDGroundConnect to the ground of your board.
AOUTAnalog OutputConnect to an analog-capable GPIO pin on your board.
ℹ️

Always use a pin capable of analog input (ADC). On ESP32-based boards like Dasduino Connect Plus, we recommend using GPIO 32 or 33.


Use ADC1 channels to avoid Wi-Fi interference. Below are the safe pins for analog input:

GPIOADC ChannelNotes
32ADC1_CH4Recommended
33ADC1_CH5Recommended
34ADC1_CH6Input only (safe)
35ADC1_CH7Input only (safe)
36ADC1_CH0Input only (safe)
39ADC1_CH3Input only (safe)
⚠️

Avoid using ADC2 pins when Wi-Fi is active, as it causes conflicts and unreliable readings.


Example Code​

Here’s a simple sketch to read analog soil data using a safe ADC1 pin:

const int sensorPin = 33; // Use a safe ADC1 pin like GPIO 32 or 33

void setup() {
Serial.begin(115200);
}

void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Soil Moisture Reading: ");
Serial.println(sensorValue);
delay(1000);
}

analogRead(pin)

Reads the analog voltage on the specified pin and returns a value from 0 to 4095 on ESP32 boards.

Returns value: Returns an integer analog value between 0 (dry) and 4095 (wet).

Function parameters:

TypeNameDescription
intpinThe GPIO number connected to the sensor’s analog output.

Calibration Tip​

You may wish to record the minimum and maximum values observed for your specific soil and water conditions, and map them into a percentage:

int value = analogRead(sensorPin);
int percentage = map(value, 1200, 3200, 0, 100); // Adjust based on your readings
percentage = constrain(percentage, 0, 100);
Serial.print("Soil Moisture: ");
Serial.print(percentage);
Serial.println("%");

Next Steps​

  • Try logging the data to an SD card or over Wi-Fi.
  • Create alerts or automatic watering based on moisture thresholds.
  • Integrate the sensor into a full smart gardening project!