Soil Sensor analog values differ on Uno vs Pro Mini
-
@daulagari Yes Uno is powered via USB@5v, but the soil sensor is connected to the 3.3v regulated supply.
@murdog said:
Yes Uno is powered via USB@5v, but the soil sensor is connected to the 3.3v regulated supply.
So is the full-ADC scale (1023) not the same as the voltage supply?
682/1023 * 5 Volt = 3.33 Volt and that would clip the 3.3 V ADC of the Pro Mini so therefore it is giving 1023.
-
OK, the soil sensor is connected to a 3.3v regulated supply.
But the Arduino sounds like it's still running at 5v, and by default the ADC reference is VCC. So at full scale soil sensor reading of 3.3v will be only about 2/3 of that 5v reference. (but 100% of a 3.3v reference).
So they are both right, just using different ADC references.
The 5V reference is probably fine, you lose a little bit of resolution (less than one bit) but soil moisture sensors are not exactly precise.
If it mattered, you can connect the 3.3v supply to the Aref input and set the ADC to use Aref as it's reference, in which case you'll get the full resolution 0..1023 for 0..3.3v. In this case I probably wouldn't bother.
-
OK, the soil sensor is connected to a 3.3v regulated supply.
But the Arduino sounds like it's still running at 5v, and by default the ADC reference is VCC. So at full scale soil sensor reading of 3.3v will be only about 2/3 of that 5v reference. (but 100% of a 3.3v reference).
So they are both right, just using different ADC references.
The 5V reference is probably fine, you lose a little bit of resolution (less than one bit) but soil moisture sensors are not exactly precise.
If it mattered, you can connect the 3.3v supply to the Aref input and set the ADC to use Aref as it's reference, in which case you'll get the full resolution 0..1023 for 0..3.3v. In this case I probably wouldn't bother.
-
@murdog: You should probably also have a look at the New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries thread.
If you want to make the code generic, you should have the code measure the VCC and scale the measured value according to what you read for VCC.
-
@murdog: You should probably also have a look at the New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries thread.
If you want to make the code generic, you should have the code measure the VCC and scale the measured value according to what you read for VCC.
-
Can you please share your sketch? I have tried modifying the digital version of the sketch to analog but I have no idea what to write in the sensor type.
-
Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
#include <SPI.h>
#include <MySensor.h>#define ANALOG_INPUT_SOIL_SENSOR A0
#define CHILD_ID 0 // Id of the sensor childMySensor gw;
MyMessage msg(CHILD_ID, V_HUM);
int lastSoilValue = -1;void setup()
{
gw.begin();// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
// sets the soil sensor analog pin as input
pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID, S_HUM);
}void loop()
{
// Read analog soil value
int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);if (soilValue != lastSoilValue) {
Serial.println(soilValue);
gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
lastSoilValue = soilValue;
}
// Power down the radio and arduino until analog input changes.
gw.sleep(1000);
} -
Oh, sleep should probably be set to 3600000 or more to get long sleep periods between measurements as I don't think soil moisture should be measured every second....
-
Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
#include <SPI.h>
#include <MySensor.h>#define ANALOG_INPUT_SOIL_SENSOR A0
#define CHILD_ID 0 // Id of the sensor childMySensor gw;
MyMessage msg(CHILD_ID, V_HUM);
int lastSoilValue = -1;void setup()
{
gw.begin();// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
// sets the soil sensor analog pin as input
pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID, S_HUM);
}void loop()
{
// Read analog soil value
int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);if (soilValue != lastSoilValue) {
Serial.println(soilValue);
gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
lastSoilValue = soilValue;
}
// Power down the radio and arduino until analog input changes.
gw.sleep(1000);
}@Moshe-Livne said:
Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
#include <SPI.h>
#include <MySensor.h>#define ANALOG_INPUT_SOIL_SENSOR A0
#define CHILD_ID 0 // Id of the sensor childMySensor gw;
MyMessage msg(CHILD_ID, V_HUM);
int lastSoilValue = -1;void setup()
{
gw.begin();// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
// sets the soil sensor analog pin as input
pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID, S_HUM);
}void loop()
{
// Read analog soil value
int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);if (soilValue != lastSoilValue) {
Serial.println(soilValue);
gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
lastSoilValue = soilValue;
}
// Power down the radio and arduino until analog input changes.
gw.sleep(1000);
}hello ,
i want to do same , but can you explaind the calcul is made ?
for now i use light sensor sketch with analog input , but i ant to modify by /10 for having a moisture meter reading value 0to100% or maybe there is a S_Percentage come in next version of mysensors ?
i use domotiz with gateway and for now only Light sensor work for read analog value .
thanks
-
@Moshe-Livne said:
Managed to make it a humidity sensor, which is probably close enough. Here is my sketch:
#include <SPI.h>
#include <MySensor.h>#define ANALOG_INPUT_SOIL_SENSOR A0
#define CHILD_ID 0 // Id of the sensor childMySensor gw;
MyMessage msg(CHILD_ID, V_HUM);
int lastSoilValue = -1;void setup()
{
gw.begin();// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0");
// sets the soil sensor analog pin as input
pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID, S_HUM);
}void loop()
{
// Read analog soil value
int soilValue = analogRead(ANALOG_INPUT_SOIL_SENSOR);if (soilValue != lastSoilValue) {
Serial.println(soilValue);
gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100)); // Send the relative moisture. this will probably work only on nano or uno as I used 1023
lastSoilValue = soilValue;
}
// Power down the radio and arduino until analog input changes.
gw.sleep(1000);
}hello ,
i want to do same , but can you explaind the calcul is made ?
for now i use light sensor sketch with analog input , but i ant to modify by /10 for having a moisture meter reading value 0to100% or maybe there is a S_Percentage come in next version of mysensors ?
i use domotiz with gateway and for now only Light sensor work for read analog value .
thanks
@fred97 , i also use domoticz. If you read this thread form the start, you will see that the arduino converts the moisture reading to something in the range 0-1023 (or 682 if you use mini pro). 1023 is 0 moisture, 0 is 100% moisture. so, moisture content is (1023 - reading)/1023*100.
Hope this answers your question!
Moshe
-
thanks you ,
i have try your sketch but don't work send only O
send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0 444 send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0 470 send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0 1023 send: 2-2-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0i think the problem is inside converter in percentage ....
240 when is in water and 1023 when completly dry .
-
@fred97 did you change anything in the calculation? the values in the println seems correct. do not remove the static casts - they are essential.
-
@fred97 did you change anything in the calculation? the values in the println seems correct. do not remove the static casts - they are essential.
@Moshe-Livne said:
Serial.println(soilValue);
gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100))how about just using the map() function...
Serial.println(soilValue); gw.send(msg.set(map(soilValue, 0, 1023, 0 ,100)); -
@Moshe-Livne said:
Serial.println(soilValue);
gw.send(msg.set(static_cast<int>(static_cast<float>(1023 - soilValue))/1023*100))how about just using the map() function...
Serial.println(soilValue); gw.send(msg.set(map(soilValue, 0, 1023, 0 ,100));@BulldogLowell would have used it if I knew it existed.... would it work is reverse? (1023,0, 0, 100)?
-
@BulldogLowell would have used it if I knew it existed.... would it work is reverse? (1023,0, 0, 100)?
Try it!!
int soilValue = 250; void setup() { Serial.begin(115200); Serial.println(map(soilValue, 1023,0,100,0)); } void loop() { }have you run into 123dcircuits yet?

-
Try it!!
int soilValue = 250; void setup() { Serial.begin(115200); Serial.println(map(soilValue, 1023,0,100,0)); } void loop() { }have you run into 123dcircuits yet?

@BulldogLowell not yet!!! my first encounter with arduino was only a few weeks ago.... so much to learn! the wood are lovely dark and deep, but i have promises to keep....
-
hello
it's working but is not inverted :+1:
314 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30 294 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28 287 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28 299 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:29 313 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30 1023 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:100100 is when is dry and 220/250 is when the sensor is in water .
i just need little more help for invert value , 100 = in water and 0 when is drythx
-
hello
it's working but is not inverted :+1:
314 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30 294 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28 287 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:28 299 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:29 313 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:30 1023 send: 2-2-0-0 s=0,c=1,t=1,pt=4,l=4,st=ok:100100 is when is dry and 220/250 is when the sensor is in water .
i just need little more help for invert value , 100 = in water and 0 when is drythx
@BulldogLowell said:
gw.send(msg.set(map(soilValue, 0, 1023, 0 ,100));
like this:
gw.send(msg.set(map(soilValue, 100, 250, 0, 100));it maps 100 to zero and 250 to 100
Let me know if that works for you.
or... you can try this to bound the returned value by zero and 100:
gw.send((max(min(map(soilValue, 100,250,0,100), 100), 0)));confusing but is is mapping the value, and limiting to 100 and then making sure it is at least zero (i.e. non-negative).
or just:
gw.send(constrain(map(soilValue, 100,250,0,100), 0, 100));