// Source : https://how2electronics.com/ph-meter-using-ph-sensor-arduino-oled/#:~:text=For%20testing%20the%20Ph%20meter%20designed%20above
#define pHSensorPin1 A0 // the pH sensor 1 Analog output
#define pHSensorPin2 A1 // the pH sensor 2 Analog output
unsigned long int avgValue1; //Store the average value of the sensor 1 feedback
unsigned long int avgValue2; //Store the average value of the sensor 2 feedback
int buf1[10],temp1;
int buf2[10],temp2;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// Ph sensor 1
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf1[i]=analogRead(pHSensorPin1);
delay(10);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf1[i]>buf1[j])
{
temp1=buf1[i];
buf1[i]=buf1[j];
buf1[j]=temp1;
}
}
}
avgValue1=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
avgValue1+=buf1[i];
float phValue1=(float)avgValue1*5.0/1024/6; //convert the analog into millivolt
phValue1=3.5*phValue1; //convert the millivolt into pH value
Serial.print("pH_1:");
Serial.print(phValue1,2);
Serial.println(" ");
//Ph sensor 2
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf2[i]=analogRead(pHSensorPin2);
delay(10);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf2[i]>buf2[j])
{
temp2=buf2[i];
buf2[i]=buf2[j];
buf2[j]=temp2;
}
}
}
avgValue2=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
avgValue2+=buf2[i];
float phValue2=(float)avgValue2*5.0/1024/6; //convert the analog into millivolt
phValue2=3.5*phValue2; //convert the millivolt into pH value
Serial.print("pH_2:");
Serial.print(phValue2,2);
Serial.println(" ");
}