// Simple code for Measuring Voltage from
// Capacitive soil moisture sensor
//
int soil_pin = A0; // AOUT pin on sensor
void setup() {
Serial.begin(9600); // serial port setup
analogReference(EXTERNAL); // set the analog reference to 3.3V
}
void loop() {
Serial.print("Soil Moisture Sensor Voltage: ");
Serial.print((float(analogRead(soil_pin))/1023.0)*3.3); // read sensor
Serial.println(" V");
delay(100); // slight delay between readings
}
No external library is required to use this sensor.
const int AirValue = 600;
const int WaterValue = 350;
int soilMoistureValue = 0;
int soilMoisturePercent = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
soilMoistureValue = analogRead(A0);
Serial.println(soilMoistureValue);
soilMoisturePercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
if (soilMoisturePercent >= 100) {
Serial.println("100 percent");
} else if (soilMoisturePercent <= 0) {
Serial.println("0 percent");
} else {
Serial.print(soilMoisturePercent);
Serial.println(" percent");
}
delay(250);
}
Measure the weight of an empty container and fill it with dry soil up to 200 ml. Record the dry soil weight. Add 10 ml of water mix thoroughly and refill to 200 ml. Record the new weight. Repeat the process until the soil becomes saturated. Spread the soil thinly to dry and record the final dry mass. These measurements are used to calibrate the sensor accurately.
Capacitive soil moisture sensors provide better accuracy and longer lifespan compared to resistive sensors by avoiding probe corrosion. They measure dielectric changes in the soil rather than electrical resistance which can be affected by fertilizer and soil composition. Compared to combined soil moisture modules capacitive sensors provide only analog output but offer improved reliability and durability.