pH Probe not reading levels correctly.

Hi all,

I’m a beginner at arduino and my project to start with is a pH level measurement tool.
I got almost everything working and connected everything but i found out when i went into the Serial monitor the level for water is 2-4 making it an acidic based solution. Im useing a board that comes from aliexpress that has the website “www.auto-ctrl.com” but it seems to be dead and also “Logo_PHsensor v1.1” printed beneath it. The board had 6 pins labeled To,Do,Po,G,G,V+ (left to right)

The code im useing is

const int analogInPin = A0;
int sensorValue = 0;
unsigned long int avgValue;
float b;
int buf[10],temp;
void setup() {
Serial.begin(9600);
}

void loop() {
for(int i=0;i<10;i++)
{
buf*=analogRead(analogInPin);*

  • delay(10);*
    }
    for(int i=0;i<9;i++)
    {
  • for(int j=i+1;j<10;j++)*
  • {*
    _ if(buf*>buf[j])_
    _
    {_
    _ temp=buf;
    buf=buf[j];
    buf[j]=temp;
    }
    }
    }
    avgValue=0;
    for(int i=2;i<8;i++)_

    _avgValue+=buf;_
    _float pHVol=(float)avgValue5.0/1024/6;
    float phValue = -5.70 * pHVol + 21.34;
    Serial.print("sensor = ");
    Serial.println(phValue);_

delay(1000);
}[/td]
[/tr]
[/table]
I had also tryed some code from this topic https://forum.arduino.cc/index.php?topic=336012.0 and that is:
// pHRead.ino
// Constants:-
const byte pHpin = A0; // Connect the sensor’s Po output to analogue pin 0.
// Variables:-
float Po;
void setup()
{
* Serial.begin(115200);*
}
void loop()
{
* Po = (1023 - analogRead(pHpin)) / 73.07; // Read and reverse the analogue input value from the pH sensor then scale 0-14.*
* Serial.println(Po, 2); // Print the result in the serial monitor.*
* delay(1000); // Take 1 reading per second.*
}
But that kept posting “?” in the resource manager

Did you do any calibration?

wvmarle:
Did you do any calibration?

No, i dont know how to. could you please explain to me how?

It’s quite simple. You take two standard calibration liquids (pH=4 and pH=7), take measurements at those two points (raw analog value!), and plot a straight line through them. Adjust the formula accordingly.

If you also want a bit of alkaline range, add a pH=10 solution. You’ll then notice your three data points are not on one line as indeed the probe is not linear, so in that case best take two lines (one for the <7 and one for the >7 values) to get a reasonable overall approximation.

wvmarle:
It’s quite simple. You take two standard calibration liquids (pH=4 and pH=7), take measurements at those two points (raw analog value!), and plot a straight line through them. Adjust the formula accordingly.

If you also want a bit of alkaline range, add a pH=10 solution. You’ll then notice your three data points are not on one line as indeed the probe is not linear, so in that case best take two lines (one for the <7 and one for the >7 values) to get a reasonable overall approximation.

So i got my 2 levels for ph 7 i got 524, and for ph 4 i got 626. where do i go from here

Basic high school maths.

y = ax + b

You have your two data points, now just calculate a and b and amend the formula in the code.

wvmarle:
Basic high school maths.

y = ax + b

You have your two data points, now just calculate a and b and amend the formula in the code.

I have my answers. now what part of this code shall i edit:

/*

This sample code is used to test the pH meter V1.0.

Editor : YouYou

Ver : 1.0

Product: analog pH meter

SKU : SEN0161

/
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
void setup(void)
{
pinMode(LED,OUTPUT);
Serial.begin(9600);
Serial.println(“pH meter experiment!”); //Test the serial monitor
}
void loop(void)
{
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue,voltage;
if(millis()-samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++]=analogRead(SensorPin);
if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
voltage = avergearray(pHArray, ArrayLenth)5.0/1024;
pHValue = 3.5
voltage+Offset;
samplingTime=millis();
}
if(millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print(“Voltage:”);
Serial.print(voltage,2);
Serial.print(" pH value: ");
Serial.println(pHValue,2);
digitalWrite(LED,digitalRead(LED)^1);
printTime=millis();
}
}
double avergearray(int
arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
Serial.println(“Error number for the array to avraging!/n”);
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr*;*

  • }*
  • avg = amount/number;*
  • return avg;*
  • }else{*
  • if(arr[0]<arr[1]){*
  • min = arr[0];max=arr[1];*
  • }*
  • else{*
  • min=arr[1];max=arr[0];*
  • }*
  • for(i=2;i<number;i++){*
    _ if(arr*<min){_
    _
    amount+=min; //arr<min*_
    _ min=arr*;
    }else {
    if(arr>max){
    amount+=max; //arr>max*

    max=arr*;
    }else{
    amount+=arr; //min<=arr<=max*

    * }
    }//if*

    * }//for*
    * avg = (double)amount/(number-2);
    }//if*

    * return avg;
    }*_

A part that mysteriously disappeared from your code:

 float phValue = -5.70 * pHVol + 21.34;

By the way, please add code tags to make your code readable!

And its a good idea to put the conversion in a function all by itself, appropriately named - then
its trivial to find and adjust next time. The conversion factors can be #define'd or const global variables
with appropriate names too.

Haven't read all the details here and don't know the device in question, but basically you are looking for 50 to 60-mV per pH unit output from the probe itself. The board probably/should have an op amp on board which may or may not have some gain factored in. So at the probe output, your change between pH 7 and pH 4 should give you between 150 and 180-mV change ( at unity gain).
The other thing I couldn't see at first glance, was any mention of temperature. pH probe output is temperature dependent so needs some sort of compensation. pH values are generally reported at a standard temperature of either 25 or 20 Celsius. A laboratory pH meter will have a temperature probe in the solution as well, so as well as setting the span as explained by wvmarle, you need to factor in temperature as well.
For very basic measurements, you can either ignore the temperature bit, or for a more sophisticated model, have a temperature pot on one of the analogue inputs and dial it in manually.

/*

This sample code is used to test the pH meter V1.1.

Editor : YouYou

Date : 2014.06.23

Ver : 1.1

Product: analog pH meter V1.1

SKU : SEN0161

/
#define SensorPin A2 //pH meter Analog output to Arduino Analog Input 2
#define Offset 0.00 //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
void setup(void)
{
pinMode(LED,OUTPUT);
Serial.begin(9600);
Serial.println(“pH meter experiment!”); //Test the serial monitor
}
void loop(void)
{
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue,voltage;
if(millis()-samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++]=analogRead(SensorPin);
if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
voltage = avergearray(pHArray, ArrayLenth)5.0/1024;
pHValue = 3.5
voltage+Offset;
samplingTime=millis();
}
if(millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print(“Voltage:”);
Serial.print(voltage,2);
Serial.print(" pH value: ");
Serial.println(pHValue,2);
digitalWrite(LED,digitalRead(LED)^1);
printTime=millis();
}
}
double avergearray(int
arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
Serial.println(“Error number for the array to avraging!/n”);
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr*;*

  • }*
  • avg = amount/number;*
  • return avg;*
  • }else{*
  • if(arr[0]<arr[1]){*
  • min = arr[0];max=arr[1];*
  • }*
  • else{*
  • min=arr[1];max=arr[0];*
  • }*
  • for(i=2;i<number;i++){*
    _ if(arr*<min){_
    _
    amount+=min; //arr<min*_
    _ min=arr*;
    }else {
    if(arr>max){
    amount+=max; //arr>max*

    max=arr*;
    }else{
    amount+=arr; //min<=arr<=max*

    * }
    }//if*

    * }//for*
    * avg = (double)amount/(number-2);
    }//if*

    * return avg;
    }*

    this code is not working and where we should put the value of ph which we calulated by formula_