// Gravity: Analog pH Sensor/Meter Kit V2 with Arduino
// This code reads the analog voltage from the pH sensor and converts it to a pH value.
// The sensor is connected to the analog pin A0 of the Arduino.
// The output is displayed in the Serial Monitor.
// Pin assignment
const int phPin = A0; // Analog input pin for pH sensor
// Variables to store sensor values
float voltage; // Voltage read from the sensor
float pHValue; // Calculated pH value
void setup() {
// Initialize serial communication at a baud rate of 9600
Serial.begin(9600);
Serial.println("pH Sensor Calibration and Reading");
}
void loop() {
// Read the analog input from the pH sensor
int sensorValue = analogRead(phPin);
// Convert the analog reading (0 - 1023) to a voltage (0 - 5V)
voltage = sensorValue * (5.0 / 1023.0);
// Convert the voltage to a pH value using the formula:
// pH = 7 + ((2.5 - voltage) / 0.18)
// This formula is derived from the sensor's characteristics and calibration
pHValue = 7 + ((2.5 - voltage) / 0.18);
// Print the results to the Serial Monitor
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V, pH: ");
Serial.println(pHValue);
// Wait for 1 second before taking the next reading
delay(1000);
}