Analog Pins returning value even when disconnected

Hi,
I am having an issue with a project and have narrowed it down to the fact that the analog pins aren't returning useful values. I would expect that when completely disconnected they would return zero but are in fact returning values in the vicinity of 290 to 300.
Also if I give one of them 5volts then that one goes up to around 1018 and the others jump up to around 768.
I tried an alternative board sold by our local electronics company (Jaycar) and that gives a value of around 460 with zero volts.
Here's the basic code I used to read the pins. Any help for a newb would be appreciated :slight_smile:

int valOne = 0;
int valTwo = 0;
int valThree = 0;

void setup() {
Serial.begin(9600);
// set pin modes
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A4, INPUT);
}

void loop() {
valOne = analogRead(A0);
delay(100);
valTwo = analogRead(A2);
delay(100);
valThree = analogRead(A4);
delay(100);

Serial.print("valOne: ");
Serial.print(valOne);
Serial.print("  valTwo: ");
Serial.print(valTwo);
Serial.print("  valThree: ");
Serial.println(valThree);
delay(2000);
}

With nothing connected to the pins, they will be floating, so could indeed give an apparent voltage reading, thats normal.

Connect one of the pins to GND, what does it read ?

1 Like

Random noise pickup - good for seeding the pseudo random number generator. :grinning:

2 Likes

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

1 Like

It does indeed read zero! OK thanks for that. My actual problem is with getting usable values from a photoresistor i.e. values that actually substantially change when the light conditions change but I thought I'd found the issue with the pins not returning a zero value. At least I can rule that out now.

Done! Sorry I didn't do this to begin with - I did get a message about it when I posted but the page looked fine so didn't bother. I can see it does make a difference though so will do so in future.

Mmmm yes well I will keep that in mind for any future projects that require this feature but the current one doesn't :slight_smile:

When connected to 5V they should read close to 1023. The pins that read only 768 may be damaged.

1 Like

Yes that is what I thought. Will do some more research on the topic.

How are you connecting the photoresistor (or Light Dependant Resistor, LDR)? You need to use a fixed value resistor (eg. 10K) with it to form a voltage divider.

1 Like

As I read it, he only connected one; the others will, of course, be affected by charge transfer, but no one has explained that yet, and the topic is solved, so I'll avoid it.
C

1 Like

Hi Paul,
I have 5 volts on one side of the photoresistor and a 10K resistor to earth on the other side and a lead to an analog pin on the non-earth side of the resistor. I followed the Arduino project book for that part of the wiring so it should be ok.

1 Like

Thank you for the tantalizing hint regarding charge transfer - I did a bit of googling and a slight rewire and now have my desired three sensors all giving useful results. I prematurely 'solved' the topic because I thought I had enough to progress with but your hint got me across the line :slight_smile: Much appreciated.

1 Like

(Emphasis added.)

Contributors @groundFungus and @Paul_B point out here that it may be possible to do away with the external resistor in the divider by using INPUT_PULLUP on either digital or analog inputs.

1 Like

Only issue with that is the vagaries of semiconductor 'resistance' formation mean that the internal pullup resistance is 'approximate', and varies from device to device. But presumably the OP will need to do a calibration of each such device anyway, so maybe it will suffice. My problem with on-chip resistance is that it will almost invariably change with temperature, as the chip warms and cools(for example, when powering more or fewer LEDs). That introduces unpredictability in the pullup resistance, which is no big deal when being used as a pullup, but may be undesirable in a calibrated circuit.
YMMV, no warranty, ya gets what ya pays for, etc. etc.
C

1 Like

I think you are making the common mistake of thinking that 0 volts (zero volts) is the same as 'no volts'. 0 volts is a point in the circuit designated by the circuit designer against which all other voltages in the circuit are measured unless stated otherwise. No volts is a physical impossibility, all normal matter made of protons, neutrons and electrons has a voltage, it must have because in the middle of an atom there is a positive charge and round the outside a negative charge. There is always a voltage on a pin, it just might not be the voltage you expect it to be.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.