#include <Wire.h> // For I2C communication (pH sensor and light sensor if needed)
#include <Adafruit_Sensor.h>
#define PH_SENSOR_PIN 25 // Define pH sensor pin (analog)
#define WATER_PUMP_PIN 18 // Define water pump control pin
#define DOSING_PUMP_PIN 5 // Define dosing pump control pin
float phValue = 0.0; // pH value to be read from sensor
void setup() {
Serial.begin(9600);
// Initialize sensors and pumps
pinMode(PH_SENSOR_PIN, INPUT);
pinMode(WATER_PUMP_PIN, OUTPUT);
pinMode(DOSING_PUMP_PIN, OUTPUT);
// Ensure both pumps are OFF initially
digitalWrite(WATER_PUMP_PIN, LOW);
digitalWrite(DOSING_PUMP_PIN, LOW);
}
void loop() {
// Read pH level (analog)
int phValueRaw = analogRead(PH_SENSOR_PIN);
phValue = (phValueRaw * (14.0 / 4095.0)); // Scale raw value to pH range (0-14)
// Classify the pH value
String phClassification = classifyPH(phValue);
// Print sensor readings
Serial.print("pH: "); Serial.println(phValue);
Serial.print(" - ");
Serial.println(phClassification); // Print the classification
// Control pumps based on pH value
controlPumps(phValue);
delay(2000); // Adjust delay as per required sampling rate
}
// Function to classify pH value
String classifyPH(float ph) {
if (ph < 7.0) {
return "Acidic";
} else if (ph == 7.0) {
return "Neutral";
} else {
return "Alkaline";
}
}
// Function to control water and dosing pumps based on pH
void controlPumps(float ph) {
if (ph < 6.5) { // If pH is too low (acidic)
digitalWrite(WATER_PUMP_PIN, HIGH); // Turn ON water pump to dilute acid
digitalWrite(DOSING_PUMP_PIN, LOW); // Turn OFF dosing pump
Serial.println("Water Pump ON: Increasing pH...");
} else if (ph > 7.5) { // If pH is too high (alkaline)
digitalWrite(DOSING_PUMP_PIN, HIGH); // Turn ON dosing pump to add solution
digitalWrite(WATER_PUMP_PIN, LOW); // Turn OFF water pump
Serial.println("Dosing Pump ON: Decreasing pH...");
} else { // If pH is within the desired range
digitalWrite(WATER_PUMP_PIN, LOW); // Turn OFF water pump
digitalWrite(DOSING_PUMP_PIN, LOW); // Turn OFF dosing pump
Serial.println("pH is within optimal range. Pumps OFF.");
}
}