Go Down

Topic: Compensating for temperature drift with 2-dimensional array (Read 760 times) previous topic - next topic


Aug 06, 2012, 03:58 am Last Edit: Aug 06, 2012, 04:57 am by 81Pantah Reason: 1
I am having difficulty getting my head around the right way to deal with a sensor drift issue I have. The reading from my light sensor varies with the environmental temperature. The manufacturer provides a graph that plots the offset in degrees for a spectrum of temperatures. The temperature offset is non-linear, so I can use the standard map() function.

I added a temperature sensor to my circuit and now have access to a temperature variable. What I do with this? not too sure...

After reading up a bunch on seemingly-random methods for doing this, I am leaning towards trying to implement a linear interpolation solution. I have a two-dimensional array that contains both the temperature and offset variables from the manufacturer. Here it is:

Code: [Select]
float tempcompArray[2][21] = {{-25.0000,-20.0000,-15.0000,-10.0000,-5.0000,0.0000,5.0000,10.0000,15.0000,20.0000,25.0000,30.0000,35.0000,40.0000,45.0000,50.0000,55.0000,60.0000,65.0000,70.0000,75.0000},

Here is where I am stuck: how do I map from my super accurate temperature reading to return an interpolated offset? It seems like I need to be interpolating all over the place. The measurements from the manufacturer are taken every 5 degrees C. So I have 21 offset values corresponding with the factory measurements. But, I need to arrive at something that is an integer to be able to access the array element, right?

Argh, I am a bit lost. So much so, that I can't even figure out what to Google to find a better example.

Any guidance would be greatly appreciated.

Thank you.


Search for "linear interpolation" and "linear interpolation arduino".
Can you give a link for that sensor ?



I would do it like this:

You need to decide how to represent your correction. Is it best to scale the reading to get the actual value, or add/subtract a constant value, or something else? This will hopefully be obvious from the manufacturer supplied correction data.

Take values from the manufacturer supplied correction curve at regular temperature increments - 5C or whatever. The number of values you need and spacing between samples would be determined by how non-linear the correction curve is, and how accurate you want your answer to be.

Represent each value as an integer number. You will need to decide the best way to represent these values as ints - if they are tiny, you might need to scale them up by x100 or x1000 so that the resolution of an integer is enough to get the accuracy you are aiming for.

Store these numbers in an array. The index into the array will be the temperature, rounded down to the nearest 5C (or whatever increment you're using). The array entry at that index will be the correction number at that temperature.

When you take a light and temperature reading, you will round the temperature up and down to the nearest 5C increment (or whatever) above and below that temperature. Read these two numbers from the array. Now do a linear interpolation between these two numbers to find the number corresponding to your actual temperature.

Use that number to adjust the light reading.


Alternatively, use Excel's solver functionality to come up with a formula that fits the manufacturers compensation curve and code that in as the adjustment you need to raw temp data.

Go Up