Calculate Average Temperature

bpowell

Senior Member
Hello All,

I'm currently wrapping up a project, but having a little trouble with what seems like should be a simple aspect.

I'm monitoring a temperature probe, and would like to calculate the average temperature of a day (from midnight to midnight).

So, every 5 minutes, I'm measuring the temperature, and adding it to a word variable (TempAverage)...I'm also incrementing the AverageCount variable...so at the end of the day, I should be able to take my TempAverage word, and divide by AverageCount, and arrive at my average...right?

That's all well and good...and appears to work. (I should note I'm not concerned about decimals...just the integer number is fine).

However, It's coming up on winter, and I need to factor in negative temperatures as well...this is where I'm getting muddled up.

I'm using the trusty DS18b20 temperature sensors...which reports a negative temperature as 128 + negative value... (so, -1 would be 129).

My code looks for this, and subtracts the value from the TempAverage (129 would result is a subtraction of 1 from the TempAverage).

However, How to I at the end of the day, calculate the average? How do I tell if the average is positive or negative?

I have a feeling I'm overlooking something very simple...so please feel free to smack me on the back of the head if that's what's needed...I'm just stumped here.

Thanks all!

Brendan
 

Adamey

Senior Member
Use a number like 32,000 as 0 for your TempAverage variable. Anything over 32,000 is positive, anything under is negative.
 

manuka

Senior Member
Brrr- negative temps (assumed °C)- where DO you live!? If you're going to juggle the numbers then perhaps a better approach may be to convert temps to Absolute values (°Kelvin). That way at a glance you'll be able to see such key ones as 0° Celsius (being 273 Kelvin of course). Even 40 below will still be a very positive 233 Kelvin.

Mmmm -at 12 an hour over 24 hours you'll have 288 daily temp. readings. How are you storing them, and which PICAXE ? Do you really need so many? Max. & min are often quoted for a regions temps, so is average really the best thermal expression anyway? Stan. in summery NZ (25°C at my PC)
 
Last edited:

bpowell

Senior Member
Mmmm -at 12 an hour over 24 hours you'll have 288 daily temp. readings. How are you storing them said:
Hi Manuka,

I just figured a reading every 5 minutes would give an accurate average over the course of a day. I'm storing the temperature calculation in a word variable in the picaxe...so it'll be lost if there is a power drop...so that's why I'm also keeping track of the "average count"...at the end of the day, I'll divide average-total by average-count, and life is good.

I'm tracking Min, Max, and Average, and writing all to eeprom. Why do you ask? Heck, I don't know...just for the giggles...but this part has stumped me.

I think using the offset of 32000 might be the answer!
 

bpowell

Senior Member
Use a number like 32,000 as 0 for your TempAverage variable. Anything over 32,000 is positive, anything under is negative.
By George! I think this might be the solution! If so, I'm adding your name to the remarks of my code...you'll be immortalized forever!
 

manuka

Senior Member
I'm storing the temperature calculation in a word variable in the picaxe...so it'll be lost if there is a power drop...
To me a sounder way may be to record less frequently and stash directly into EEPROM. This not only gives insurance against power loss, but storage can even be the EEPROM of the PICAXE itself (if your program is modest), thus simplifying your hardware. See a past barebones 08M approach for ~a reading a minute for 2 hours (to suit a thermal cooling experiment), with layout here. N.B. As the hot water never cooled below ~20°C the usual DS18B20 -ve temp. tweak was ignored.

FWIW-I seem to have spent most of my life noting temperatures,both ambient & scientific, and respectfully report that sudden changes are quite rare in most everyday situtations. Hence the room temp. at my PC has changed by a mere 0.2 °C (from 25.0 to 24.8°C) in the last ~90 minutes. Perhaps instead READ the temp. every minute but only WRITE it when such dramatic changes arise? Sure -sudden cold snaps may occour,but they're unusual in most outdoor environments. Even the Neanderthals' demise apparently took several decades! Stan.
 
Last edited:

westaust55

Moderator
Summation part and negative identification is easy and does not need a 32,000 offset.

288 readings per day even if you were on a temp of 50 deg C for the entire day would sum to only 14,400.

Then at the other extreme, if you start at zero and had 288 readings at -20 deg C for the entire day results in -5760 which with roll over of the integer maths becomes 65536 – 5760 = 59776.
So your maths only need look at the highest bit of the temp summation. If it is a “1” then you have negative number (= 65536 – temp_summation)
 

bpowell

Senior Member
Hi Manuka,

Thanks for the post. Although I recognize extreme temperature shifts rarely happen...it's very easy for the temperature to hover around 0 or so in my neck of the woods...so I need to code and prepare for a temperature than can go from positive to negative overnight.

I suppose I could sore my temps in an eeprom setting on the picaxe...but at the end of the day, it's not that critical to me....so I'm storing in RAM....and if there is a power loss...so be it...that's why I keep track of the number of times the average has been updated!

Brendan
 

bpowell

Senior Member
So your maths only need look at the highest bit of the temp summation. If it is a “1” then you have negative number (= 65536 – temp_summation)
Hi Westaust,

This is what I thought at first as well...but when you put it into practice, the logic falls apart. Say you have temps hovering around 1 to 0 C...then they drop briefly to -1....the average will get all monkeyed up. the offset prevents such problems from occurring. I've run a lot of numbers through excel proving my point...let me know if I'm off track.

Thanks!
 

Dippy

Moderator
Two's complement might sort your woes.
If you do all your additions in T'sC then your pos and negs will be sorted automatically (assuming I haven't misunderstood the question which is highly likely).
 

hippy

Ex-Staff (retired)
I'm with westaust55 and Dippy; start at zero and use two's complement. If temperature sample is negative convert to a two's complement equivalent, add that sample to the total.

At the end of the day if the total is negative print "-", convert to its positive value. Print the total divided by number of samples.

I think you are off-track with your negative throwing averages, but it perhaps depends what you mean by average. If you sample every hour ...

10 10 10 0 0 -1 0 0 10 10 10 10

Your average = 69/12 = 5.75 by my calculation. What would you expect it be ?
 

techElder

Well-known member
Here's my totally subjective ' 2 degrees ' ...

What does the 'average' temperature tell you anyway?

  • It's not a temperature that makes you hot or cold.
  • It isn't the temperature that freezes your pipes or boils your water.
  • Stick your toe in a tub of water. Does your toe feel 'average'?
What's the old 'average' temperature good for anyway? :D
 

hippy

Ex-Staff (retired)
What's the old 'average' temperature good for anyway?

I'd say it was more; What's the 'daily average' temperature good for ?

Over a smaller or rolling period I can see more usefulness so perhaps it's just a question of what period.
 

manuka

Senior Member
This was largely my point too. Average/mean expressions are a statistical blackhole of course, as extreme values will cause bias. Median is often preferred instead. In this thermal case this is further compounded by the "temp hovering around freezing". A slightly subzero average may be a result of a short & sharp chill, and not really representative when it comes to considering burst pipes etc due to thermal lag issues. A better measure may relate to the extent of time such sub zero conditions are experienced.
 

Dippy

Moderator
Maybe he's doing some statistical analysis?
Maybe he just wants to?

All these stats MEAN (haha) something. Daily/weekly/monthly/yearly temperature stats are quoted all over the place whether (haha) they be for Greenies getting on bandwagons or holiday brochures.

Much of it is in how you present your data and how others interpret these data.
 

KeithRB

Senior Member
Here's my totally subjective ' 2 degrees ' ...

What does the 'average' temperature tell you anyway?

What's the old 'average' temperature good for anyway? :D
It gives you a number to answer when someone asks "How cold was it yesterday?" But I think High and Lows give more information.
 

marks

Senior Member
tHE TEMP was above average today lol

Code:
Celsius:
Readtemp12 c.7,w1                                   'read in result ds18b20
 convert:                                           
W1 = W1 + 320/16                                   '  info      -20 degrees will equal 0 degrees
                                          
BINTOASCII w1,b8,b7,b6,b5,b4  
SERTXD (CR, LF, "Temperature  ",B6,b5,b4,"  Degrees C", CR, LF)
PAUSE 1000
GOTO celsius
it would be a lot easy to use readtemp 12
and add the offset at sensor level( here we have added 20 degrees)

for example 40 degrees becomes 60 degrees
and minus 18 becomes 2 degrees
this will reduce the maths so you only deal with positive numbers lol
just remember to subtract 20 from you final result .
 

bpowell

Senior Member
Hi All,

Thanks for all of the great ideas...

As for why I'm recording the average....why not? I'd like to be able to look at a few years data and see if I can spot trends, etc. for my area...It's just something fun to do...I enjoy the mental exercise.

I'm currently working with the 32000 offset...however, I'll read up on two's compliment and see how I can implement that. I'm just not able to wrap my head around it at the moment...but nobody has every accused me of being the sharpest knife in the drawer!

Thanks again everybody!
 

westaust55

Moderator
@BP,

Honestly, the use of the 2's compliment method is almost invisible.
See the attached spreadsheet extract to pdf which shows (I hope) the concept.

Code:
Pseudo code:
READTEMP pin, temp
IF  temp < 128 ; ie positive
Sum = temp + sum
Else
Sum = 128 &#8211; temp + sum
ENDIF
inc samples
then At the end to calc average
sign = &#8220;+&#8221;
IF Sum > 32768
Sign = &#8220;-&#8220;
Sum = - Sum
ENDIF
Average = Sum / Samples
 

Attachments

Last edited:

premelec

Senior Member
@Tex... the average temperature is very useful in energy gain and loss calculations - hence we have 'degree days' - also of some use in agriculture in regard to seed germination and plant growth - though these are not linear but weighted values. However anyone can collect and calculate anything and have fun - not everything is of utility! :)
 
Top