Recommend method for analog sensor reading?

Hi all,
I use a couple different analog temp sensors, one of them is an LM35, and the other is a Phidgets 1114 (because it has a wider temp range). I am using the following code to read the sensors multiple times, with the idea that I will get a better reading, since there is some inherent noise on the ADC. Is there a better way to do this? The reason I ask is, it seems when I change the number of times I read the sensors, I get different results, not just a smoother “more accurate” reading. Thanks!

void writeTemps() {
  int An0 = 0;
  int An1 = 0;
  static uint32_t prevMillis;
  int readcount = 12; // number of times to read the analog sensors and average for accurate results
  for (int i = 1; i < readcount; i++){
    An0 += (analogRead(0)/readcount);
    An1 += (analogRead(1)/readcount);
    }
   InTempF = (((((5*(An0*97))/1024)*9)/5)+32);   //LM35 temp sensor
   OutTempF = (((((An1/4.3)-52)*9)/5)+32);        //Phidgets 1114 temp sensor
   if (millis() - prevMillis > 120000) {                    //write temps to database every 2 min
     prevMillis = millis();
     sprintf( statusstr, "?intemp=%d&outtemp=%d", InTempF, OutTempF );
     es.ES_client_browse_url(PSTR(HTTPPATH1), statusstr, PSTR(HOSTNAME), &browserresult_callback);
  }
 lcd.setCursor(0,3);
 lcd.print("In: ");
 lcd.print(InTempF);
 lcd.print(" Out: ");
 lcd.print(OutTempF);
 lcd.print("    ");
 }

Define your a0 and a1 to be float and you will be fine. It's just an integer operation thing that you can read about online. a0/readcount only gives quotient, the integer part of the division.

Thanks! I made that change and I'll keep an eye on the output over the next couple hours.

besides changing An0 to float as liudr suggested, you should postpone the division. NB - analogRead(0)/readcount - will still be executed as an integer division. So you have to change:

  for (int i = 1; i < readcount; i++){
    An0 += (analogRead(0)/readcount);
    An1 += (analogRead(1)/readcount);
    }

to

  for (int i = 1; i < readcount; i++){
    An0 += analogRead(0);
    An1 += analogRead(1);
    }
An0 /= readcount;
An1 /= readcount;

Good eye! I didn't catch that.

Hi guys,
I found another inconsistency in my code, which I thought I would share in case anyone refers back to this at some point. I noticed that when I changed how many times I would read the analog sensors, my results would vary up and down accordingly. A simple oversight, was that in the line: "for (int i = 1;i<readcount; i++){ " this means that it is cycling thru the reads one less time than it is diving by. Oops! so, this should read "for (int i = 0;i<readcount; i++){ ".

void writeTemps() {
  float An0 = 0;
  float An1 = 0;
  static uint32_t prevMillis;
  int readcount = 8;                           // number of times to read the analog sensors and average for accurate results
  for (int i = 0; i < readcount; i++){
    An0 += analogRead(0);
    An1 += analogRead(1);
    }
  An0 /= readcount;
  An1 /= readcount;  
  InTempF = (((((5*(An0*97))/1024)*9)/5)+32);
  OutTempF = (((((An1-200)/4)*9)/5)+32);        //Phidgets 1114 sensor formula to C is (sensorVal-200)/4
  if (millis() - prevMillis > 120000) {                   //write temps to database every 2 min
    prevMillis = millis();
    sprintf( statusstr, "?intemp=%d&outtemp=%d", InTempF, OutTempF );
    es.ES_client_browse_url(PSTR(HTTPPATH1), statusstr, PSTR(HOSTNAME), &browserresult_callback);
  }

That's right! 8)

Thanks for the correction. I was working on an A/D sampling project yesterday and used your code from the other day and was getting inconsistent behavior. This should help me out, thanks.

Adding a low-pass filter on the analog pin can do the job in hardware, typically temperature is a low bandwidth signal so a long time-constant RC filter can drastically reduce the noise. By chosing the right level of noise and combining with the multiple sampling technique you can increase the precision to better than 10 bits too ;)