how to read correctly from a voltage divider


hello everyone,

i'm building device simple battery level indicator. there voltage divider bound analog input 8.
i several readings arduino , averaging them.

and strange thing: averaged value varies number of readings.

here (simpliefied) source:

code: [select]
int   battmeasurecount = 0;
float battvolts = 0;
float battvoltsmedian = 0;
int   battsamples = 10;

void batteryupdate() { // called @ every cycle
  
 if (battmeasurecount <= battsamples) { // battsamples readings...
  battvolts = battvolts + 12.10 * analogread(8) / 1024; // empirical formula
  battmeasurecount++;
 }
 else
   { // reached desired samples count...
   battvoltsmedian = battvolts / battsamples; // computing mean value

    serial.print("battvoltsmedian="); serial.println(battvoltsmedian);

    // resetting variables
    battmeasurecount = 0;
    battvolts = 0;
   }

}

void setup() {
 pinmode(8, input);
}

void loop() {
batteryupdate();
}


and results:

with battsamples=10   -->  battvoltsmedian=9.82
with battsamples=500  -->  battvoltsmedian=8.90

with higher number of readings (500 or more), value stabilising. measured multimeter, gave same value (approx. 8.90 v).

i cannot understand, since device fed stabilised power source, why reads high value (9.92) 10 samples.


is other way better compute mean value, statistical ?
maybe combine last computed mean value current reading.
so there won't series anymore, continuous reading , average computing.




regards,
ciprian


you should post code between code tags, </> in 'post' window, rather inline in post. it's not bad idea read "how use forum" post @ top of section's index page posting guidelines.

before else, arduino using?


Arduino Forum > Using Arduino > General Electronics > how to read correctly from a voltage divider


arduino

Comments