Wednesday, September 26, 2018

Electrolysis Issues with Soil Moisture Sensor

One problem with testing for moisture in soil or for free water is electrolysis of the electrodes. As material is removed from one electrode and deposited on the other (a byproduct of using a dc current), the resistance changes, and the values coming from the sensor start to drift.

One way to solve this is to flip the polarity of the current each time you do a read. The following code will change the polarity of the pins (and the equation evaluation) each time through the loop.

This circuit will print the word "Triggered" if it senses moisture (even skin moisture) and light up the onboard LED. You could have additional sections of code for varying levels of moisture (very wet, wet, dry, very dry), based on the analog results.

Wiring:

Probe pin 1 - A0 //analog input
Probe pin 2 - A5 //digital output
2M ohm resistor between A0 (analog input) and A4 (digital output)

Code:

int state=0;
int aValue=0;


void setup() {
  // put your setup code here, to run once:
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  if (state==0){
    digitalWrite(A4, HIGH);
    digitalWrite(A5, LOW);
    aValue=analogRead(A0);
    Serial.println(aValue);
    if (aValue<200){
    Serial.println("Triggered");
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
 
    state=1;
    delay(500);
  } else if (state==1){
    digitalWrite(A4, LOW);
    digitalWrite(A5, HIGH);
    aValue=analogRead(A0);
    Serial.println(aValue);
    if (aValue>800){
      Serial.println("Triggered");
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
    state=0;
    delay(500);
  }


}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...