# how to get an average read of a sensor

how would I take an average over 25 reads over 1 minute and write that to the serial monitor?

this is my temp sensor code

`````` float temperature;
int lm35pin = A5;

void setup()
{

Serial.begin(9600);
}

void loop() {
temperature = (5.0 * reading * 100.0) /1023 ;

Serial.print("   TEMP:");
Serial.println(temperature);
delay(10000);
}
``````

Since you want 25 reads over 1 minute, you need to sample at 60/25 = 2.4 second intervals.
So, put your measurement code in a loop that checks when the time, micros(), changes by that amount from the last sample you stored in a 25 element array. You will need a pointer to go from 24 back to 0 to keep the pointer inside the limits of the array. Once you get 25 samples you can sum all the elements of the array and divide by 25 for the average. By resetting the pointer to the beginning, your array keeps the last 25 samples by writing over the outdated sample.

If you want to get fancy and don't want to compute the average by summing up values, you can keep a running total of the last 25. This requires subtracting the outdated sample and adding the new sample in the array. It's tricky, but can be done and is faster.

Be aware that the maximum value of an integer is 32,767. Although the maximum sum of the 25 readings (1023 * 25) will fit in an integer, doing any multiplication may exceed the maximum and give unexpected results.

im just trying to get a smoother reading from the lm35 sensor. It took me a while to get it working properly.
all the libraries out there give wacky readings. So I found with a 10 second delay it was a bit better but not more accurate. with the delay it just dosnt print all the different readings.

is averaging the reads out over say a 1 minute or 30 second period the best way to be more accurate?

Averaging just filters out noise like rounding errors and noise from the sensor. Averaging makes things look more settled (less-changing) but the down side is that it introduces a delay. Just depends on what your requirements are.

Did some searching and doing 25 readings and averaging it seems to give me a very smooth reading with very little change.

`````` void setup()
{

Serial.begin(9600);
}

void loop() {

float temperature;
int lm35pin = A5;

// (Temp Sensor)average
int count = 25;
int raw = 0;
for (int i=0; i< count; i++) raw += analogRead(lm35pin);
raw = raw / count;
temperature = (5.0 * raw * 100.0) /1023 ;

Serial.print(raw);
Serial.print("   TEMP:");
Serial.println(temperature);
delay(5000);
}
``````

" maximum value of an integer is 32,767" for a signed integer, yes.

unsigned int can be 65535, (2^16)-1

DaleScott:
Since you want 25 reads over 1 minute, you need to sample at 60/25 = 2.4 second intervals.
So, put your measurement code in a loop that checks when the time, micros(), changes by that amount from the last sample you stored in a 25 element array. You will need a pointer to go from 24 back to 0 to keep the pointer inside the limits of the array. Once you get 25 samples you can sum all the elements of the array and divide by 25 for the average. By resetting the pointer to the beginning, your array keeps the last 25 samples by writing over the outdated sample.

If you want to get fancy and don't want to compute the average by summing up values, you can keep a running total of the last 25. This requires subtracting the outdated sample and adding the new sample in the array. It's tricky, but can be done and is faster.

If you are storing the values in an array you will be using an index, not a pointer.