ph.cc – rb , rn, pH

ph.cc is a c++ program to take all the equations presented in the DIY Photometer paper and get to the bottom of how pH is computed from absorbance.    This it does, and in addition we use it to generate a table of rb, rn, and pH.  with rn as the independent variable.  We use the equations 1-8 in the paper as well as the calibration equation 8, which is the linear equation of the relationship of the broadband absorbance ratio, narrow band absorbance ratio.  Narrow band absorbance ratio relationship to pH is described in detail and allows direct calculation of pH from rn – the narrow band  absorbance ratio.

This program uses the calibration equation 8 for the original paper’s photometer device.  As they authors point out, the calibration is almost certainly going to vary even using components with the same part numbers and from the same manufacturers.  We use it here as an example and first estimate of the true calibration, which is yet to be determined.   The calibration should be in the ballpark, but it seems very unlikely to be good to go .  It is useful however, to model the expected behavior of the system under different conditions of  temperature T,  salinity S,  as well as electronic components.

Below is a run of the program from the command line.  We used a raspberry pi computer since it has a fully loaded c++ compiler.

pi@maui2:~/photom $ g++ ph.cc
pi@maui2:~/photom $ ./a.out |more
Program: ph.cc
e1 : 0.005926
e23 : 0.057525
a : -236.903265
equConstant : 7.596691
pH : 7.303157
rb,rn,pH
0.486882, 0.200000, 6.889684
0.495291, 0.210000, 6.911757
0.503700, 0.220000, 6.932786
0.512109, 0.230000, 6.952867
0.520518, 0.240000, 6.972082
0.528927, 0.250000, 6.990504
0.537336, 0.260000, 7.008196
0.545745, 0.270000, 7.025215
0.554154, 0.280000, 7.041611
0.562563, 0.290000, 7.057429
0.570972, 0.300000, 7.072708
0.579381, 0.310000, 7.087485
0.587790, 0.320000, 7.101792
0.596199, 0.330000, 7.115659
0.604608, 0.340000, 7.129112
0.613017, 0.350000, 7.142176
0.621426, 0.360000, 7.154873
0.629835, 0.370000, 7.167224
0.638244, 0.380000, 7.179247
0.646653, 0.390000, 7.190960
0.655062, 0.400000, 7.202379
0.663471, 0.410000, 7.213517
0.671880, 0.420000, 7.224390
0.680289, 0.430000, 7.235010
0.688698, 0.440000, 7.245389
0.697107, 0.450000, 7.255537
0.705516, 0.460000, 7.265464
0.713925, 0.470000, 7.275182
0.722334, 0.480000, 7.284697
0.730743, 0.490000, 7.294020
0.739152, 0.500000, 7.303157
0.747561, 0.510000, 7.312117
0.755970, 0.520000, 7.320906
0.764379, 0.530000, 7.329530
0.772788, 0.540000, 7.337997
0.781197, 0.550000, 7.346311
0.789606, 0.560000, 7.354479
0.798015, 0.570000, 7.362506
0.806424, 0.580000, 7.370396
0.814834, 0.590000, 7.378154
0.823243, 0.600000, 7.385786
0.831652, 0.610000, 7.393294
0.840061, 0.620000, 7.400684
0.848470, 0.630000, 7.407958
0.856879, 0.640000, 7.415121
0.865288, 0.650000, 7.422177
0.873697, 0.660000, 7.429127
0.882106, 0.670000, 7.435977
0.890515, 0.680000, 7.442728
0.898924, 0.690000, 7.449383
0.907333, 0.700000, 7.455946
0.915742, 0.710000, 7.462419
0.924151, 0.720000, 7.468805
0.932560, 0.730000, 7.475105
0.940969, 0.740000, 7.481323
0.949378, 0.750000, 7.487460
0.957787, 0.760000, 7.493519
0.966196, 0.770000, 7.499502
0.974605, 0.780000, 7.505410
0.983014, 0.790000, 7.511247
0.991423, 0.800000, 7.517012
0.999832, 0.810000, 7.522709
1.008241, 0.820000, 7.528339
1.016650, 0.830000, 7.533904
1.025059, 0.840000, 7.539405
1.033468, 0.850000, 7.544843
1.041877, 0.860000, 7.550221
1.050286, 0.870000, 7.555539
1.058695, 0.880000, 7.560800
1.067104, 0.890000, 7.566003
1.075513, 0.900000, 7.571152
1.083922, 0.910000, 7.576246
1.092331, 0.920000, 7.581287
1.100740, 0.930000, 7.586276
1.109149, 0.940000, 7.591215
1.117558, 0.950000, 7.596103
1.125967, 0.960000, 7.600944
1.134376, 0.970000, 7.605737
1.142785, 0.980000, 7.610483
1.151194, 0.990000, 7.615184
1.159603, 1.000000, 7.619840
1.168012, 1.010000, 7.624452
1.176421, 1.020000, 7.629021
1.184830, 1.030000, 7.633548
1.193239, 1.040000, 7.638034
1.201648, 1.050000, 7.642479
1.210057, 1.060000, 7.646885
1.218466, 1.070000, 7.651252
1.226875, 1.080000, 7.655581
1.235284, 1.090000, 7.659872
1.243693, 1.100000, 7.664126
1.252102, 1.110000, 7.668345
1.260511, 1.120000, 7.672527
1.268920, 1.130000, 7.676675
1.277329, 1.140000, 7.680789
1.285738, 1.150000, 7.684869
1.294147, 1.160000, 7.688916
1.302556, 1.170000, 7.692931
1.310965, 1.180000, 7.696914
1.319374, 1.190000, 7.700865
1.327783, 1.200000, 7.704786
1.336192, 1.210000, 7.708676
1.344601, 1.220000, 7.712537
1.353010, 1.230000, 7.716368
1.361419, 1.240000, 7.720171
1.369828, 1.250000, 7.723945
1.378237, 1.260000, 7.727691
1.386646, 1.270000, 7.731410
1.395055, 1.280000, 7.735102
1.403465, 1.290000, 7.738767
1.411874, 1.300000, 7.742406
1.420283, 1.310000, 7.746019
1.428692, 1.320000, 7.749607
1.437101, 1.330000, 7.753170
1.445510, 1.340000, 7.756708
1.453919, 1.350000, 7.760222
1.462328, 1.360000, 7.763712
1.470737, 1.370000, 7.767179
1.479146, 1.380000, 7.770622
1.487555, 1.390000, 7.774043
1.495964, 1.400000, 7.777441
1.504373, 1.410000, 7.780817
1.512782, 1.420000, 7.784171
1.521191, 1.430000, 7.787504
1.529600, 1.440000, 7.790815
1.538009, 1.450000, 7.794105
1.546418, 1.460000, 7.797375
1.554827, 1.470000, 7.800624
1.563236, 1.480000, 7.803854
1.571645, 1.490000, 7.807063
1.580054, 1.500000, 7.810253
1.588463, 1.510000, 7.813424
1.596872, 1.520000, 7.816575
1.605281, 1.530000, 7.819708
1.613690, 1.540000, 7.822822
1.622099, 1.550000, 7.825918
1.630508, 1.560000, 7.828996
1.638917, 1.570000, 7.832056
1.647326, 1.580000, 7.835099
1.655735, 1.590000, 7.838124
1.664144, 1.600000, 7.841132
1.672553, 1.610000, 7.844123
1.680962, 1.620000, 7.847098
1.689371, 1.630000, 7.850056
1.697780, 1.640000, 7.852997
1.706189, 1.650000, 7.855923
1.714598, 1.660000, 7.858833
1.723007, 1.670000, 7.861727
1.731416, 1.680000, 7.864605
1.739825, 1.690000, 7.867468
1.748234, 1.700000, 7.870316
1.756643, 1.710000, 7.873149
1.765052, 1.720000, 7.875968
1.773461, 1.730000, 7.878771
1.781870, 1.740000, 7.881561
1.790279, 1.750000, 7.884336
1.798688, 1.760000, 7.887097
1.807097, 1.770000, 7.889843
1.815506, 1.780000, 7.892577
1.823915, 1.790000, 7.895296
1.832324, 1.800000, 7.898002
1.840733, 1.810000, 7.900695
1.849142, 1.820000, 7.903375
1.857551, 1.830000, 7.906041
1.865960, 1.840000, 7.908695
1.874369, 1.850000, 7.911336
1.882778, 1.860000, 7.913964
1.891187, 1.870000, 7.916580
1.899596, 1.880000, 7.919184
1.908005, 1.890000, 7.921775
1.916414, 1.900000, 7.924355
1.924823, 1.910000, 7.926922
1.933232, 1.920000, 7.929478
1.941641, 1.930000, 7.932022
1.950050, 1.940000, 7.934554
1.958459, 1.950000, 7.937075
1.966868, 1.960000, 7.939585
1.975277, 1.970000, 7.942083
1.983687, 1.980000, 7.944571
1.992096, 1.990000, 7.947047
2.000505, 2.000000, 7.949513
2.008914, 2.010000, 7.951968
2.017323, 2.020000, 7.954412
2.025732, 2.030000, 7.956846
2.034141, 2.040000, 7.959269
2.042550, 2.050000, 7.961682
2.050959, 2.060000, 7.964085
2.059368, 2.070000, 7.966478
2.067777, 2.080000, 7.968860
2.076186, 2.090000, 7.971233
2.084595, 2.100000, 7.973596
2.093004, 2.110000, 7.975949
2.101413, 2.120000, 7.978293
2.109822, 2.130000, 7.980627
2.118231, 2.140000, 7.982951
2.126640, 2.150000, 7.985267
2.135049, 2.160000, 7.987573
2.143458, 2.170000, 7.989870
2.151867, 2.180000, 7.992157
2.160276, 2.190000, 7.994436
2.168685, 2.200000, 7.996706
2.177094, 2.210000, 7.998967
2.185503, 2.220000, 8.001219
2.193912, 2.230000, 8.003463
2.202321, 2.240000, 8.005698
2.210730, 2.250000, 8.007924
2.219139, 2.260000, 8.010142
2.227548, 2.270000, 8.012352
2.235957, 2.280000, 8.014554
2.244366, 2.290000, 8.016747
2.252775, 2.300000, 8.018932
2.261184, 2.310000, 8.021109
2.269593, 2.320000, 8.023278
2.278002, 2.330000, 8.025439
2.286411, 2.340000, 8.027592
2.294820, 2.350000, 8.029738
2.303229, 2.360000, 8.031876
2.311638, 2.370000, 8.034006
2.320047, 2.380000, 8.036128
2.328456, 2.390000, 8.038243
2.336865, 2.400000, 8.040351
2.345274, 2.410000, 8.042451
2.353683, 2.420000, 8.044544
2.362092, 2.430000, 8.046629
2.370501, 2.440000, 8.048708
2.378910, 2.450000, 8.050779
2.387319, 2.460000, 8.052843
2.395728, 2.470000, 8.054901
2.404137, 2.480000, 8.056951
2.412546, 2.490000, 8.058994
2.420955, 2.500000, 8.061031
2.429364, 2.510000, 8.063060
2.437773, 2.520000, 8.065083
2.446182, 2.530000, 8.067100
2.454591, 2.540000, 8.069109
2.463000, 2.550000, 8.071112
2.471409, 2.560000, 8.073109
2.479818, 2.570000, 8.075099
2.488227, 2.580000, 8.077083
2.496636, 2.590000, 8.079060
2.505045, 2.600000, 8.081031
2.513454, 2.610000, 8.082996
2.521863, 2.620000, 8.084955
2.530272, 2.630000, 8.086907
2.538681, 2.640000, 8.088854
2.547090, 2.650000, 8.090794
2.555499, 2.660000, 8.092728
2.563909, 2.670000, 8.094657
2.572318, 2.680000, 8.096579
2.580727, 2.690000, 8.098495
2.589136, 2.700000, 8.100406
2.597545, 2.710000, 8.102311
2.605954, 2.720000, 8.104210
2.614363, 2.730000, 8.106104
2.622772, 2.740000, 8.107992
2.631181, 2.750000, 8.109874
2.639590, 2.760000, 8.111750
2.647999, 2.770000, 8.113622
2.656408, 2.780000, 8.115487
2.664817, 2.790000, 8.117348
2.673226, 2.800000, 8.119202
2.681635, 2.810000, 8.121052
2.690044, 2.820000, 8.122896
2.698453, 2.830000, 8.124735
2.706862, 2.840000, 8.126568
2.715271, 2.850000, 8.128397
2.723680, 2.860000, 8.130220
2.732089, 2.870000, 8.132038
2.740498, 2.880000, 8.133851
2.748907, 2.890000, 8.135659
2.757316, 2.900000, 8.137462
2.765725, 2.910000, 8.139260
2.774134, 2.920000, 8.141053
2.782543, 2.930000, 8.142841
2.790952, 2.940000, 8.144625
2.799361, 2.950000, 8.146403
2.807770, 2.960000, 8.148177
2.816179, 2.970000, 8.149946
2.824588, 2.980000, 8.151710
2.832997, 2.990000, 8.153469
2.841406, 3.000000, 8.155224
2.849815, 3.010000, 8.156974
2.858224, 3.020000, 8.158720
2.866633, 3.030000, 8.160461
2.875042, 3.040000, 8.162197
2.883451, 3.050000, 8.163929
2.891860, 3.060000, 8.165656
2.900269, 3.070000, 8.167379
2.908678, 3.080000, 8.169098
2.917087, 3.090000, 8.170812
2.925496, 3.100000, 8.172522
2.933905, 3.110000, 8.174228
2.942314, 3.120000, 8.175929
2.950723, 3.130000, 8.177626
2.959132, 3.140000, 8.179318
2.967541, 3.150000, 8.181007
2.975950, 3.160000, 8.182691
2.984359, 3.170000, 8.184371
2.992768, 3.180000, 8.186048
3.001177, 3.190000, 8.187720
3.009586, 3.200000, 8.189387
3.017995, 3.210000, 8.191051
3.026404, 3.220000, 8.192711
3.034813, 3.230000, 8.194367
3.043222, 3.240000, 8.196019
3.051631, 3.250000, 8.197667
3.060040, 3.260000, 8.199311
3.068449, 3.270000, 8.200951
3.076858, 3.280000, 8.202587
3.085267, 3.290000, 8.204220
3.093676, 3.300000, 8.205849
3.102085, 3.310000, 8.207473
3.110494, 3.320000, 8.209095
3.118903, 3.330000, 8.210712
3.127312, 3.340000, 8.212326
3.135721, 3.350000, 8.213936
3.144131, 3.360000, 8.215542
3.152540, 3.370000, 8.217145
3.160949, 3.380000, 8.218744
3.169358, 3.390000, 8.220339
3.177767, 3.400000, 8.221931
3.186176, 3.410000, 8.223519
3.194585, 3.420000, 8.225104
3.202994, 3.430000, 8.226686
3.211403, 3.440000, 8.228264
3.219812, 3.450000, 8.229838
3.228221, 3.460000, 8.231409
3.236630, 3.470000, 8.232976
3.245039, 3.480000, 8.234541
3.253448, 3.490000, 8.236101
3.261857, 3.500000, 8.237659

This is pretty abstract but you may notice that for pH values from 7.6 to 8.2, the first three significant figures change quite slowly, which is encouraging and makes it plausible that 0.01 pH measurement accuracy is obtainable with the arduino based photometer.  In checking our built system behavior without a dye and just using water and coffee, we see very low absorbance ratio values (< 1.0) and a lively variance  of +- .05 or so.which is more like  a  +- 0.1 pH  accuracy down in the non-linear part of the pH / absorbency ratio curve.  It also explains why the measurement range is for pH 7.6 to 8.2 .

When we get to the lab, we’ll see whether we can get that  0.01 pH  precision by watching the RB variation, and at the same time, get enough data to compute the system linear correlation equation for RN and RB … ie.  calibrate the system.

 

 

/*
ph.cc
15 nov 16

formula translation to c++
test numerical methods described in equations 1-7
print out a table of pH vrs RN

Values of RN from .1-2.8 produce pH from 6.37 to 8.39 depending on T and S

RN range .8 – 2.6 Fig 3 range is 0.8 to 2.5

pH range: 7.6 – 8.2
S range: 30.0 – 36.2
T range: 15C – 30C ~= 288K – 304K

288K = 14.85C
300K = 26.85C
303K = 29.85C
304K = 30.85C

eq 8 :calibration equation of RB to RN
RB absorption ratio of diy broadband spectrometer
RN absorption ratio narrowband lab spectrometer)
*/
#include <cstdio>
#include <stdio.h>
#include <math.h>

const char* Program = “ph.cc”;

int main()
{
printf (“Program: %s \n”, Program);

double pH, equConstant,R, e1,e2,e3, e23;
double T, S; // temp and salinity

// set values for S and T
// for salinity S values from 20 to 40
// for temperatures T from 278.15 K to 308.15 K
S = 30;
//S = 40;
T = 280;
T = 303;

// RN is absorbance ratio
double RN = 0.5;

// eq 5
e1 = -0.007762 + (4.5174 / pow(10.0,5.0)) * T;
// e1 = -0.007762 + 4.5174 * T / pow(10.0,5.0) ;
//e23 = -0.020813 + 2.60262 / pow (10.0,4.0)*T + 1.0436 / pow (10.0,4.0) *(S-35);
//e23 = -0.020813 + ((2.60262 / pow (10.0,4.0))*T) + ((1.0436 / pow (10.0,4.0)) * (S-35)) ;
e23 = -0.020813 + (2.60262 / pow (10.0,4.0))*T + (1.0436 / pow (10.0,4.0)) * (S-35) ;
printf (“e1 : %f\n”, e1);
printf (“e23 : %f\n”, e23);

// eq 7 equilibrium constant equConstant
double a,b,c,d;

// look eq for a can produce wildly different results
// a = -246.64209 + 0.315971 * S + 2.8855 + S*S/pow(10.0,4.0) ;
// a = -246.64209 + 0.315971 * S + ((2.8855/pow (10.0,4.0)) * S* S) ;
a = -246.64209 + 0.315971 * S + (2.8855/pow (10.0,4.0)) * S * S ;
b= 7229.23864 – 7.098137* S – 0.057034 * S * S;
c = 44.493382-0.052711 * S;
d = 0.0781344;
equConstant = a + b / T + c * log(T) – d*T; // log is the natural log ln

printf (” a : %f\n”, a);
printf (” equConstant : %f\n”, equConstant);

// lets look at ph now
pH = equConstant + log10 (( RN – e1)/(1-RN*e23) ); // assume the COMMON LOG

printf (” pH : %f\n”, pH );

// From Fig. 3. the calibration chart shows RN of 0.8 to 2.8
// print out table of RN to pH using equations 1-7

/*
RN = (1.1892) RB – 0.379
RB = ( RN +0.379) / 1.1892
*/

printf (“rb,rn,pH\n” );
for ( double rn=0.2; rn <= 3.5; rn = rn + .01) {
pH = equConstant + log10 (( rn – e1)/(1-rn*e23) ); // assume the COMMON LOG
double RB = ( rn +0.379) / 1.1892;
printf (“%f, %f, %f\n”,RB, rn, pH );
}

return 0;
}

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s