/* LCD = VCC = 5v, SDA = A4, SDC = A5
PH Meter V+ = 5v, G = GND, Po = A0 */
        
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float calibration_value = 21.34;
int phval = 0; 
unsigned long int avgval; 
int buffer_arr[10],temp;
void setup() 
{
 Serial.begin(9600);
  lcd.init(); 
  lcd.begin(16, 2);
  lcd.backlight();
  lcd.setCursor(0, 0);
  
}
void loop() {
 for(int i=0;i<10;i++) 
 { 
 buffer_arr[i]=analogRead(A0);
 delay(30);
 }
 for(int i=0;i<9;i++)
 {
 for(int j=i+1;j<10;j++)
 {
 if(buffer_arr[i]>buffer_arr[j])
 {
 temp=buffer_arr[i];
 buffer_arr[i]=buffer_arr[j];
 buffer_arr[j]=temp;
 }
 }
 }
 avgval=0;
 for(int i=2;i<8;i++)
 avgval+=buffer_arr[i];
 float volt=(float)avgval*5.0/1024/6;
 float ph_act = -5.70 * volt + calibration_value;
 lcd.setCursor(0, 0);
 lcd.print("  PH  =");
 lcd.setCursor(9, 0);
 lcd.print(ph_act);
 delay(1000);
}
/* 

Nernst Equation
E = E0  - 2.3 (RT/nF) ln Q

Where
Q= Reaction coefficient
E = mV output from the electrode
E0 = Zero offset for the electrode
R = Ideal gas constant= 8.314 J/mol-K
T = Temperature in ºK
F = Faraday constant = 95,484.56 C/mol
N = Ionic Charge */