I'm working in the development of a pH meter, and I have a doubt regarding offset_voltage correction in the pH calculation, because there is a sign in an equation which is different in the circuit note and the code from github corresponding to it .
In the library CN0398.cpp from EVAL-ADICUP360 ADuCM360_demo_cn0398 , in line 216, CN0398::read_ph(float temperature) function, the equation to calculate pH is:
float m = (calibration_ph - calibration_ph) / (calibration_ph - calibration_ph);
ph = m * (volt - calibration_ph + offset_voltage) + calibration_ph;
where the variables are:
calibration_ph : pH value of second calibration point
calibration_ph : pH value of first calibration point
calibration_ph : voltage value of second calibration point
calibration_ph : voltage value of first calibration point
volt : measured voltage
Why is the offset being added and not subtracted?
CN-0398 Circuit note in page three shows the equations used to get this. First, it calculates the slope m diving voltages by pH values, which is just the opposite as it's done in the code above, where pH values are divided by voltages. (equation 2)
Then, equation (3) is supposed to be the way of getting the unkown pH:
x = ((y - y1) + m * x1 - b) / m
where b is the offset_voltage, x the unknown pH, y is the measured voltage, x1 the pH at calibration point 1, y1 is pH value at calibration point 1, and m the slope.
If I substitute x by ph, y by volt, and m by 1/m into equation (3), to compare the equation in the code with the one in the circuit note, I get:
ph = ((volt - y1) + (1 / m) * x1 - b)) / (1 / m)
If I multiply by m, I get:
ph = m * (volt - y1 - b) + x1,
which is: ph = m * (volt - calibration_ph - offset_voltage) + calibration_ph;
I'm not sure how equation 3 is calculated, and I would appreciate if steps from equation (2) to equation (3) could be explained in more detail, but it makes sense to subtract the offset instead of adding it.
Thus, is the sign of the offset_voltage in the code wrong? I have checked also in ADuCM360_demo_Smart_Greenhouse CN0398.c library, and the offset is also added and not subtracted.
I would very much appreciate if anyone could clarify this issue.
Many thanks and best regards,
Thanks for having an interest in this referfence design.
regarding the derivation of equation 3 found in the circuit note, the equation was derived from the slope intercept form (y = m*x +b) of a linear function where y = Y - Y1 and x = X - X1 (note: Y is the measure voltage, Y1 is the measured voltage at first point, X is the unknown pH, X1 is the known pH at first point)
after x and y have been substituted to the equation of the intercept form:
(Y - Y1) = m * (X - X1) + b
and from this equation, you can derive X as a function of (Y, Y1, m, b) which gives you the following equation that is found in equation 3 from the circuit note:
X = [(Y - Y1) + m*X1 -b)]/m
Hope that helps.
Thanks for the clear explanation Erbe.
Ok, now I see where equation 3 comes from.
My main doubt is, in the code from the library, shouldn't the offset_voltage be subtracted instead of added?
In the code, m is calculated inversely, dividing ph by voltage. Thus, if I substitute m by 1/m in your equation, I get:
X = [(Y - Y1) + (1/m)*X1 -b)]/(1/m)
which results in:
X = m*(Y - Y1 - b) + X1
while in the code the equation is different, being:
X = m*(Y - Y1 + b) + X1)
Is the library wrong?
Thank you for pointing out the issue.
Indeed, the pH formula is wrong in the CN0398 source code.
This is fixed with https://github.com/analogdevicesinc/EVAL-ADICUP360/pull/15
Thank you and have a nice day!
Thanks for the confirmation, Amiclaus!
Best regards to all the team,