Hello guys, I'm a newbie and I'm currently working on a project to build a pH meter. I read other posts about pH meter, but I was not able to understand some parts of the code. The code is working, no problem with that, I tested two buffer solutions (pH=4,0 and pH=7,0) and they retrieved a perfect match in the serial monitor. I just don't want to use a code without knowing how it works.
Source of the code and more informations on how pH meters work: https://www.botshop.co.za/how-to-use-a-ph-probe-and-sensor/
float calibration = 0.00; // change this value to calibrate
const int analogInPin = A0; // pin which the sensor is connected
int sensorValue = 0; // there's no further reference to this
unsigned long int avgValue;
float b; // there's no further reference to this
int buf[10],temp; // what's this temp for?
void setup() {
Serial.begin(9600);
}
void loop() {
for(int i=0;i<10;i++) // get 10 sample value from the sensor for smooth the value
{
buf[i]=analogRead(analogInPin);
delay(30);
}
for(int i=0;i<9;i++) // sort the analog from small to large(?) How come?
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i]; // these equations work as a reset to the values?
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++) // take the average value of 6 center sample
avgValue+=buf[i];
float pHVol=(float)avgValue*5.0/1024/6; // convert the analog into millivolt
float phValue = -5.70 * pHVol + calibration; // this comes from a linear function
Serial.print("sensor = ");
Serial.println(phValue);
delay(500);
}
Thanks so much for your attention.