I am wondering if anyone has a implemented a floating analog input detection routine that works reliably?
I have a custom PCB with analog pins moved to a row of headers next to each other. In my application the user doesn't have to use all analog inputs so some might be left floating. I want to detect that on startup and turn them off (not read those inputs). If the floating pins are read, they will randomly set states in my application so I want to avoid that.
So far I have made a crude implementation where in the startup routine the pins are checked 3 times with tiny delays in between, and if the max and min value on a given pin is different, it is considered floating and isn't read anymore in the main program.
The issue I am having with that is, that when an input right next to the floating one is connected (to a potentiometer for example) the fuzzyness of the floating input decreases and it's signal mimics the signal if the input right next to it. This makes floating input detection difficult because the min and max values are not as much apart anymore.
When switching between inputs, the usual way out of that problem is to read the new input twice, discarding the first reading.
If you want analog inputs to work properly, you do have to obey the rules. The input voltage source must have impedance less than 10K. Floating inputs obviously violate that.
That would be a problem for reading a voltage divider like a potentiometer because you would have a parallel resistor across the upper side of the divider (or lower side for a pull-down).
I think this might work:
Put a small capacitor between the pin and Ground.
Set the analog pin to OUTPUT HIGH.
Do an analogRead().
Set the analog pin to OUTPUT LOW.
Do another analogRead().
If the two reads are significantly different then there is not enough drive current from whatever source and you can consider it "floating".
This works because setting the pin to an OUTPUT will charge or discharge the capacitor. When you switch back to analogRead() the charge in the capacitor will move to the sample/hold capacitor at the input to the ADC. If there is a voltage source connected to the pin it will change the capacitor value before the value is read. You may need a resistor to protect the connected signal from the HIGH and LOW signals written to the pin.
I tried pulldown resistors with this issue before, worked well to prevent the floating signal.
But like it was said above, it distorted the signal when a voltage divider was used, and I plan to use potentiometers here.
Although I did some maths and if the pulldown resistor was big enough, it should not change the signal. Eg. if the bottom half of the voltage divider was 2K ohms and the pulldown parallel to it was 10M ohms, then the resulting parallel resistance would be 1999.6 ohms which is pretty much 2k. I just thought it could be done purely in software
Interesting idea with switching the pins to outputs. I will try that!
I wrote this function which ignores the first ADC reading like it was suggested. so far it is working without problem. Maybe someone will make use of it.
Great. It is known to make a difference. According to MicroChip, this is a problem when changing ports (multiple analog inputs actually use the same ADC). I'm curious whether you tested the behaviour when using the same port. Edit - hmmm it looks like you did.
When you enter a function like that, though, you don't really know (from the function point of view) what the last port was...
With any use of the analog ports you need to perform some sort of calibration to allow for tolerances etc . So any additional pull up /down will make no difference .
If you add a pull down resistor to the lower half of a voltage divider eg a linear pot, the reading will not be linear anymore. Similarly with pull up resistors on the upper half of a voltage divider.
like you said, you would usually do calibration and set tolerances etc, but if a linear input is a requirement then pull up/down does make a difference.
Not sure what you mean - you wouldn’t normally use a pot in a divider , and any resistors in parallel with each other create an equivalent lower resistance via the resistors in parallel Equation .
No non linearity in output verses input , even if you did use a pot , or are you saying something else I missed ? ( the pot position verses output may be non linear , but not the voltage )
A potentiometer is just a nicely packaged voltage divider circuit that you can adjust with a dial/knob.
So if you add a pull up/down resistor to it, you are adding it in parallel to the top or bottom side of the voltage divider. So the effective resistance changes and skews the signal.
eg: if you have a 10k pot and a 1k pulldown resistor.
the easiest to illustrate would be if the pot is in the middle, the voltage divider will be 5k on one side and (5k || 1k) = 833ohm on the other. so the voltage on the analog input will not be half of the total voltage used.
In my experience the effect is that the pot signal is not linear even if linear pots are used.
I mentioned this an a rough solution to this in my post #5 above
I’ll give you that the position verses output is non linear as I said .
But I can’t see why you would ever use a pot as a voltage divider for a voltmeter, even linear is not that linear .
If you did you would not need a pull up /down resistor anyway .
There is also the danger with a pot that an inadvertent twiddle could over voltage an input and destroy the processor .
The important bit anyway is input /output is linear !
when an input is connected and not floating, no need for a pull up/down, i agree,
my whole issue is that my project gives the user the option to connect a pot if they chose to. meaning, sometimes the input will be floating,
so i was figuring out how to get around that... and having a pull up/down that is there when input is floating AND when a pot is connected is not a viable solution, because yes, it solves the floating input issue, but distorts the signal when a pot is connected
sets a criterion that depends on the noise in the system.
Decouple your supply, put the circuit in a grounded metal box, and I'd guess that the noise might deteriorate below the threshold (5).
Also pots are inherently noisy so unless you connect a capacitor between the slider and ground pin a pot might generate more thhan 5 of noise, and be counted as disconnected.
If you connect a pot between a +Vcc and ground, then a current will flow in the Vcc and 0V connections. Why not detect that?
Yes, since I posted this function, I decreased this criterion to >=2.
I saw that when the input is used, the fuzzyness is -/+ 1 , most often than not, there is no fuzziness at all. Even with questionably sourced pots I am using.
When I left it at >=5, sometimes the floating ones were within that range and came up as not floating which was an error.
That's a good point with detecting current instead. How can I implement that on an arduino?
A small value fixed resistor between either Vcc or ground, in series where the pot normally connects to it. Then measure the voltage with another analog input. That would take two inputs for each one analog though.
There is another problem. What happens when you change the pot setting?
The max and min will be different and you will report a flaoting pot.
Of course once the pot setting becomes stable again it will be OK.
I like @johnwasser idea in post 4 (I dont entirely understand it) but it gave me an idea.
Connect a digital pin via a 1M resistor to the analog input pin & pot.
When its set as an input, its high impedance and wont affect the pot reading.
set it as an output high and if the pot is O/c you will read 1023.
set it as output low and if the pot is O/c you will read 0.
If those readings are >1020 apart you can be certain you have no pot connected.
What if just the +5 or 0V to the pot is disconnected?
Again the readings will diagnose this.