Long wire Water Tank Level Indicator, Floating Values when on Electrical outlet

Hi Shantanu,
I did the numbers and can see why it is variable. The arrangement I used to work with is your 20,000 (20k) pullup resistors (built-in), turned on by code lines 9-13 INPUT_PULLUP, and 27k external pull-down resistors. Water resistivity varies a lot, due to changes in temperature, turbidity, mineral content etc. It will vary with electrical current in the water as well due to the ionisation process introduced by the dc current between the probes.

Let's say the water resistance is somewhere around 18k per 10cm. The circuit (+5 - probe - gnd) is a voltage divider with 20k probe to vcc and 10k8 probe to ground. The 10k8 is the combined parallel resistance of 18k in the water and 27k external pull down. Seen this way, the level probe is at about 1.75 volts above ground and 5v vcc is 3.25 volts above the probe.

Assuming a 10 cm gap, the current in the water will be in the range of 300 microamps. Any change in the water resistance will have an effect on the current, which will drive a change in the voltage seen at the pin, as it is the centre of a voltage divider.

When the AVR reads an input pin, a small but meaningful change in current results, since a small amount of power is consumed to make the reading. Since your current in the water is so small it may be affected by the read current, causing varying readings. In addition, the current in the water is ionising the water and that in itself causes a change in resistance which contributes to instability at the probe.

Thinking about how I would proceed, I would remove the INPUT_PULLUP directive and set the pins as INPUT. I cannot see why your code needs to set the pins HIGH (lines 15 – 18) because it is the same thing as setting the internal pullups on. I would advise we turn them off. To do that remove lines 15 – 18 and change lines 9 – 13 to pinMode(firstLevel, INPUT); would also suggest we use an external pull down value that will give us a bit more current in the water – while this will increase ionisation somewhat, it will also reduce any effect during the microprocessor read cycle. I would try either 10k or 4k7 pull downs. (An alternative approach would be to use the internal pullups and discard the external pull down, then measure from ground to pin rather than vcc to pin – I like that approach better, but it makes no technical difference other than removing the potential introduced because the water is grounded as mentioned in earlier posts).

This way, the only factor affecting the readings is the variance of water resistivity. Since you are only interested in whether the probe is covered or not, then you can interpret the change in pin voltage (which will now be significant) as covered or uncovered.