#43540
Hi
I have used the formula provided by DFRobot for Analog PH Sensor (DFRobot_PH-master library - readPH() ). I discovered it gave incorrect PH values.

I have used the following code and found it to be accurate. I have used the Buffer solution provided by DFRobot in both cases to test the reading.

float readPHAnalog()
{
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;

int n = 20;
int sp = 4, ep = 12; //This value must be less than n value above
int buf[n],temp;
for(int i=0;i<n;i++) //Get 10 sample value from the sensor for smooth the value
{
buf=analogRead(PH_PIN);
delay(10);
}
for(int i=0;i<(n-1);i++) //sort the analog from small to large
{
for(int j=i+1;j<n;j++)
{
if(buf>buf[j])
{
temp=buf;
buf=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=sp;i<ep;i++) //take the average value of center sample
avgValue+=buf;
.float phValue=(float)avgValue*5.0/1024/(ep-sp); //convert the analog into millivolt
phValue=3.5*phValue; //convert the millivolt into pH value
return phValue;
}