My code is not working, I'm trying to use ESP32 and 5v Relay and DFRobot Ph sensor. When Ph level is at 9 and above I want my relay is high and turn it to low. This is my code.
#include <Arduino.h>
// Define the pins
const int pHpin = 36; // The GPIO pin where the pH sensor is connected
const int relayPin = 23; // The GPIO pin controlling the relay
float Offset = 0.0; // Calibration offset for pH sensor
void setup() {
Serial.begin(9600); // Start serial communication for debugging
pinMode(relayPin, OUTPUT); // Set the relay pin as an output
pinMode(pHpin, INPUT); // Set the pH sensor pin as an input
digitalWrite(relayPin, LOW); // Ensure the relay starts off
}
void loop() {
int pHvalue = analogRead(pHpin); // Read the analog pH value
float voltage = pHvalue * (5.0 / 4095.0); // Convert that to voltage
float pH = 3.5 * voltage + Offset; // Convert voltage to pH value
Serial.print("pH Level: ");
Serial.println(pH); // Output pH value to serial monitor
if (pH >= 9.0) {
digitalWrite(relayPin, HIGH); // Turn on the relay if pH is 9 or higher
} else {
digitalWrite(relayPin, LOW); // Turn off the relay otherwise
}
delay(1000); // Delay before the next reading
}