Question on analogRead - Newb

Hi All,

I am trying to use the analogRead. The value returned is all over the place. With a rock steady input of 1.8 volts (resistor divider from the 5V pin) the value goes as high as 380 and as low as 352. As I plan to use it to measure the output from a thermistor it is not quite acceptable. The code used is simple:

int analogPin = 0; // 1.8 volts
int val = 0; // variable to store the read value

void setup()
{
Serial.begin(9600); // Output to the Serial Monitor
}

void loop()
{
val = analogRead(analogPin); // read the input pin

Serial.println(val); // Print with return

delay(5000);
}

Any suggestions or brickbats gladly accepted

Midas55

Are there any potential noise sources in your system? What is your power supply? Quite obviously your 1.8 V is not rock steady, so your only recourse is to eliminate the noise by either going after the source or by filtering it.

The simplest answer: never use a single analog reading for your data; always average several. See what your results look like if you print out four-sample averages (or eight, or sixteen) instead of single readings.

The next simplest answer: put some large caps across power and ground throughout your circuit. Does the arduino use an LC filter on its AREF pin? If not, adding one would help (if that's even possible).

  • Ben

Hi Ben,

Thanks for the reply, you hit it on the money. The planned location for the thermistor was about 10 meters from the Aduino board so I simulated it with the resistor divider at the end of a 10 meter stretch of wire. That is where the noise came from, it works fine if I plug everything directly into the board.

Back to the drawing board...

Midas

If you construct a sensor circuit that outputs a digital signal instead of an analog voltage, you can transmit it over long distances without worrying as much about noise. You would also probably want to give the sensor circuit its own power supply.

  • Ben

Hi. I'm running with a similar problem. In my project i must read an analog dc voltage, send the read value to an app in PC, wait for almost 2 sec, after this, another analog read over the same input. For this task i wrote the next code, that is invoked twice:

int Mido_dc() {
long val_dc=0;
long tmp=0;
for (int i=0; i<=99;i++) {
tmp = analogRead(dcPin);
val_dc = val_DC + tmp;
}
val_dc = tmp / 100
Serial.print(val_dc);
}

For test this i made a little external circuit with a 9Vdc battery and a resistive divisor. It place a fixed 2.50v at the analog input. (measeured with a digital multimeter). The problem is that at the first read the value send is aprox 512, but at the second read the value is grater(p.e. 530) I was making a lot of tests, with another multimeter and a osciloscope to verify noise, but the analog source is really steady.
Maybe some mistake in my code? Are there some technical issues that i'm not considering? Or this is normal? :-/
Thanks!