ph calibration.

Hi :smiley:
I ve been using this code for ph ,temp measurement and backlight control of the lcd for my grade project.
Heres the code.

#include<LiquidCrystal_I2C.h>
#include<Wire.h>
#include<Timer.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
float V,temprature,Rx,b,slope=11.0,C=94.5;
unsigned long int avgValue;
const int  analogIn=A1;
int Vin=A0,sensorValue=0,buf[10],temp;
unsigned long startMillis=0,currentMillis;
const unsigned long period=25000; 
int button=0;
int buttonstate;
Timer t;
Timer t1;
void setup() {
  pinMode(button,INPUT);
  pinMode(Vin,INPUT);
  pinMode(analogIn,INPUT);
  t.every(100,takeReading);
  t1.every(100,takeReading1);
  Wire.begin();
  lcd.begin(16,2);
  lcd.print("Ph & temp meter");
  delay(2000);                                     
  lcd.clear();
   }
void loop() {
   t.update();
   t1.update();
   buttonstate=digitalRead(button);
   currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
   if (currentMillis - startMillis >= period){
    if(buttonstate==LOW){ 
    lcd.noBacklight();
   }
   if(buttonstate==HIGH){
    lcd.backlight();
    startMillis=currentMillis;
   }
    }
}
void takeReading(){
  lcd.setCursor(0,1);
  lcd.print("temp: ");
  V = (analogRead(Vin)/1023.0)*5.0; 
  Rx = V*slope+C;  
  temprature= (Rx/100.0-1.0)/0.00385;   
  lcd.print(temprature);
  lcd.setCursor(12,1);
  lcd.print("C");
}
void takeReading1(){
 lcd.setCursor(0,0);
 for(int i=0;i<10;i++) 
 { 
  buf[i]=analogRead(analogIn);
  delay(10);
 }
 for(int i=0;i<9;i++)
 {
  for(int j=i+1;j<10;j++)
  {
   if(buf[i]>buf[j])
   {
    temp=buf[i];
    buf[i]=buf[j];
    buf[j]=temp;
   }
  }
 }
 avgValue=0;
 for(int i=2;i<8;i++)
 avgValue+=buf[i];
 float pHVol=(float)avgValue*5.0/1023.0/6.0;
 float phValue = -5.70 * pHVol + 21.34;
 lcd.print("ph:   ");
 lcd.print(phValue);
}

I recendly calibrating my system with two trimers resistors.But I want some sugestions and ideas about how to make it autocalibrate with a button or two for low and high range of ph. Thanks.

Have some kind of button to tell the device you're using pH = 4.0 buffer.
Have it take reading, store in EEPROM.
Do the same for a pH=7.0 buffer.
Store measured value in EEPROM.

Then read back the two values and use them to calculate the slope and offset for your probe. Done.

I need only 4.0 and 7.0 buffers? What about the 10.0 because I saw somewhere that the calibration with 4.0 and 7.0 helps in low range accuracy, 7.0 and 10 helps in high range accuracy.

It depends what pH range you're measuring. At work we only have to measure things with high pH so we always use the 7 and 10 buffers.

There's no reason you couldn't use all 3 if you need the full range.

Most common applications (aquarium, hydroponics) are for lower pH values so buffers 4 and 7 suffice. Buffer 10 is also less stable than the other two, I don't think I've even seen it in the aquarium shops.

Of course if you're mostly interested in the higher pH values you need the pH 7 and 10 buffers. Or all three.

Im actually intrested at every ph value from 0-14 so I need all tree. :slight_smile:
Im also found this device that gave me an image of how I can make it but I need to se how to translate it in programming code somehow.

atlas scientific pH_Circuit_4.0.pdf (1.05 MB)

The data sheet you link to makes the same mistake:

• Full range pH reading from .01 to 14.00

pH actually has a larger range than this... negative values, or values >14, are perfectly well possible.

The values are easy to handle as a good pH probe is highly linear.

wvmarle:
Most common applications (aquarium, hydroponics) are for lower pH values so buffers 4 and 7 suffice. Buffer 10 is also less stable than the other two, I don't think I've even seen it in the aquarium shops.

Of course if you're mostly interested in the higher pH values you need the pH 7 and 10 buffers. Or all three.

Saltwater tanks run in the 8's. Normally calibrate for that with 7 and 10.

Delta_G:
Saltwater tanks run in the 8's. Normally calibrate for that with 7 and 10.

Theres also freshwater and brackish tanks that needs pretty alkaline and hard water. For example african ciclid tanks but this not our topic for now.
Im actually asking if anyone has in mind some informations or a few code examples that I can base on to make the automated calibration.
Thanks.

This is not a trivial thing to do, as easy as it may sound. Even if there are code examples you probably have to adept them big time, or at least spend a good bit of time understanding what's going on.

The hardest part is probably the user interface: how does the user tell the device they're doing a calibration, and with which buffer? This is then closely tied to your specific hardware: available buttons and display.

Actually my device is experimental on a solderless breadboard so I can put as many button I want. So I have to find more info about how it works and then create the program.
Also I have a question.Its better to use powder buffers or solutions?
Because they have differend ph values and as I see on hardware and software they work on a differend line equation.

Dimitris1993:
Actually my device is experimental on a solderless breadboard so I can put as many button I want. So I have to find more info about how it works and then create the program.

The calibration part I told you already; the user interface works the way YOU want it to work, and the way YOU think it’s best integrated in your project’s UI as of course it’ll do more than calibration. I’ve done something similar using a web interface; user gives the pH value; takes measurement; that are the calibration points that are stored in EEPROM and used for the later measurements.

For buffers… powders will have to be dissolved just the same…? I really don’t know which is better.

Dimitris1993:
Actually my device is experimental on a solderless breadboard so I can put as many button I want. So I have to find more info about how it works and then create the program.
Also I have a question.Its better to use powder buffers or solutions?
Because they have differend ph values and as I see on hardware and software they work on a differend line equation.

Doesn't matter what type of buffer you use as long as you know it's exact pH.

Ok thanks I'll see what I'll gona do.

Oh also you have a right wvmarle I need 4 and 7 or 4 and 6.86 more than 10 or 9 buffers.Alkaline buffers are unstable from the CO2 that lowers the ph.

Dimitris1993:
Oh also you have a right wvmarle I need 4 and 7 or 4 and 6.86 because 10 or 9 buffers are unstable from the CO2 that lowers the ph.

That's why you have to keep them sealed tightly. In the lab we also cover with parafilm to keep them.

The small bags of powdered buffer that I see for sale are single use. Add water, do your calibration, dispose of. Quite expensive in the long run but no risk of CO2 or other issues as the powder is kept sealed.

It all depends on how precise you want to be. It's hard to go better than 0.1 pH points accuracy, the low voltage and the ADC resolution don't help here: about 58 mV per pH point - that's less than 12 ADC points for the standard 5V reference. With such probes typically biased at 2.5V you can't use the internal reference either.

Possibly of interest:

The probe itself is 0mV at true neutral pH (ideal). It's the amp that's got it at 2.5. You can use the same amp to give you a full range of 0 to 5V for the normal pH scale. Setting the resistors in that amp is what the calibration is all about in the old meters where you twisted knobs to calibrate. Nowadays you do it with math in the code.

Mmm... Just checked the file OP posted at #5.

The sensor described in that file does not match the sketch posted: the data sheet shows a sensor that communicates over Serial (and which takes specific commands to calibrate at pH 4, 7 and 10), while the sketch in #0 reads an analog input. OP also talks about changing pots, while the board in that pdf doesn't have any pots.

Hi,
Yes the first page needs to be read very carefully…
ph2.jpg

ph1.jpg

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom… :o