stopping floating analog pins

Hello,
can anyone enlighten me on how to deal with floating analog pins? Here is my scenario:

sensor=soil moisture has power line(white), data line(red) and ground(stripped). I don't know much about how the sensor itself is wired only that it uses Frequency Domain Reflectometry technology.

I have a basic sensor set up that triggers a solenoid. the program I've written takes an average from all the sensors and if it falls in certain range turns on solenoid. my issue is I don't want an accidentally unplugged sensor to skew the average of the other sensors. Also it would be cool for future to basically have the system automatically know if a sensor is plugged in and in turn use the solenoid (for example if I wanted to run multiple solenoids of different arrays of sensors)

So basically is there a way to wire my project so that the analog inputs don't float, but also so that if a sensor does get plugged in it will get an accurate reading?

Thanks and if this is covered elsewhere please feel free to just point me in right direction.

Turn on the internal pullup resistors. Or add external pullups of 100K to 1M if the 30-50K internal is too strong.

...or use a connector with a switch. Some connectors have a switch that can ground your analog input when there is nothing plugged in, but will disconnect the analog input from ground when something is plugged in.

This may cost a little more than a resistor but does not load down the unspecified sensor.

Ok, I guess I was wrongly under the impression that the pull up resistors were only for the digital pins. Also Is there a reason to use pull up resistors rather than pull down? As far as testing whether the internal resistors are to strong would simply be done via attaching the sensor and seeing if data is accurately read?

-For the connector with switch do you have any suggestions on what to search for? just did a google search and didn't see anything right away that looked correct. Also does this suggest that the pull-up resistors effect the sensor reading voltage? or is it more of a power consumption issue?

Using the internal pullup resistors has the same effect as connecting an external resistor of 30-50K from +5V to the analog input.

Doing so could change the reading that you get from a sensor. Post a link to the data sheet or product page of the sensor.

Here is the data sheet/manual:

http://manuals.decagon.com/Manuals/13876_EC-5_Web.pdf

Just doing some experiments it seems as though the value its returning is accurate. Tried without pull up resistors on and tried with pull up resistors off. Also I'm using the mkr zero chip so its running at 3.3V.

A side programming note:
it seems as though I have to put the pinMode command in my loop function, as when I put it in setup it only seems to turn on the pull up resistors for initial reading. Am I doing something wrong or is this normal?

Pull up resistors mean that you can use the internal pullup resistors (good) and the analog input goes to full scale when disconnected (not so good).

Pull down resistors mean that you have to use external resistors (not so good) but the analog input goes to zero when disconnected (good).

The pinMode command should not have to go in the loop function, but without seeing your code it is impossible for me to figure out what went wrong.

To post code:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

vaj4088:
Pull up resistors mean that you can use the internal pullup resistors (good) and the analog input goes to full scale when disconnected (not so good).

Why is this not good? 1023 is a special value but no more special than 0.

I like this solution because the code can easily turn the pullups on and off in code. You can test for connectedness and then turn off the pullups for the real reading.

I completely agree with you, and using the internal pullup resistors is likely the way that I would want to do it myself. However, "newbies" tend to think that somehow 0 is more special than 1023. You are correct that it is not different from a C standpoint.

On some processors, a 16-bit comparison with 0 is slightly faster and simpler than a comparison with 1023. I would not worry about it in most cases.

I just did an experiment to turn on INPUT_PULLUP on a floating analog input, and read out the analog value.

The result, on an ebay Pro Mini, was 1007 - 1008, not 1023. If you touch the floating input, the value varies down from there, but never below about 950. I don't have a good explanation for this, but beware!

This was done with the example program "AnalogInOutSerial" that comes with the Arduino IDE.

I know that analogRead() does more than just simply read the pin. I looked at the code from wiring_analog.h to see what it was doing. Unlike analogWrite() it doesn't seem to disable the pullup or anything. There's a few direct register assignments that I didn't look too closely at. It is quite possible that analogRead() does disable pullups.

That would mean that your Pro Mini was charging up the pin to (near) 5V with the pullup but the analogRead() sees a floating value, dependent on where your fingers are.

analogRead just controls the analog multiplexer and sets the ADC going and waits for it, the pin state is not affected, just sampled.

Another forum member duplicated my results and sent them via a PM. As they may be of general interest, I repeat them below. Of course, the 30K-50K impedance of the pullup exceeds the recommended 10K maximum input impedance to the ADC, so perhaps we are just seeing the effect of the time constant with the sample and hold capacitor.

Firstly, duplicated the " sensorValue = analogRead(analogInPin);" line. No significant difference. Changed delay to 200 of course so I could read the numbers.

Wondered whether the internal pull-up actually pulls up to Vcc, or has a voltage drop - which would in no way restrict its effectiveness for digital reads - so added a 100k pull-up. This restricts the values to 1019 - 1023 which is clearly consistent with a 35k resistive value of the internal pull-up and gives an effective input impedance (resistance) of the analog input itself as well over two megohms to ground (pull-up assumed to be 35k, 1023-1009 is 14, so 1023 ÷ 14 × 35) but only if the input "resistance" actually is to ground and not something else such as ½ Vcc.

So it just means that whilst the input impedance is high, a modest shunt does not pull a high impedance "all the way" to rail (either rail). I did not try a capacitor across the input or a 100k pull-down because it certainly does not go anywhere near to zero when left open either - there are more experiments to check, but it certainly is susceptible to static pick-up as you would expect.

vaj4088:
...or use a connector with a switch. Some connectors have a switch that can ground your analog input when there is nothing plugged in, but will disconnect the analog input from ground when something is plugged in.

This may cost a little more than a resistor but does not load down the unspecified sensor.

I have an application for a connector like this, but like the other user above, haven't found one. Do you have a recommendation or a source for these connectors?

Headphone connectors have switches in them. Look for a stereo socket with too many pins. Stereo requires only ground, left and right but you often see 4 or 5 pins on the sockets.

Barrel connectors like the big ugly one on your Uno or Mega also have an extra terminal that can be used to detect the presence of a plug. Look for ones that have 3 pins.

I used to get connectors at a store that no longer exists, and then I moved out of the state, so I no longer have a source. You might try DigiKey, Allied, Newark, Mouser, or others.

In particular, audio receptacles for 1/4" and 1/8" (3.5 mm) often have embedded switches that may be used. I believe that other connectors have this as well.

Sorry.

Hi, I have the same question, but just know I find some way to fix it, I am going to do a "radio" to pick up the RF in the air but the flowing makes me crazy, but I have one way to fix it: for example, I read analog input from A0 pin, and read the analog input from A1 pin as well, so that I can get (A0-A1), it can cancel some "noise"(I think it is because the board).
It works on my board but if it doesn't work on your board, please let me know.
@mhart9