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. General Discussion
  3. Modifying A0 value to Percentage

Modifying A0 value to Percentage

Scheduled Pinned Locked Moved General Discussion
12 Posts 5 Posters 90 Views 5 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.
  • alowhumA Offline
    alowhumA Offline
    alowhum
    Plugin Developer
    wrote on last edited by
    #3

    Perhaps this code is of interest to you. It's the Candle plant health sensor. It's got all the bells and whistles you could want, including optional support for automated irrigation.

    One thing you'll notice in there is that the capacitive moisture sensors output voltages between 0 and 3. So in @rvendrame's code you may have to change the 1023 to 650, and also make sure no voltages higher than that would lead to percentages above 100%.

          int16_t moistureLevel = analogRead(analog_pins[i]);
          Serial.print(F("raw analog moisture value: "));
          Serial.println(moistureLevel);
    
          if( moistureLevel > 650 ){moistureLevel = 650;}
          moistureLevels[i] = map(moistureLevel,0,650,0,99); // The maximum voltage output of the capacitive sensor is 3V, so since we're measuring 0-5v about 614 is the theoretical highest value we'll ever get.
    

    I notice in your code that the title Analog Soil Moisture Sensorx3 is too long. It can't be longer than 25 characters.

    M 2 Replies Last reply
    1
    • rvendrameR rvendrame

      @mrhutchinsonmn I think you just need to replace this line in the first sketch:

      send(msg.set(sensorValue));
      

      By this:

      float moisture_percentage;
      moisture_percentage = ( 100 - ( (sensorValue/1023.00) * 100 ) );
      send(msg.set( moisture_percentage ));
      

      I don't use HA so I don't know if something needs to be changed in HA side...

      M Offline
      M Offline
      mrhutchinsonmn
      wrote on last edited by
      #4

      @rvendrame That worked! I did need to change "float moisture_percentage" to init "moisture_percentage" to get passed the "Call of overloaded function is ambiguous" error. Not sure if that is the correct approach but I was able to compile and upload. Not sure how I missed the send msg but I did. Thanks again!

      rvendrameR 1 Reply Last reply
      0
      • alowhumA alowhum

        Perhaps this code is of interest to you. It's the Candle plant health sensor. It's got all the bells and whistles you could want, including optional support for automated irrigation.

        One thing you'll notice in there is that the capacitive moisture sensors output voltages between 0 and 3. So in @rvendrame's code you may have to change the 1023 to 650, and also make sure no voltages higher than that would lead to percentages above 100%.

              int16_t moistureLevel = analogRead(analog_pins[i]);
              Serial.print(F("raw analog moisture value: "));
              Serial.println(moistureLevel);
        
              if( moistureLevel > 650 ){moistureLevel = 650;}
              moistureLevels[i] = map(moistureLevel,0,650,0,99); // The maximum voltage output of the capacitive sensor is 3V, so since we're measuring 0-5v about 614 is the theoretical highest value we'll ever get.
        

        I notice in your code that the title Analog Soil Moisture Sensorx3 is too long. It can't be longer than 25 characters.

        M Offline
        M Offline
        mrhutchinsonmn
        wrote on last edited by
        #5

        @alowhum Thank you.. I may try that code. I have found that when a sensor goes bad it either drops to a "0" reading or "1023". Currently, I use those numbers (below 175 and above 700) to tell Home Assistant to shut off the relay going to that zone, so automations do not keep calling for water.

        1 Reply Last reply
        0
        • M mrhutchinsonmn

          @rvendrame That worked! I did need to change "float moisture_percentage" to init "moisture_percentage" to get passed the "Call of overloaded function is ambiguous" error. Not sure if that is the correct approach but I was able to compile and upload. Not sure how I missed the send msg but I did. Thanks again!

          rvendrameR Offline
          rvendrameR Offline
          rvendrame
          Hero Member
          wrote on last edited by
          #6

          @mrhutchinsonmn said in Modifying A0 value to Percentage:

          @rvendrame That worked! I did need to change "float moisture_percentage" to init "moisture_percentage" to get passed the "Call of overloaded function is ambiguous" error. Not sure if that is the correct approach but I was able to compile and upload. Not sure how I missed the send msg but I did. Thanks again!

          I'm glad it worked! And right, the code I provided was to work with integers.

          if you want to keep using float (and have decimals places), you have to add the number of decimal places in send function ( I put 2 decimals in this example):

          send(msg.set( moisture_percentage , 2  ));
          

          Home Assistant / Vera Plus UI7
          ESP8266 GW + mySensors 2.3.2
          Alexa / Google Home

          M 1 Reply Last reply
          1
          • alowhumA alowhum

            Perhaps this code is of interest to you. It's the Candle plant health sensor. It's got all the bells and whistles you could want, including optional support for automated irrigation.

            One thing you'll notice in there is that the capacitive moisture sensors output voltages between 0 and 3. So in @rvendrame's code you may have to change the 1023 to 650, and also make sure no voltages higher than that would lead to percentages above 100%.

                  int16_t moistureLevel = analogRead(analog_pins[i]);
                  Serial.print(F("raw analog moisture value: "));
                  Serial.println(moistureLevel);
            
                  if( moistureLevel > 650 ){moistureLevel = 650;}
                  moistureLevels[i] = map(moistureLevel,0,650,0,99); // The maximum voltage output of the capacitive sensor is 3V, so since we're measuring 0-5v about 614 is the theoretical highest value we'll ever get.
            

            I notice in your code that the title Analog Soil Moisture Sensorx3 is too long. It can't be longer than 25 characters.

            M Offline
            M Offline
            mrhutchinsonmn
            wrote on last edited by
            #7

            @alowhum I did ding around with the sketch you shared, which is really well done and full-featured. I get a reading of 50% when the sensor is immersed and 95% when it is dry. What I would like is to see 100% when immersed and near 0% when it is dry. Can the code be changed to meet that need?

            H mfalkviddM 2 Replies Last reply
            0
            • rvendrameR rvendrame

              @mrhutchinsonmn said in Modifying A0 value to Percentage:

              @rvendrame That worked! I did need to change "float moisture_percentage" to init "moisture_percentage" to get passed the "Call of overloaded function is ambiguous" error. Not sure if that is the correct approach but I was able to compile and upload. Not sure how I missed the send msg but I did. Thanks again!

              I'm glad it worked! And right, the code I provided was to work with integers.

              if you want to keep using float (and have decimals places), you have to add the number of decimal places in send function ( I put 2 decimals in this example):

              send(msg.set( moisture_percentage , 2  ));
              
              M Offline
              M Offline
              mrhutchinsonmn
              wrote on last edited by
              #8

              @rvendrame That is what I was looking for.. Nice!...Thank you

              1 Reply Last reply
              0
              • M mrhutchinsonmn

                @alowhum I did ding around with the sketch you shared, which is really well done and full-featured. I get a reading of 50% when the sensor is immersed and 95% when it is dry. What I would like is to see 100% when immersed and near 0% when it is dry. Can the code be changed to meet that need?

                H Offline
                H Offline
                hard-shovel
                wrote on last edited by
                #9

                @mrhutchinsonmn

                try changing

                 moistureLevels[i] = map(moistureLevel,0,650,0,99); // The maximum voltage output of the capacitive sensor is 3V, so since we're measuring 0-5v about 614 is the theoretical highest value we'll ever get.
                

                to

                 moistureLevels[i] = map(moistureLevel,325,650,99,0); // The maximum voltage output of the capacitive sensor is 3V, so since we're measuring 0-5v about 614 is the theoretical highest value we'll ever get.
                
                1 Reply Last reply
                0
                • M mrhutchinsonmn

                  @alowhum I did ding around with the sketch you shared, which is really well done and full-featured. I get a reading of 50% when the sensor is immersed and 95% when it is dry. What I would like is to see 100% when immersed and near 0% when it is dry. Can the code be changed to meet that need?

                  mfalkviddM Offline
                  mfalkviddM Offline
                  mfalkvidd
                  Mod
                  wrote on last edited by
                  #10

                  @mrhutchinsonmn said in Modifying A0 value to Percentage:

                  I get a reading of 50% when the sensor is immersed and 95% when it is dry

                  Could you expand on what the sensor is immersed in? Wet soil?

                  The conductivity will vary depending on what minerals and how much of them are present in the soil, which will vary depeding on what type of water the soil is watered with, season, whether fertilizer is added and a bunch of other stuff. So be aware that the level that means 0% moisture will vary.

                  M 1 Reply Last reply
                  0
                  • mfalkviddM mfalkvidd

                    @mrhutchinsonmn said in Modifying A0 value to Percentage:

                    I get a reading of 50% when the sensor is immersed and 95% when it is dry

                    Could you expand on what the sensor is immersed in? Wet soil?

                    The conductivity will vary depending on what minerals and how much of them are present in the soil, which will vary depeding on what type of water the soil is watered with, season, whether fertilizer is added and a bunch of other stuff. So be aware that the level that means 0% moisture will vary.

                    M Offline
                    M Offline
                    mrhutchinsonmn
                    wrote on last edited by
                    #11

                    @mfalkvidd In this particular case, the sensor is monitoring the rain barrel of my garden irrigation system and is used to govern whether or not to fire off an automation calling to water a raised bed. The water does have a lot of iron in it, so not sure if that plays a part in what seems to be an unusual reading. I am expecting to see a number around 275-300 when completely immersed in water, but I am getting a reading of around 400+, which is strange to me. I felt using percentages would allow me to customize each sensor sketch for the environment it is being used in.

                    mfalkviddM 1 Reply Last reply
                    0
                    • M mrhutchinsonmn

                      @mfalkvidd In this particular case, the sensor is monitoring the rain barrel of my garden irrigation system and is used to govern whether or not to fire off an automation calling to water a raised bed. The water does have a lot of iron in it, so not sure if that plays a part in what seems to be an unusual reading. I am expecting to see a number around 275-300 when completely immersed in water, but I am getting a reading of around 400+, which is strange to me. I felt using percentages would allow me to customize each sensor sketch for the environment it is being used in.

                      mfalkviddM Offline
                      mfalkviddM Offline
                      mfalkvidd
                      Mod
                      wrote on last edited by
                      #12

                      Thanks for explaining @mrhutchinsonmn

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


                      10

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.0k

                      Posts


                      Copyright 2019 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