#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// the pH meter Analog output is connected with the Arduino’s A0
int phval = 0;
// Store the average value of the sensor feedback
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);
delay(2000);
lcd.clear();
}
void loop() {
// Get 10 sample value from the sensor for smooth the value
for (int i = 0; i < 10; i++) {
buffer_arr[i] = analogRead(A0);
delay(30);
}
// sort the analog from small to large
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;
// take the average value of 6 center sample
for (int i = 2; i < 8; i++)
avgval += buffer_arr[i];
// convert the analog into millivolt
float volt = (float)avgval * 5.0 / 1024 / 6;
// convert the millivolt into pH value
float ph_act = 3.5 * volt;
lcd.setCursor(0, 0);
lcd.print("pH Val:");
lcd.setCursor(8, 0);
lcd.print(ph_act);
delay(1000);
}