Need analog pin to read zero without anything connected and without grounding.

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?

Don't read them.

.

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?

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.

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

.

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.

fuzzybabybunny:
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.