Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Soil Moisture analog input question

Soil Moisture analog input question

Scheduled Pinned Locked Moved Troubleshooting
soil moisture
3 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GRB
    wrote on last edited by
    #1

    Hello,
    I have created a sketch based on the analog light sensor sketch for a soil moisture sensor. It incorporates voltage monitor because I am using a 9v battery as a source, using Arduino Nano, sensor is connected to 5vdc regulated.This is connected via a serial gateway to vera. I am having trouble with the sketch if I include the analog ref voltage code from the voltage monitoring code, I do not see a change in the analog value from the soil moisture sensor.

    // use the 1.1 V internal reference
    analogReference(INTERNAL);

    I have verified that that the voltage output is changing on the sensor. If I omit this code I am able to see the value of the moisture sensor change, however as the sensor detects more moisture and changes the value in Vera I notice that the battery level indicator changes. I do not have the 0.1uf cap connected from voltage tap to analog input, could this create the issue? I believe since I am not using the 1.1v reference code, I need to recalculate the ADCmax and also V/bit ratio in order to get an accurate percentage battery voltage. How does setting the analog reference impact other analog inputs? At this moment I don't have the sketch with me but will upload it later.

    1 Reply Last reply
    0
    • epierreE Offline
      epierreE Offline
      epierre
      Hero Member
      wrote on last edited by
      #2

      Hello,

      Have a look here:

      Leaf wetness is close to what you are using for the sensor can do both. Again I recommend the immersion gold for all other will erote too quickly... take good notice of the offline/sleep process to save your battery. I would recommend here a rechargeable LiOn such as CR123...
      https://github.com/empierre/arduino/blob/master/LeafWetnessSensor.ino

      This second one is for a more complex one using a sensirion sensor that mesures groud temp + humidity digitally:
      https://github.com/empierre/arduino/blob/master/SoilMoistSensorSHT1x.ino

      The last I've not finished yet is about gyspum captors someone here has done yet. Since current is not polarized, there is less oxydation, and the captor inside the gypsum avoids humidity contact.

      z-wave - Vera -> Domoticz
      rfx - Domoticz <- MyDomoAtHome <- Imperihome
      mysensors -> mysensors-gw -> Domoticz

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GRB
        wrote on last edited by
        #3

        Here is the sketch if anyone can help out.

        #include <SPI.h>
        #include <MySensor.h>

        #define CHILD_ID_LIGHT 0
        #define LIGHT_SENSOR_ANALOG_PIN 1

        #define NODE_ID 9

        int BATTERY_SENSE_PIN = A2; // select the input pin for the battery sense point

        unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)

        MySensor gw;
        MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
        int lastLightLevel;
        int oldBatteryPcnt = 0;

        void setup()
        {
        // use the 1.1 V internal reference
        //analogReference(INTERNAL);
        gw.begin(NULL, NODE_ID, false);

        // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Soil_Moist_Sensor_grb", "1.15");

        // Register all sensors to gateway (they will be created as child devices)
        gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
        }

        void loop()
        {
        int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
        Serial.println(lightLevel);
        if (lightLevel != lastLightLevel) {
        gw.send(msg.set(lightLevel));
        lastLightLevel = lightLevel;
        }

        // get the battery Voltage
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
        Serial.println(sensorValue);

        // 1M, 470K divider across battery and using internal ADC ref of 1.1V
        // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
        // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
        // 3.44/1023 = Volts per bit = 0.003363075 - changed without ref volt
        float batteryV = sensorValue * 0.00305734;
        int batteryPcnt = sensorValue / 10;

        Serial.print("Battery Voltage: ");
        Serial.print(batteryV);
        Serial.println(" V");

        Serial.print("Battery percent: ");
        Serial.print(batteryPcnt);
        Serial.println(" %");

        if (oldBatteryPcnt != batteryPcnt) {
        // Power up radio after sleep
        gw.sendBatteryLevel(batteryPcnt);
        oldBatteryPcnt = batteryPcnt;
        }

        gw.sleep(SLEEP_TIME);
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        8

        Online

        11.7k

        Users

        11.2k

        Topics

        113.1k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular