Modifying A0 value to Percentage
-
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 Sensorx3is too long. It can't be longer than 25 characters. -
@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...
@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!
-
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 Sensorx3is too long. It can't be longer than 25 characters.@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.
-
@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!
@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 )); -
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 Sensorx3is too long. It can't be longer than 25 characters.@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?
-
@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 ));@rvendrame That is what I was looking for.. Nice!...Thank you
-
@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?
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. -
@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?
@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.
-
@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.
@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.
-
@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.