Skip to content

Instantly share code, notes, and snippets.

@ilunie
Created January 21, 2026 14:32
Show Gist options
  • Select an option

  • Save ilunie/62b60186fef2126c004e0389175bff60 to your computer and use it in GitHub Desktop.

Select an option

Save ilunie/62b60186fef2126c004e0389175bff60 to your computer and use it in GitHub Desktop.
ESP32 - pH sensor
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float calibration_value = 21.34 + 1.5;
unsigned long int avgval;
int buffer_arr[10], temp;
float ph_act;
#define PH_SENSOR_PIN 34
void setup() {
Wire.begin();
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("pH Sensor Ready");
delay(2000);
lcd.clear();
}
void loop() {
for (int i = 0; i < 10; i++) {
buffer_arr[i] = analogRead(PH_SENSOR_PIN);
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 * 3.3 / 4095.0 / 6;
ph_act = -5.70 * volt + calibration_value;
Serial.print("pH Value: ");
Serial.println(ph_act);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("pH Value:");
lcd.setCursor(0, 1);
lcd.print(ph_act, 2);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment