Modelling Temperature Sensor Delay

Hi all, I'm incredibly new to programming and trying to figure out how to compensate for temperature sensor delay in my program. My problem is that I don't know how to store the previous reading and recall it for use in the equation.

The equation is basically Temperature = TempRead + (TempRead - TempPrev) where Temperature is a proposed temperature, TempRead is the current reading and TempPrev is the previous reading.

Can anyone help point me in the right direction?


  TempPrev = getTemp(); // function that wraps the reading of the temperature
  TempRead = getTemp();
  Temperature = TempRead + TempRead - TempPrev;
  TempPrev = TempRead;

That code will do what you asked for, but what you asked for doesn't seem to actually make sense.

What exactly are you trying to calculate with that equation ?

Robtillaart - thanks so much for your help!

It's part of a larger system, I'm just trying to minimise the delay of the temperature sensor by predicting what the temperature actually is.

So you are adding an estimate of the rate of temperature change, to the latest reading ? To try to predict what the following reading will be ?

At the rate at which a microcontroller would be reading the temperature, that hardly seems necessary. You may end up simply confusing yourself by amplifying the "noise" which will be present in the temperature readings.

Temperatures are slow moving inputs compared to the speed of any control devices, so the issue is not generally to predict the temperature changes but to smooth out any of the noise that happens on the input from the sensor. This is usually done by taking an average of a number of values over a set period of time and calling that the 'tempertaure'.

The equation is basically Temperature = TempRead + (TempRead - TempPrev)

How about this?

unsigned short Temperature(void) {
  static unsigned short TempPrev=0;
  unsigned short TempRead = getTemp(); //read the temperature
  unsigned short tmp = TempRead + (TempRead - TempPrev);
  TempPrev = TempRead;
  return tmp;

All if wrap'd in the function call.

Thanks everyone for your help!

It's for a uni project where they specifically want the temperature change to be predicted in the equation I gave, otherwise I would do what you guys are suggesting :)

This is where things like the PID library can also come in handy.

"It's for a uni project where they specifically want the temperature change to be predicted in the equation"

Temperature = TempRead + ( TempRead - TempPrev )

It seems to me that this equation does not (a) estimate the actual temperature (b) estimate the temperature delta since the last reading (c) estimate the rate of change of the temperature (d) make a useful prediction of the temperature.

It doesn't seem to do any of those things.

Just because "they" want something, doesn't make it right.

Temperature = TempRead + ( TempRead - TempPrev )

it is a crude form of forward error estimation. What it does is to say that the same "error", as defined by TempRead - TempPrev, will persist for the next read, which is generally true and helpful for slow-moving measurements, like temperature.

However, in cases of very high gain (very frequent temperature readings), this approach will yield unstable readings - it is essentially the D in a pid controller.

A different from of it would go like this:

Temperature = TempRead + (( TempRead - TempPrev ) - (TempPrev - TempPrev2)); where TempPrev2 is the temperature two readings ago.