Sensor Calibration

Place code snippets and demo code here
Post Reply
BeanieBots
Posts: 344
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 182 times
Been thanked: 112 times

Sensor Calibration

Post by BeanieBots »

Not many people have access to a calibrated source to get their sensors calibrated and to be honest, in most cases it doesn't really matter. However, I think it's always nice to at least have all your sensors showing the same value for the same temperature or humidity.
With that in mind, this is more of an excercise in algebra than a code example. Hopefully, some might find it useful.

In most cases, any error can simply be compensated for by adding or subtracting a small offset. This will work for an indoor sensor that does not see a large range but an outdoor sensor will also need compensation for gain. So here's how to do the maths:-
I will only cover linear compensataion of the type y=mx+c.
As there are two unknowns in our equation, we will need two sets of values. These need to be as far apart as possible. For temperature, one could be done in a warm place such as an airing cupboard and the other in a fridge or freezer.

Now for the equations with long meaningful names;)

MeasuredHigh 'The higher value shown by the device you wish to calibrate.
MeasuredLow 'As above, but the lower value.
ActualHigh 'The value shown by the sensor that you want to match.
ActualLow 'As above, but the lower value.

From y=mx+c, we get Actual=Gain*Measured+Offset
With our two sets of data, we now make two equations and solve them to get gain & offset.

Equation 1: ActualHigh = Gain*MeasuredHigh+Offset
Equation 2: ActualLow = Gain*MeasuredLow+Offset

We can eliminate the offset by subtracting equation 1 from equation 2 which gives:-
ActualHigh - ActualLow = Gain*MeasuredHigh + Offset - Gain*MeasuredLow + Offset
Simplifying gives:-
ActualHigh - ActualLow = Gain(MeasuredHigh - MeasuredLow)
Solving for gain gives:-

Gain = (ActualHigh - ActualLow) / (MeasuredHigh - MeasuredLow)

We can now substitute this expression for gain into one of the earlier equations to solve for offset.

ActualHigh = ((ActualHigh - ActualLow)/(MeasuredHigh - MeasuredLow))*MeasuredHigh + Offset
Rearange to solve for Offset:-

Offset = ActualHigh - MeasuredHigh*((ActualHigh-ActualLow)/(MeasuredHigh - MeasuredLow))

You can either take some readings and pop them into a calculator or write a simple little program as shown below to get values for gain and offset.

For temperatures, if you really want to get accurate values, you can use boiling water to give 100'C and ice water to give 0'C.

For humidity, it takes a little more effort.
A saturated salt solution (with a little extra salt) will give 75.47%RH at 20'C
Dry Potassium Acetate (available from most chemists) will give 23.11%RH at 20'C

Worked example:-

Let's say your sensor gives 102 when the actual is 100 and -0.3 when the actual is 0.2

Gain = (100 - 0.2)/(102 - (-0.3)) = 99.8/102.3 = 0.97556207
Offset = 100 - 102 * ((100 - 0.2)/(102 - (-0.3))) = 100 - 102 * (99.8/102.3) = 0.49266862

Code: [Local Link Removed for Guests]

'********************************************************
'*                                                      *
'*  Calculation of m & c for y=mx+x compensation        *
'*  V1.0.0 15/12/2022 Firmware 1.44.2                   *
'*                                                      *
'********************************************************

'Enter your readings as described below. 
AH = 100   'The actual high temperature
MH = 102   'The high temperature reported by your sensor
AL = 0.2   'The actual low temperature
ML = -0.3  'The low temperature reported by your sensor

Gain = (AH-AL)/(MH-ML)
Offset = AH - MH*(AH-AL)/(MH-ML)

Wlog "Gain = ";Gain
Wlog "Offset = ";Offset

'Test an example of the y=mx+c correction
ExampleTemperature = 102

CorrectedTemperature = ExampleTemperature * Gain + Offset

Wlog
Wlog "Example   = ";ExampleTemperature
Wlog "Corrected = ";CorrectedTemperature
User avatar
cicciocb
Site Admin
Posts: 1997
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 428 times
Been thanked: 1336 times
Contact:

Re: Sensor Calibration

Post by cicciocb »

Thanks for this post BeanieBots,
I can also add that there is another method using a polynomial expression.
Something in the style of y=c0 + c1*x + c2*x^2 + c3*c^3 ......
Even if it can be a little scary, there are several websites that allow you to automatically calculate the coefficients of the polynomial by providing a calibration table.

For example these sites :
https://mycurvefit.com/
https://stats.blue/Stats_Suite/polynomi ... lator.html
https://arachnoid.com/polysolve/

The result is generally very accurate and quite simple to implement.
BeanieBots
Posts: 344
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 182 times
Been thanked: 112 times

Re: Sensor Calibration

Post by BeanieBots »

Indeed cicciob, I agree that a polynomial solution is much better but can sometimes be a little processor intensive.
The best middle ground I find tends to be a quadratic. AX^2 + BX + C.

Edit:-
Just got round to checking out the links you posted. Very useful, especially the last one. Thanks.
Post Reply