Go Down

Topic: Need analog pin to read zero without anything connected and without grounding. (Read 4766 times) previous topic - next topic

fuzzybabybunny

Unused analog pins return a random value on analogRead but I want them to all return zero.

Besides hooking the unused analog pins to ground, is there any other way to have analogRead return zero? Perhaps a software solution that can be implemented in code?

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Delta_G

Unused analog pins return a random value on analogRead but I want them to all return zero.

Besides hooking the unused analog pins to ground, is there any other way to have analogRead return zero? Perhaps a software solution that can be implemented in code?
No, you have to understand that without anything connected the pins are floating.  The laws of physics say that they will pick up whatever random signals that they happen to pick up and will have those voltages.  If you read them, then it will read the voltage that is on the pin.  How would the Arduino ever know that the pin was connected or not?  All it sees is the voltage there.  It has no way to know if that voltage is from a sensor or a floating pin. Your only option if you want them to reliably read 0 is to make sure that they reliably have 0V on them and the only way to do that is to connect them to ground.

Why do you think you need them to read 0 when not connected?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

fuzzybabybunny

Why aren't pullups or pulldowns used for analog pins?

From the documentation: https://www.arduino.cc/en/Tutorial/DigitalPins

"Often it is useful to steer an input pin to a known state if no input is present."

That works fine and dandy for a digital pin, but I would think that being able to apply the same principle to an analog pin (steering an input pin to a known state when there's no input) would be a useful feature?

Delta_G

That works fine and dandy for a digital pin, but I would think that being able to apply the same principle to an analog pin (steering an input pin to a known state when there's no input) would be a useful feature?
How would that be useful?  An analog pin is reading an analog voltage.  It really wouldn't ever be connected to an open circuit and if it was then you would surely know so why would you read it in that case?

You can always take a resistor and connect your analog inputs to ground.  Not that it would do anything useful.  You already know you grounded those pins so you know they have 0V on them so why would you ever in that case try to read one of them?

But this still begs the question, why do you think you need them to read 0 when not connected?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

fuzzybabybunny

In the future I plan on querying the values of the analog pins remotely from the internet via a REST API. Without physical access to the board, I would like the board to return zero values for those pins which aren't connected to anything.

Others may rearrange, add, or subtract sensors to different pins. I'm hoping there is a way to avoid having  them also remember to attach unused pins to common ground. It would be easier to just have them plug sensors in and not have to worry about anything else.

larryd

If you know which analogs are not valid, when you retrieve their values, ignore them in the returned data.


.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Delta_G

Others may rearrange, add, or subtract sensors to different pins. I'm hoping there is a way to avoid having  them also remember to attach unused pins to common ground. It would be easier to just have them plug sensors in and not have to worry about anything else.
That may sound easier, but it ain't never gonna work.  How will the Arduino ever be able to tell the difference between a voltage coming from some sensor and a voltage coming from random noise?  It can't.  You'll need to define where the sensors go and when.  

Besides, even if you have them connect the pins to ground, how will you be able to tell the difference between a pin reading 0 because it is connected to ground or a pin reading 0 because that is the sensor value?  
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

fuzzybabybunny

Oh sweet I think I got it.

Added a resistor of like ~148K to ground and now it works beautifully to detect any empty pins and it still returns exactly the right values when the sensors are plugged in.

A 10K resistor wasn't enough and still gave a reading in the 780 range, so I bumped it higher.

Any analog pin returning a value of greater than 1000 is disconnected. The sensors themselves normally would never hit anywhere close to 1000 or 0 and if they did that means they're shot anyway or the environment is insanely out of whack, like the fish pond is somehow 100% pH 1.00 acid, lol.

PaulMurrayCbr

Added a resistor of like ~148K to ground and now it works beautifully to detect any empty pins and it still returns exactly the right values when the sensors are plugged in.

A 10K resistor wasn't enough and still gave a reading in the 780 range, so I bumped it higher.
As I understand it: the pulldown resistor needs to be very much greater than the impedance of the sensor that you are connecting to the pin.

Consider a "sensor" with is just an ideal 5v battery in series with a 1k resistor. Without any pulldown, your pin will read a full 5v (1023). If you put a 1k pulldown on the pin, the pin will read half that, because the voltage at the pin will be 2.5.

Any sensor will have some sort of internal resistance, because we just don't live in an ideal world. A simple sensor is a potentiometer - 5v at one end, ground at the other, and a tap moving along a resistor of some particular value. You can see immediately that putting a fixed resistor from the tap to ground will change the voltage everywhere except for the extreme ends of the travel of the knob. The response becomes nonlinear, and the nonlinearity is greatest … I'm not sure. Either in the middle of the range, or when the resistance from one end to the tap matches your pulldown resistance. Some calculus may be required.

The point is:
  • adding a pulldown changes the numbers you get from the sensor - it will make them somewhat lower than you'd expect
  • The important numbers are the impedance of the sensor, the impedance of an analog input pin, and the value of the pulldown resistor
  • if the pulldown is too low compared to the sensor, the "pulling down" effect will cancel out the data you get from the sensor
  • if the pulldown is too high compared to the impedance of an analog pin, it will fail to pull down the pin to zero when the pin is unconnected

But according to topic=65134.0, the impedance on an analog pin is 100MΩ, so meh. Slap a big resistor on there and you're good.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up