# How to use this calibration formula to calibrate a sensor ?

Hi expert,

i’m using an MQ3 sensor. I have the formula shown down to calculate the sensor value.
My question is how to use it to calculate a sensor response.
I try this code but it was not a success. Can you help me ???
Question : to use my formula, i will have some values calculated using the same sensor for differents solutions. The array obtained was :
float pts [4] [2] = {
{
C1,R1 }
, {
C2,R2 }
, {
C3,R3 }
, {
C4,R4 }
,{

};

The calibartion formula :
Log(C2 / C1) Log (R / R1) = Log (R2 / R1) Log (X / C1).

C2,C1,R1,R2 and R are known. R is the resistance calculated from the sensor with AnalogRead (A0).

I’m looking for a code which can depending of the value of R to fix the write interval for work :

Example : if R is between R2 et R3 the formula will be used using C2,R2 et C3,R3
than the result will be in this fonction:

``````for (int nn=0; nn<3; nn++) {

if (R >= pts[nn][1] && R <= pts[nn+1][1]) {
result = (log((pts[nn+1][0]/pts[nn][0])*log (R/pts[nn][1])+log(pts[0][nn+1]/pts[0][nn])*log pts[nn][0])/log (pts[nn+1][1]/pts[nn][0]));

Serial.print(" C =");
Serial.print("  ");
}
else
{
Serial.println ("Out of Limites ");
Serial.print (" ");

}
``````

Thanks for help.

Where did you get that calibration formula from? It is unnecessarily complicated.
It is the same as:

``````C2 * R = R2 * X
``````

If you are trying to solve for X, this becomes:

`````` X = C2 * R / R2;
``````

Pete

ok,

it's the formula from the constructor, the most important is how to fix the write interval of work ?

What constructor?

Pete

Figaro ( japan).

el_supremo:
Where did you get that calibration formula from? It is unnecessarily complicated.
It is the same as:

``````C2 * R = R2 * X
``````

I can’t find any reference to such a Byzantine calibration formula in the sensor literature or the data sheets, and it doesn’t make much sense, even as a randomly chosen equation to linearize a complicated dependence. What would X be, having units of capacitance? Unknown in an RC impedance bridge?

My math skills are a bit rusty but just for fun I tried to prove Pete’s equality and can’t. So I turned to my crutch (Mathematica) and assuming that the derived equality is correct, plugged it back into the original equation and solved for R. That yielded R=R2, which also doesn’t give much of a clue.

Pete: would you care to give any hints as to where I went wrong?

I would be curious to see what the circuit is and understand why C1 and R1 are irrelevant.

Cheers, Jim

*pdf attached of Mathematic output

Untitled-1.pdf (17.7 KB)

hello pete and jim,

i see you do many things for my request i will explain to you :

the sensor response is not linear, but it seems like a power fonction : that’s is the response will be linear if we apply the log function.
R1… Rn was the response of the sensor
C1…Cn was the concentration of different and know solution

R is the unknow solution resistance
C is the concentration which i’m looking for.

In my code i have a funtion of calibration where i put the different value of my known Resistance and concentration.
i have too a function using the calibration formula :

Log(C2 / C1) Log (R / R1) = Log (R2 / R1) Log (X / C1). In this formula just X is the unknow concentration.

According to that the sensor’s response is supposed Linear and i use this formula for formula.

i attached an example.

Enjoy

Calibration formula.pdf (54.7 KB)

Not at all what I had in mind! It would have helped if you had posted that sheet first, as the operation is just linear interpolation on a log-log plot. You have 5 measured quantities so just solve for X:

X = C1 * exp((Log(C2 / C1) Log (R / R1) /Log(R2/R1))

Pete: would you care to give any hints as to where I went wrong?

My (elementary) mistake. I had raised both sides to the power of ten to get rid of the logs but in one subsequent step I made the mistake of using 10^(ab) => 10^a * 10^b which isn’t correct. It should be 10^(ab) = (10^a)^b which doesn’t help reduce the equation further.

Pete

Hello pete and jim,

good reply . But my need is a code to use the log-log interpolation formula and where R is between R1 and R2 ? how i can chose the right interval ?????

Thanks

I don't understand. It doesn't matter if R is between R1 and R2, or not. You have two calibration points that determine the straight line form of the response, and that is what is being used to estimate X from R.

hello jim,

it's wrirght that R is between R1 and R2 but it can be between R2 and R3 or R3 and R4. i'm going to make an online sensor and after determination of R 1..R4 the code must select the interval of calculation depending of the value of R.

Thanks

One way to do this, from a practically infinite number of possibilities, is:
First, sort your R values so that R1 < R2 < R3 < R4, then

``````if ( (r >= r1) && (r <= r2) ) use_R1_and_R2_for_calculation();
if ( ( (r >= r2) && (r <= r3) ) use_R2_and_R3_for_calculation();
if ( (r >= r3) && (r <= r4) ) use_R3_and_R4_for_calculation();
if ( (r < r1) || (r > r4) ) error();
``````

etc.

ok Jim,