analogRead gives noisy results

I'm building a digital thermostat. I've got a simple thermistor/potentiometer feeding into an analog port. I'm reading the result and outputting that on 7 segment displays (via 74HC595 feeding two 74HC4543s)

I'm finding that I get oddly noisy results from analogRead. I'm not just talking about minor fluctuations; bit 7 seems to alternate between reads. It seems to be particularly within certain ranges; so for example if the reading is around 578 it'll be fairly stable, but if the reading goes above 580 it'll alternate each time I read it - I'll get sequences like this: 581, 728, 581, 729, 582

The other bizarre thing is that if I adjust the delay within the loop it makes no difference. If I try to fool it by adding an extra read that doesn't seem to work either.

I'm using an Arduino Uno, so I can't use analogReadResolution to reduce the range.
I've worked around it by adjusting the potentiometer so that the range of values don't cause an issue, so I'm only asking for curiosity. Here's the code - anything obviously wrong here?

//Pin connected to SH_CP (11) of 74HC595
int clockPin = 8;
//Pin connected to ST_CP (12) of 74HC595
int latchPin = 9;
////Pin connected to DS (14) of 74HC595
int dataPin = 10;

void setup() {
  pinMode(A5, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  Serial.begin(9600);

}

void loop() {
  
  int temperature = analogRead(A5);
  delay(500);
  int toBCD = (((temperature%100) / 10) << 4) + (temperature % 10);

  Serial.print(temperature, DEC);
  Serial.print(" ");
  Serial.print(toBCD, HEX);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, toBCD & 0x0FF);  
  digitalWrite(latchPin, HIGH);
  Serial.println("");
}

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>

Please use code tags.

Read this before posting a programming question

How to use this forum

What is your wiring exactly?

And are you talking about the temperature variable or the toBCD variable?

Perhaps try reading the value a couple of times like below to allow the analog pin to stabilize some.

int temperature = analogRead(A5);
int temperature = analogRead(A5);
int temperature = analogRead(A5);

zoomkat:
Perhaps try reading the value a couple of times like below to allow the analog pin to stabilize some.

int temperature = analogRead(A5);

int temperature = analogRead(A5);
int temperature = analogRead(A5);

It happens consistently each time around the loop. One iteration analogRead will return a value around 581, the next iteration analogRead will return 728, with minor variations either side, over and over again. And as I said, it doesn't seem to matter if the delay is a couple of milliseconds or a couple of seconds.

As for a wiring diagram, the thermistor part is exactly like your typical basic analog input example - thermistor connected to +5V, potentiometer connected to ground, the point where the two meet goes to A5.

The LED display is a standard 595 shift register setup, with 0-3 feeding one 4543 and 4-7 feeding the other, each going into a 7 segment common anode. That works fine; I have had it running a simple counter from 0 to 99 over and over.

What ever is happening is nothing to do with analogRead() What ever itis has to do with

a. your circuit or

b. the thing your trying to get the temp of

Mark

It happens consistently each time around the loop. One iteration analogRead will return a value around 581, the next iteration analogRead will return 728, with minor variations either side, over and over again. And as I said, it doesn't seem to matter if the delay is a couple of milliseconds or a couple of seconds.

Basically sounds like the arduino input pin may be floating, possibly due to bad wiring, grounding, or no input from the gizmo.

Three responders and I agree that you might have a hardware issue. Please post a schematic and a clear photo of the wiring.

thermistor connected to +5V, potentiometer connected to ground, the point where the two meet goes to A5.

Why a pot and not a fixed resistor ? Have you tried a fixed resistor ?

I used a trimpot so that I can adjust the resistance - wanted to make sure I got the widest range of output values over the temperatures I'm measuring.

If the input was floating I'd see random values. What I am seeing is that the value changes according to temperature as expected, but over certain ranges it appears that one fairly significant bit is noisy, resulting in erroneous output.

I think you're right, it's probably the Arduino hardware that's faulty.

Replace the thermistor with a resistor and see if you get the same thing.

I used a trimpot

Not one of those nasty, cheap skeleton trim pots by any chance ? I await with interest the results of replacing the pot with a fixed resistor.

davetong:
it's probably the Arduino hardware that's faulty.

I think you misinterpreted 'hardware attached to Arduino' as 'Arduino hardware'.

I doubt that the Arduino hardware is 'faulty'.

Did you try a fixed (and or high precision) resistor as suggested?

Potis can create sometimes strange (very nonlinear) results, especially while operated.

Can you answer my question in reply #2 please? And copy and paste the results you are seeing.

I took all your various suggestions and bread-boarded a circuit using fixed resistors and a different thermistor. I also added a capacitor across them to reduce any noise. That gave consistent results over the range I was looking at, so I replaced the thermistor/trimpot combination on my board and it's stable. Thanks for all your help.