const int voltagePin = A0; // Analog pin to read voltage
const int relayPin = 2; // Digital pin to control relay
const float voltageThreshold = 20.0; // Voltage threshold for cutoff
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Initially, relay is off
Serial.begin(9600); // For debugging
}
void loop() {
int sensorValue = analogRead(voltagePin);
float voltage = (sensorValue / 1023.0) * 24.0; // Assume sensor range 0-50V
Serial.print("Battery Voltage: ");
Serial.println(voltage);
if (voltage < voltageThreshold) {
digitalWrite(relayPin, LOW); // Cut off relay
Serial.println("Voltage below threshold, relay OFF");
} else {
digitalWrite(relayPin, HIGH); // Relay ON
Serial.println("Voltage above threshold, relay ON");
}
delay(1000); // Delay for stability
}