Average temperature calculation using LM35 and Nokia N5510 LCD display

Please can someone help me?
My mission is to write a code that can calculate average temperature from a sensor (LM35) after like 1 minute and display the result on N5510 LCD display. I am using embed LPC 1768. I managed to display the temperature in degrees C, F and K using Craig Evans’ N5510 examples but I don’t know how to make it calculate average temperature. I am a complete beginner in this field.

my code is:

AnalogIn LM35(p15);
int temperature_C = ((LM35.read()*3276)/10);
int length = sprintf(buffer,“T C= %2d C”,temperature_C);
if (length <= 14)

    int temperature_f=(9*temperature_C)/5 + 32; 
    length = sprintf(buffer,"T F = %.2d F",temperature_f);
    if (length <= 14)
    int temperature_K = temperature_C + 273.15;
        length = sprintf(buffer,"T K= %2d K",temperature_K); 
    if (length <= 14)  

Thanks in advance.

Calculating an ‘average’ implies that you have a number o f temperature values (say n) collected over an interval of time. The average is simply the sum of all those temperature values divided by their number

avg = sum(To … Tn)/n

eg: Say you have 5 samples taken at 12 sec intervals (so 1 minute of elapsed time) - values in DegC:

Temp1 = 25.6
Temp2 = 25.4
Temp3 = 26.1
Temp4 = 25.8
Temp5 = 25.6

Average = Sum(Temp1 … Temp5)/5
= 25.7

You only need to do this for the DegC values as since you derive the others from this one.

It means that your lcd.printString calls are only necessary after every 5th sample (in this example).

An alternative method is a so-called ‘moving average’ where you can update the value at every sample interval and it gradually arrives at the final value.

/* Get moving average of input value */
moving_avg = moving_avg_previous + [current_measurement] - moving_avg_previous/NSAMP;
moving_avg_previous = moving_avg;

val = moving_avg/NSAMP; // save averaged value

Where NSAMP is the number of samples to average. The higher this value the longer it takes to reach the average. For a measurement like temperature try values like 4 … 8.

This has the effect of a low-pass filter where a sudden large change in temp will look like a slow rise (or drop) to the new average value.

Hope this helps!

Thank you so much for the wonderful idea. I appreciate.

I used the first method and it works perfectly.
Thanks once again.

Your problem is sorted out?

Yes of course…