Calibration for capacitive soil moisture sensor

Hello everyone. I´m creating an automated irrigation system with capacitive soil mositure sensors v2.0

I´m having a lot of trouble with the calibration and Im not sure if the problem is the length of wire that Im using or the sensors themselves, the code, the method for calibration etc. The way I´m doing it is by getting the analog reading when the sensor is in dry soil and when it is in very wet soil, then I´m using the map function to scale those numbers from 0 to 100. Initially the calibrated values seem right, but suddenly the value that i end up getting when the sensor is in wet soil is accurate, but when it is in dry soil instead of 0 i get numbers like -30. This is the code that I´ve been using.

//Se conectan los sensores a los pines 36, 39, 34 y 35
int sensor1=36;
int sensor2=39;
int sensor3=34;
int sensor4=35;


void setup() {
  Serial.begin(115200);
pinMode(sensor1,INPUT);
pinMode(sensor2,INPUT);
pinMode(sensor3,INPUT);
pinMode(sensor4,INPUT);


}

void loop() {
int humedad1=analogRead(sensor1); 
int humedad2=analogRead(sensor2);  
int humedad3=analogRead(sensor3);  
int humedad4=analogRead(sensor4);  

int porcentaje1=map(humedad1,2300, 1300, 0, 100);
int porcentaje2=map(humedad2,2000, 1230, 0, 100);
int porcentaje3=map(humedad3,1925, 1200, 0, 100);
int porcentaje4=map(humedad4,2045, 1320, 0, 100);

Serial.println(porcentaje1);
Serial.print("\t");
Serial.print(porcentaje2);
Serial.print("\t\t");
Serial.print(humedad3);
Serial.print("\t\t\t");
Serial.print(humedad4);
Serial.println();
delay(500);
}

Because of where I need to place the controller, an ESP32, the distance between the controller and the sensors would be about 12 meters (around 40ft ) and 6 meters (20ft) so thats the wire length that I´m using now and its an 18 AWG. I would really appreciate if someone could help me figure out whats going on. Thank you in advance :slight_smile:

Long wires act as antennas, and usually lead to problems in several different forms. You may need shielding, etc.

For informed help, post a link to the exact sensor you are using, and a hand drawn wiring diagram.

Keep in mind that soil moisture is very difficult to measure electronically and no sensor is very accurate. The very cheap ones with two prongs are utterly worthless. At best you will have only a crude approximation to the truth.

A relatively accurate method of measuring "available" water content is to measure the weight loss upon baking the soil at high temperatures, until the weight stops changing. Some water is always retained.

1 Like

More details on everything............

https://www.reddit.com/r/arduino/comments/q1anwt/beware_of_faulty_capacitive_soil_moisture_sensors/

The wire is shielded with multiflex TFFN/PVC. This is the sensor Im using https://www.amazon.es/Capacitive-Moisture-Corrosion-Resistant-Raspberry/dp/B07FLR13FS but I didn´t buy it from this link, i bought it in a store that doesn´t have a webpage.

This is the diagram of my circuit, the grey wire is for 3,3V and the red one for 5V

I checked for the things they mention in that post and in the videos that the linked before buying the sensors to make sure they weren´t faulty so technically they should be fine but I´m still having problems

Calibrate each sensor individually, with nothing else connected, several times using different soil samples, so you get a feeling for the variations in behavior.

Only then consider connecting up the rest of that problematic circuitry, with household AC, motors and relays.

1 Like

I see that dry soil gives A/D values of around 2000, so it's not bad if it's off by 30.
You can add a constrain() to the map function, so it doesn't get outside the 0-100 range.
Leo..

int porcentaje2 = map(humedad2, 2000, 1230, 0, 100);
porcenta2 = constrain(porcenta2, 0, 100);

Hello Natalia1400

Take a view here to gain the knowledge:

hth

Have a nice day and enjoy coding in C++.

1 Like

The problem is that everytime the moisture goes up and then down again those values change for some reason. It was 2000 but now its showing around 1800

That is how those sensors behave. They are neither accurate nor reliable, but are somewhat better than nothing.

Hi, @Natalia1400

Can you please post some images of your calibration setup?

How are you measuring the moisture to calibrate the sensor?

How do you know after increasing the moisture, that you have returned to the previous level?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I have a cup with dry soil, and another one with very wet soil. I dont have any reference device to calibrate the sensor. After I put the sensor in the wet soil, to return to what should be the dry value, I take the sensor out, I get it dry it with a tissue and then I put it back in the dry soil.

What sort of variation do you see when you remove the sensor from the sample, and put in back in the same sample?

What happens when you pack the soil around the sensor?

Repeat a few times to get a feeling for the spread of values.

1 Like

Hi,
Do you wipe the sensor dry before testing in each sample?

Do you insert the sensor to the same depth on the sensor when you try each sample?

I have some of those sensors and what annoys me is no conformal coating on the sensor circuit to isolate the circuit against humidity from the soil.

The circuit measures pF, so even breathing on the sensor circuit will possibly change the reading.

Tom.. :grinning: :+1: :coffee: :australia:

Its not always the same, sometimes the value that i was getting becomes higher and sometimes lower. That´s whats so frustrating. I do notice that the readings are different if the sensor is burried even a little bit deeper or if its more superficial, but still, even if i put the sensor back the same way it was the values keep changing too much.

yes, i wipe it and always put it back at the same depth

What you are doing is called "the scientific method", and is the best way to proceed.

If the sensor does not give repeatable measurements, it is simply not very useful. Hopefully, lesson learned.

What you're doing can't give accurate results, because soil doesn't compact the same against the sensor every time. Just calibrate 0-100 with a dry sensor (not in soil) and a wet sensor (in water).
Then learn at what percentage you should water your plants.
Be happy with three or four moisture stages. Dry/ok/wet.

Yep, a damaged silk screen could also change sensor readings.
Leo..

Ooh you´re right, I hadn´t really thought about the way the soil compacts. But I still have to make a scale from 0 to a 100 because I have to water the crops at a specific percentage for the proyect.