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