Question about code and electrolysis



  • I am writing code to read 2 analog moisture sensors that are subject to electrolysis. I am using millis as a timer to have it only read on set intervals. I currently have this code outside the millis function.

    unsigned long currentMillis = millis();
      int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
      int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
    

    my question is will this code still read the soil probes constantly and therefore erode them or will it only read on the intervals I have defined. I know it reports only on the intervals I have outlined but that really defeats the purpose or trying to prevent electrolysis if it is still reading them constantly and just not reporting the values. Thanks for any help!

    #include <SPI.h>
    #include <MySensor.h>
    
    #define ANALOG_INPUT_SOIL_SENSOR1 A0
    #define CHILD_ID1 0 // Id of the sensor child
    #define ANALOG_INPUT_SOIL_SENSOR2 A1
    #define CHILD_ID2 1 // Id of the sensor child
    
    
    long previousMillis = 0;
    long interval = (1000); //1000 is 1 second 
    
    MySensor gw;
    MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
    int lastSoilValue1 = -1;
    int lastSoilValue2 = -1;
    
    void setup()
    
    {
      gw.begin();
      gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
      pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
      pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
      gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID2, S_HUM);
    }
    
    void loop()
    
    {
      unsigned long currentMillis = millis();
      int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
      int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
      
      if (currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;  
        Serial.println(soilValue1);
        Serial.println(soilValue2);
        gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
        gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
        lastSoilValue1 = soilValue1;
        lastSoilValue2 = soilValue2;
      }
    }```


  • well I just moved it inside the if statement to make sure. If this doesn't do what I am hoping it does please chime in! Thanks.

    #include <SPI.h>
    #include <MySensor.h>
    
    #define ANALOG_INPUT_SOIL_SENSOR1 A0
    #define CHILD_ID1 0 // Id of the sensor child
    #define ANALOG_INPUT_SOIL_SENSOR2 A1
    #define CHILD_ID2 1 // Id of the sensor child
    
    
    long previousMillis = 0;
    long interval = (10000); //1000 is 1 second 
    
    MySensor gw;
    MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
    int lastSoilValue1 = -1;
    int lastSoilValue2 = -1;
    
    void setup()
    
    {
      gw.begin();
      gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
      pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
      pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
      gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID2, S_HUM);
    }
    
    void loop()
    
    {
      unsigned long currentMillis = millis();
      
      if (currentMillis - previousMillis > interval) {
        int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
        int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
        previousMillis = currentMillis;  
        Serial.println(soilValue1);
        Serial.println(soilValue2);
        gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
        gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
        lastSoilValue1 = soilValue1;
        lastSoilValue2 = soilValue2;
      }
    }
    

  • Hardware Contributor

    @punter9
    Nothing in your code seems to control the power to your soil sensor. You'll probably need to implement hw/sw for this.



  • @punter9
    You could try powering it from a digital pin, turn the pin high, wait 50ms, take analog reading, turn digital pin low.


  • Hero Member

    @punter9 An older post which discusses corrosion and how to avoid it



  • ah this makes sense. As long as these are connected to my voltage supply there will be this issue. I need to hook it to a controllable source such as a digital pin to get controlled voltage.

    I am learning here, but this seems to me the steps to the most electrolysis resistance.

    Base - 2 bolts in soil with an analog reading going to arduino, power and ground to power supply. Very high electrolysis

    1.) implement a digital pin as the power source - write high, wait, take analog reading, write low

    2.) implement a voltage dividing circuit. add resistor to one side of circuit, use sensor as other side, take analog reading between the two

    3.) reverse polarity each reading - write digital pin 1 high, digital pin 2 low, take analog reading. Next time do the inverse.

    Does this all seem correct? I really appreciate your help and references on this, I am really learning a ton here.



  • Ok, I decided for my needs all I really need to do is be able to turn power on and off so I have written code to utilize the digital pins as a power source, and continue with the analog read. This should extend the life of my sensors since I only need to get a reading about 4 times a day.

    Would anyone mind looking over this code and make sure it is actually limiting electrolysis as I intend? I am still learning here. Thanks for all the help so far!

    The way I have it set up at the moment is to have the power(vcc) come from the digital pin and then tie the ground to the common ground so as to not tie up more digital pins. Will this work as intended or do I need to also have a dedicated digital pin written low for each sensor?

    #include <SPI.h>
    #include <MySensor.h>
    
    #define ANALOG_INPUT_SOIL_SENSOR1 A0
    #define CHILD_ID1 0 // Id of the sensor child
    #define ANALOG_INPUT_SOIL_SENSOR2 A1
    #define CHILD_ID2 1 // Id of the sensor child
    
    
    long previousMillis = 0;
    long interval = (10000); //delay betweein readings - 1000 is 1 second 
    
    MySensor gw;
    MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
    int soilPower1 = 1; //digital pin to get power from
    int soilPower2 = 2; //digital pin to get power from
    
    void setup()
    
    {
      gw.begin();
      gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
      pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
      pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
      gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID2, S_HUM);
      pinMode(soilPower1,OUTPUT);
      pinMode(soilPower2,OUTPUT); 
    }
    
    void loop()
    
    {
      unsigned long currentMillis = millis();
      digitalWrite(soilPower1,LOW);
      digitalWrite(soilPower2,LOW);
      
      if (currentMillis - previousMillis + 500 > interval) {
        digitalWrite(soilPower1,HIGH);
        digitalWrite(soilPower2,HIGH);
      }
    
      if (currentMillis - previousMillis > interval) {  
        int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
        int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
        previousMillis = currentMillis;  
        Serial.println(soilValue1);
        Serial.println(soilValue2);
        gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
        gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
        digitalWrite(soilPower1,LOW);
        digitalWrite(soilPower2,LOW);
      }
    }
    

Log in to reply
 

Suggested Topics

5
Online

11.4k
Users

11.1k
Topics

112.7k
Posts