/*
https://forum.arduino.cc/t/i-need-help-with-soil-ph-sensor-and-rs485/1259395
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int phPin = A0;
float voltage = 0.0;
float phValue = 0.0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(4800);
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("pH Sensor");
}
void loop() {
int sensorValue = analogRead(phPin);
voltage = sensorValue * (5.0 / 1023.0);
// Formula: measuring PH value = ((9-3)/(5-0) * voltage) + 3
phValue = ((9.0 - 3.0) / (5.0 - 0.0)) * voltage + 3.0;
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V, pH Value: ");
Serial.println(phValue);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage, 2);
lcd.print(" V");
lcd.setCursor(0, 1);
lcd.print("pH Value: ");
lcd.print(phValue, 2);
delay(2000);
}