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("");
}
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.
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.
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.
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.