Internal Pullup affects AnalogRead()

I have a moisture sensor that returns 0 - 3.3V based on its wetness. I'm happy using a 0-675 range and losing some resolution, so using the 5V reference is OK right now.

Simply reading the sensor returns between 0 and 650 (so I'm assuming there's some parasitic loss in the 3m cable, etc).

What I'd like to do is to know when the sensor has been disconnected, so I thought I'd use the internal pullup via digitalWrite(A0, HIGH). Now, however, my readings shoot up to 740 or so at the high end, and never drop below 230 at the low end.

Of course, if I disconnect my sensor I get a 1023 reading so I can detect the disconnect.

Can someone help me with the maths to understand why I'm getting different readings with the internal pullup enabled and whether I can correct through calculation or whether I should be exploring a different approach to identifying when my sensor is disconnected.

I think when on a pin you activate an analogRead digital functions are deactivated for this pin, so you can not have pullup resistor activated.
If you need a Pull-up resistor use an external resistor.

Not according to the documentation

However, there is a line that says "Be aware however that turning on a pullup will affect the values reported by analogRead()." But doe snot describe how they will be affected.

But doe snot describe how they will be affected.

That’ll depend on Mr Ohm and his law.

  1. At my point of vue it is not a good idea to mix analog and digital fuction on the same pin even it is possible.
    And for noise reducing it is also right.
  2. R pull-up are given from 30 k to 60 k --> what confidence can we have ?
  3. for analog measurements I think it is a very, very bad, idea to put a resistor to power rail.
    Normally we use a resistor referenced to ground rail, always for noise reducing.

The circuit for digital input, analog input and digital output is all available at the same time for an analog pin.
So AWOL is right, it depends on the value of the internal pullup resistor and the impedance of the circuit.

You can not distinguish a single analog value from an open pin.
But you can turn the pullup resistor on for a just a moment, and read the analog value.
You can also connect a resistor to an digital output pin of the Arduino. Set that as input, and the resistor is not doing anything, set the pin as output high and low, and that changes the analog input.

Perhaps you have to add some delays with that short test. If you do that test every time, retrieving the humidity takes a little longer. I hope that is not a big problem.

What I recommend is to make the best measures possible . If you are not hard you can not respect it.

For your problem you need to know some details that you probably will not find on the Arduino website but in the datasheet of the micro -controller .
The input impedance of the analog digital converter is very high but there is a restriction on the impedance of the circuit connected to the ADC : it must not exceed 10 kohms . If it exceeds 10 kohms you need to decrease the sampling frequency - > see the datasheet .

The output impedance of your sensor will form a voltage divider with resistor pull-up that I recall vary from 30 to 60 kohms.
This is why I recommend an external resistor

In the datasheet after a change in the ADC configuration it is recommended not to keep the first measure, you must not only add a delay but you must throw the first 2 or 3 measures .

If you absolutely want to use a pull-up , you must already understand the role played by the pull-up in your application .
The pull-up is just to fix a potential to prevent that unconnected pin acts as an antenna.

You need to know the electrical schematic of your sensor to know the value of its output impedance.
With ADC you're not in digital electronics domain but in analog electronics domain .
If this output impedance and pull-up resistor are well known it will possible to calculate a correction.

The arduino board are not optimized for analog measurements. The microcontroler Aref pin is too noisy.
If you have a UNO board you can improve drastically by welding on the back-side of the board a 100 nF capacitor directly on the micro socket between GND and AREF pin.

Please notice that a pull-up or a pull-down resistor act exactly the same way to prevent the antenna effect such as divider bridge effect..

Guys, thanks very much for your advice and guidance. I think the impedance of the sensor is 10k, but given I don’t have the exact schematics, I can’t be sure. However, by combing suggestions from 68tjs and Caltoa you all I have decided to adopt the following approach:

Given I only need to read soil moisture every 15 minutes or so, every minute I turn on the internal pull-up, read the analog pins, turn off the pull-up, wait a few msec, make a few “sacrificial” readings and resume readings as normal. This gives me plenty of time to detect a disconnected sensor and not have to worry about finding the impedance of the internal pull-up or the sensor.

To 68tjs’s point, it may not be the most elegant solution, but it does meet my needs.

Thanks.
Gaz

Very good.
You have to do some tests with timing and throwing away the first sample and so on.
Suppose 5ms delay between it has some influence, then I would use 50ms to be sure.