Soil Moisture Sensor corroded

Hello,
I'm new here and have a question about a problem. Last year I built an irrigation system that works fine.
Unfortunately, as already described here, the humidity sensor decomposes because there is voltage all the time. I have already replaced it with stainless steel.
Now I have read here as a solution: "Another sollution : switch on the power of the sensor only when you need it. I mean : switch on 5 seconds before you "analogRead". Switch of right after the reading." Unfortunately it was not discussed any further.
Enclosed is my sketch for my two sensors. How could I solve the problem of checking only every two hours and not applying any voltage in between?

int Sensor = A0;  //Senor pin
int Sensor2 = A1; //Senor pin
 
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
 Sensor=analogRead(A0);

  Serial.print("Feuchtigkeits-Messwert-Pfanze:");
  Serial.println(Sensor);
  delay(500);

Sensor2=analogRead(A1);
  Serial.print("Feuchtigkeits-Messwert2-Wasserstand:");
  Serial.println(Sensor2);
  delay(500);
}

The easy way: a very long delay().

The proper way: millis() based timing, as is showcased in the Blink Without Delay example, and more extensively in the "many things at a time" thread.

That will keep corrosion limited. It won't stop it, of course.

A device known as a relay can be used by your uController, to apply and remove power from your sensors: How Does A Relay Work - SPDT DPDT SPST Automotive Relay - YouTube

In this case you can also use I/O pins for power. The sensor doesn't need much. Set the pins to INPUT to disconnect, and HIGH resp LOW to switch on the sensor.

wvmarle:
In this case you can also use I/O pins for power. The sensor doesn't need much. Set the pins to INPUT to disconnect, and HIGH resp LOW to switch on the sensor.

i try this. The sensor does go on and off but the humidity values are no longer correct.

const int SensorPower = 7;  //PowerSensor pin plant - use digital I/O pin
int Sensor = A0;  //Sensor pin plant
int Sensor2 = A1; //Sensor pin Water
 
 
void setup()
{
  pinMode(SensorPower,OUTPUT);   //set pin 7 to be an output
  Serial.begin(9600);
}
 
void loop()
{
  digitalWrite(SensorPower, HIGH);
  
 Sensor=analogRead(A0);
 delay(5000);  // Warmup Sensor;
  Serial.print("Feuchtigkeits-Messwert-Pfanze:");
  Serial.println(Sensor);
  delay(500);

 digitalWrite(SensorPower, LOW);

Sensor2=analogRead(A1);
  Serial.print("Feuchtigkeits-Messwert2-Wasserstand:");
  Serial.println(Sensor2);
  delay(500);

delay(2UL * 60UL * 60UL * 1000UL);  //x hours each of 60 minutes each of 60 seconds each of 1000 milliseconds all unsigned longs

  
}

You have to do the 5-second warm-up BEFORE reading the sensor, not after.

Note: for proper power down you have to switch both sides of the sensor: both the 5V and the GND pins, otherwise there may still be a current flowing through the ground pin. You also must set the pinMode to INPUT, digitalWrite LOW does not switch it off. A LOW pin is connected to ground, not off.

The reason your values are “wrong” basically shows you the corrosion process. The value immediately taken is arguably the “correct” value, as much a cheap sensor like this can be really quantitative.

Do this experiment: take readings starting the moment you switch on the sensor for 5-10 seconds. Say one reading every 100 ms. You’ll see the readings change (soil resistance will increase) before stabilising: that’s because the ions in the soil have been migrating.

const byte SensorPower = 7;  //Power pin plant - use digital I/O pin
const byte SensorGnd = 8;  //Gnd pin plant - use digital I/O pin
const byte Sensor = A0;  //Sensor pin plant
 
void setup()
{
  Serial.begin(9600);
  // "cooling down" the sensor.
  Serial.println(F("10 seconds delay before measuring."));
  pinMode(SensorPower, INPUT);
  pinMode(SensorGND, INPUT);
  delay(10000);
  Serial.println(F("Start measuring!"));
  pinMode(SensorPower, OUTPUT);
  pinMode(SensorGnd, OUTPUT);
  digitalWrite(SensorPower, HIGH);
  digitalWrite(SensorGnd, HIGH);
  for (byte i = 0; i < 100; i++) {
    Serial.println(analogRead(A0));
    delay(100);
  }
  Serial.pritnln(F("Measurement done. Disconnecting sensor."));
  pinMode(SensorPower, INPUT);
  pinMode(SensorGND, INPUT);
}
 
void loop() {}

A much better way of measuring soil moisture is with a capacitive sensor (no corrosion whatsoever as there is no electrical contact with the soil). A good alternative (current through the soil but very limited corrosion) is by using an AC current, preferably in the 1 kHz to 1 MHz range. That also prevents corrosion.

wvmarle:
You have to do the 5-second warm-up BEFORE reading the sensor, not after.

Thank you so much for your help. I adapted the sketch a bit for myself. I don’t know if this is all right now. :wink:
But the first test looked very good. I now let the sensor “warm up” for 20 seconds after which approximate values are stable. Enclosed is my complete skecht.

//int Sensor = A0;  //Senor pin plant
const byte SensorPower = 7;  //Power pin plant - use digital I/O pin
const byte SensorGnd = 8;  //Gnd pin plant - use digital I/O pin
const byte Sensor = A0;  //Sensor pin plant

//int Sensor2 = A1; //Senor pin water lever
const byte SensorPower2 = 12;  //Power pin water level - use digital I/O pin
const byte SensorGnd2 = 13;  //Gnd pin water level - use digital I/O pin
const byte Sensor2 = A1;  //Sensor pin water level


int pumpPin = 4;  //1. Pumpe pin, water pump + Magnetventil pin, solenoid valve
int pumpPin2 = 3; //2. Pumpe pin, water pump + Magnetventil pin, solenoid valve


void setup() {
//  pinMode(Sensor, INPUT);
  pinMode(pumpPin, OUTPUT);
//  pinMode(Sensor2, INPUT);
  pinMode(pumpPin2, OUTPUT);
  digitalWrite(pumpPin, HIGH);     // Relais wird nicht gestartet,Prevents relays from starting up engaged
  digitalWrite(pumpPin2, HIGH);    // Relais wird nicht gestartet,Prevents relays from starting up engaged
  Serial.begin(9600);
}

void loop() {

//********* Round 1 ***************
//check

  Serial.println(F("Start measuring plant in 20s!"));
  pinMode(SensorPower, OUTPUT);
  pinMode(SensorPower2, OUTPUT);
  pinMode(SensorGnd, OUTPUT);
  pinMode(SensorGnd2, OUTPUT);
  digitalWrite(SensorPower, HIGH);
  digitalWrite(SensorPower2, HIGH);
  digitalWrite(SensorGnd, LOW);
  digitalWrite(SensorGnd2, LOW);
  delay(20000);
  Serial.print("plant:");
  Serial.println(analogRead(A0));
  delay(500);
  Serial.print("water level:");
  Serial.println(analogRead(A1));
  delay(500);


// 1. sensor and pump:
  if((analogRead(A0) >580) && (analogRead(A1)<700)) {    //Enter humidity value (A0)
    digitalWrite(pumpPin, LOW);
    delay(25000);
    digitalWrite(pumpPin, HIGH); 
    Serial.println(F("watering successfully"));
    }
  if((analogRead(A0) <580) && (analogRead(A1) <700)) {    //Enter humidity value (A0)
    Serial.println(F("Soil still moist."));
    }
       

//poweroff
 { Serial.println(F("Measurement done. Disconnecting sensor."));
  pinMode(SensorPower, INPUT);
  pinMode(SensorGnd, INPUT);
  pinMode(SensorPower2, INPUT);
  pinMode(SensorGnd2, INPUT);

    }
    


  //delay(7200000); // 2hour
  delay(2UL * 60UL * 60UL * 1000UL);  //x hours each of 60 minutes each of 60 seconds each of 1000 milliseconds all unsigned longs

  
}