Capacitive soil moisture sensors measures soil moisture levels by capacitive sensing rather than resistive sensing like the more common soil moisture sensors on the market. There are different brands, for instance DFROBOT SKU SEN0 193
const int AirValue = 620; //you need to replace this value with Value_1
const int WaterValue = 310; //you need to replace this value with Value_2
int soilMoistureValue = 0;
int soilmoisturepercent=0;
void setup() {
Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop() {
soilMoistureValue = analogRead(A2); //put Sensor at A2 port
Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent > 100)
{
Serial.println("100 %");
}
else if(soilmoisturepercent <0)
{
Serial.println("0 %");
}
else if(soilmoisturepercent >0 && soilmoisturepercent < 100)
{
Serial.print(soilmoisturepercent);
Serial.println("%");
}
delay(250);
}
Project with pin power
An alternative wiring is to put the power supply on a digital pin, allowing turning the power to the sensor on/off.
int probePowerPin = 7;
const int AirValue = 620; //you need to replace this value with Value_1
const int WaterValue = 310; //you need to replace this value with Value_2
int soilMoistureValue = 0;
int soilmoisturepercent=0;
void setup ()
{
Serial.begin(9600);
pinMode (probePowerPin, OUTPUT); // define the digital output
}
void loop () {
// turn on external probe power
digitalWrite (probePowerPin, HIGH); // Turn external probe power On
soilMoistureValue = analogRead(A1); //put Sensor at A1 port
Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent > 100)
{
Serial.println("100 %");
}
else if(soilmoisturepercent <0)
{
Serial.println("0 %");
}
else if(soilmoisturepercent >0 && soilmoisturepercent < 100)
{
Serial.print(soilmoisturepercent);
Serial.println("%");
}
delay(250);
}