Go Down

Topic: Trouble using 4 moisture sensors in parallel on A0-A3 input pins on Uno (Read 1 time) previous topic - next topic



To be clear, I'm a bit of a newbie.

I'm having trouble trying to read moisture sensors connected to the first 4 analog input pins on an Uno.  I'm using some moisture sensors plugged into LM393 boards supplied with the moisture sensors. I only connect the analog outs from the LM393's to the analog pins on the Uno, and leave the digital output from those boards unconnected. I've measured the voltages getting to each LM393 and all are getting ~4.95v.

I do a digitalWrite(a*,HIGH) to each pin to set the pullup resistor and then call pinMode(a*,INPUT) to set the mode back to input (I see that this might not be necessary since I'm following it with an analogRead(a*) later).

I then loop over analogRead(A*) and serialWrite calls. When I sequentially short the two moisture sensor arms on a sensor, the first 3 sensors (whose analog outputs are tied to A0-A2) show no change in their value (all four sensors stay at ~1023), but when I short out the Moisture sensor arm for the 4th sensor (on pin A3) that sensor's output drops to ~0 and the other 3 sensor outputs drop to ~200-300 (although these latter values do vary).

Any ideas on what I'm doing wrong and how I can get the 4 sensors to be independent and correctly read the resistance across the moisture sensor arms?  I've switched the LM393 boards and moisture sensors around and find that the board/sensor combo connected to A3 always works and the other 3 do not.

In case this is relevant, I am using i2c devices (real time clock and LCD) that are connected to A4 and A5. But I believe that I get the same behavior even if the i2c devices are disconnected.

I'd really appreciate any input as this is driving me crazy!


Are the grounds from the moisture sensor boards connected to the Arduino ground?  Can you provide a  link to the sensor's datasheets?
You will save everyone's time if you read and follow the forum guidelines.  :)           


The ground is connected to the Arduino ground.

I am having trouble finding the datasheets for the YL-69 and the LM393 (labeled as YL-38). I've sent a request to the place in China where I purchased them and will post pdf's if and when I get them.

However, in digging around for the datasheets, I found a suggestion to power the moisture sensors through the Arduino's digital pins so that they could be turned on only when a reading is needed. This would prolong their life, because they wouldn't have a constant current flowing through them inducing corrosion. I plan to re-wire things in this manner and I wonder if doing so would help with the problem I'm seeing. It would allow me to turn on only one sensor, read it, then turn it off and move to the next sensor.  If isolating the sensors by only having one turned on at a time is a possible solution to my problem, then perhaps I should withdraw this question until I see if it still exists after re-wiring. Any thoughts?



How much current do the sensors need? More than 20mA? If so, use a transistor instead that the Arduino can control.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


One link I saw says that these are low power (<20mA), but is there a way for me to determine that with a voltmeter without using the datasheet? 

If they are near 20mA, would it help to turn only one on at a time?


I found another link which says the current draw for these sensors in 35mA. Can the Uno provide that to 4 of these sensors? If not, would this explain my problem?


AFAIK those moisture sensors are the equivalent of two nails and a 10k pull up resistor.
That is, if you use the analogue output.

The chip is just a threshold detector, connected to the digital output. And maybe also to an indicator LED.
The blue pot sets the chip's threshold.

Try two galvanised nails with a plastic spacer, and a 10k pull up resistor.


Thanks Leo, I'll check into it.

Just to let people know, I re-wired things to sequentially turn on each sensor, read it and then turn it off before moving on to the next sensor and this DID NOT fix the problem. Only one sensor continues to function (the last one connected to A3) and the 3 others still do not work.  Very puzzled.


Try 100n capacitors from each analogue sensor input to ground.



 I tried piggy-backing 4-100nF capacitors across the 4 analog probe signal lines and ground and don't see any change in behavior. Is that what you intended me to try?


Yes, that usually fixes analogue input problems.
I think it's time to see your code.
And a wiring diagram.


Hi Leo:

I've tried to draw up a schematic for the moisture sensor part of my watering system (I did not include any of the connections for the pump relays or the i2c connections for the RTC/LCD. The latter use analog pins A4 (SDA) and A5 (SCL).  A PDF of the schematic and my code are attached

I've commented out code for the LCD. The LCD doesn't initialize properly, so after looking over the sensor issue ... if you are really bored and/or interested in looking at that code to see why, go ahead. I've installed the new i2c version of the LCD library, but that didn't fix the LCD. The LCD isn't critical for the watering system ( I just thought it might be nice to see what the status of the system was when its operating off of the USB connection to the PC).

Thanks a ton for your help, I'm clearly doing something wrong ... I just hope its not moronic!! :->



Can't find any big problems in your code.

digitalWrite(A0, HIGH); // set pullup on analog Pin0  | this block is not needed
pinMode(A0,INPUT); // set mode of analog Pin0 to INPUT | this block is not needed

This line:

MoistureSensorValue = analogRead(MoistureSensorPin);  // read the moisture sensor i

Try using it twice, like this.

MoistureSensorValue = analogRead(MoistureSensorPin);  // dummy read
MoistureSensorValue = analogRead(MoistureSensorPin);  // read the moisture sensor i

The second read overwrites the first one, and might clear any ghost charge from the analogue muxer.

If that doesn't work, write a simple program that just writes the analogue pin values to the serial monitor.
With constantly powered (directly to 5volt) sensors.
Nothing else connected to the Arduino.



  Here's the output from the two trials you asked me to run. Note the 100nF capacitor are still in place across the sensor output and Ground (I didn't notice until I did both of these trials, let me know if you want me to repeat without the caps)

Output from Original Code with double read of Analog Pins:

Date: 08/07/2016/  Time: 13:32:37/
Sensor Values (1-4):   110, 238, 296, 1003,
Date: 08/07/2016/  Time: 13:32:59/
Sensor Values (1-4):   100, 236, 324, 1004,
Date: 08/07/2016/  Time: 13:33:21/                  \
Sensor Values (1-4):   104, 233, 299, 1003,          | Shorting #1
Date: 08/07/2016/  Time: 13:33:43/                   |   
Sensor Values (1-4):   99, 240, 328, 1004,          /
Date: 08/07/2016/  Time: 13:34:05/
Sensor Values (1-4):   110, 236, 299, 1003,
Date: 08/07/2016/  Time: 13:34:27/                  \
Sensor Values (1-4):   101, 224, 326, 1003,          | Shorting #2
Date: 08/07/2016/  Time: 13:34:49/                   |
Sensor Values (1-4):   109, 230, 316, 1003,          /
Date: 08/07/2016/  Time: 13:35:11/                  \
Sensor Values (1-4):   102, 228, 295, 1003,          | Shorting #3
Date: 08/07/2016/  Time: 13:35:33/                   |
Sensor Values (1-4):   100, 229, 300, 1004,          /
Date: 08/07/2016/  Time: 13:35:55/
Sensor Values (1-4):   101, 232, 309, 1003,
Date: 08/07/2016/  Time: 13:36:17/                  \
Sensor Values (1-4):   106, 239, 324, 718,          | Shorting #4
Date: 08/07/2016/  Time: 13:36:39/                  |
Sensor Values (1-4):   107, 235, 301, 997,          /

Output from Simple Code with Constant 5v, Double read of Analog Pins and no other attachments:

Sensor Values (1-4):   543, 476, 629, 998,
Sensor Values (1-4):   750, 576, 573, 998,
Sensor Values (1-4):   821, 636, 544, 998,
Sensor Values (1-4):   842, 675, 543, 998,
Sensor Values (1-4):   852, 698, 554, 999,
Sensor Values (1-4):   857, 710, 571, 998,
Sensor Values (1-4):   860, 718, 589, 999,
Sensor Values (1-4):   860, 718, 591, 998,
Sensor Values (1-4):   858, 716, 583, 999,
Sensor Values (1-4):   854, 707, 562, 998,

Looking at the voltage across the YL-38 VCC and GND pins for this simple case I get a constant 4.86v for all four sensors. 




  I pulled the Caps and ran the main program again. What I see is that, as always, the 4th sensor drops to 0 after shorting, but none of the others show any change.  I did note that the very first reading of sensor #1 came in at ~450 and all subsequent readings of #1 were ~100. Sensors #2 and #3 stayed around 350-400, while sensor #4 was ~1000, unless shorted.


Go Up