Help! I'm using a soil moisture detector on an arduino uno r3. However, the readings seem to be inaccurate. Whenever I leave my detector on a table or holding in into air, it has a very high reading like 1000. But when I submerge it into soil it turns zero, even though I submerge the soil into water. Another thing I noticed is that the readings are high when I submerge it directly into a cup of water without any soil. The code is in the comments for reference. Please help me...
int sensor;
const int powerpin = 8;
const int delayTime = 1000;
const int redLED = 2;
const int greenLED = 3;
const int thresh = 500;
void setup()
{
Serial.begin(9600);
pinMode(powerpin, OUTPUT);
pinMode (redLED, OUTPUT);
pinMode (greenLED, OUTPUT);
}
void loop() // code that loops forever
{
digitalWrite(powerpin, HIGH); //turning the sensor on
delay (10); // short delay
sensor = analogRead(A0); // read sensor
digitalWrite(powerpin, LOW); // turn sensor off
Serial.println(sensor); // print reading
delay(delayTime);
if(sensor>thresh){
digitalWrite(redLED, LOW);
digitalWrite (greenLED, HIGH);
}
else{
digitalWrite(redLED, HIGH);
digitalWrite (greenLED, LOW);
}
delay(delayTime);
}
Thanks for posting your code and for using code tags. But if you had read the forum guide you would know that there are other things that the forum needs to make it possible to help you. Please read it before you post again.
There are many types of soil moisture sensor which work with different principles. You must post a link to the description and specs of the type you are using.
Ok, this sounds great. But you say it is inaccurate. Can you say why you believe this to be inaccurate?
That does seem strange, but maybe it is normal for the type of sensor you are using.
If you want a value which is, for example, 0% in dry air and 100% in wet soil, you could use the map() function like this:
sensor = map(analogRead(A0), 1023, 0, 0, 100); // read sensor
Sorry about the inconvenience I made and I appreciate the clarification. Here are the specs the I used for reference as well.
-
YL-69 Soil Moisture Detector
Soil Moisture Sensor Soil Humidity Detection Module Philippines | Ubuy -
Arduino Uno R3
Redirect Notice -
Green and Red LEDs 5mm
-
220 ohms resistors (x2)
-
Breadboard
-
Jumper Wires
This is the video I followed along. I noticed that the soil moisture in this video has the opposite results of mine. I'm starting to think if I bought defective ones or maybe I'm missing something.
Link:
I'll take note of that. Thank you very much.
The sensor in the video is a different design to your sensor, I suspect. It may be wired differently internally and so give an output signal with different characteristics. That doesn't necessarily mean it's defective. From your description of how it behaves, it sounds like it's working fine. But that code was written for a different design of sensor, and so may need to be modified in order to work correctly with your sensor.
Try this change
sensor = 1023 - analogRead(A0); // read sensor
I wish people would stop getting these sensors
Yes you can prolong the life by shutting them off between readings.
Knowing more about your sensor now, I guess it is normal. The reading you would get from clear water would be different from wet soil. The conductance of clear water will probably be much lower than wet soil, depending on the dissolved minerals in the water. Distilled water would have a very low conductance, and mineral water would probably be higher, but still not as high as wet soil.
The sensor @cookie_doe is using does seem to be lower quality than the one in the video. The sensor in the video seems to have much larger conducting surfaces and they appear to be gold plated.
Personally, I made my own sensor from two lengths of galvanised steel wire held apart with a large screw terminal block, which has lasted for many years.
Indeed. My sensor does this and takes readings only once every 15 minutes.
The 1 second delay in the code is only for demonstration purposes. There is no need to measure the moisture level that frequently!
Even hours will do, moisture normally doesn't change rapidly.
Kudos for a very low cost diy sensor
I appreciate all the suggestions made. I'll try out the changes to calibrate the delay value of soil moisture detector. May I ask if you guys can recommend me different soil moisture detectors, please? I just need to seek advice since I am still a neophyte when it comes to electronics I'm currently working on a capstone project that I will probably improve it when I enter college. Thank you so much..
Why do you want a different sensor?
If you connect the one you have correctly it will work
The capacitive ones don't measure in direct contact with the soil. There's a lot of them on ebay, aliexpress and so on, those can be flaky. I recommend you get them from more reputable sources.
pinMode(powerpin, OUTPUT);
Your module should be powered by the 5V pin NOT an output pin
An explanation to why would benefit OP.
The OP has not asked for one