#include <LiquidCrystal_I2C.h>
// Pin Definitions
#define LOAD_RELAY_PIN 13
#define CHARGE_RELAY_PIN 12
#define VOLTAGE_SENSOR_PIN A0
// LCD Pin Configuration
LiquidCrystal_I2C lcd(0X27,20,4);
// Voltage Cutoff Levels
#define FULL_CUT_VOLTAGE 13.6 // Maximum voltage before charging stops
#define LOW_CUT_VOLTAGE 10.6 // Minimum voltage before load disconnects
// Variables
float batteryVoltage = 0;
void setup() {
lcd.begin(16, 2); // Initialize the LCD
lcd.print("12V AutoCut");
lcd.backlight();
delay(2000);
// Configure relay pins as output
pinMode(LOAD_RELAY_PIN, OUTPUT);
pinMode(CHARGE_RELAY_PIN, OUTPUT);
// Start with relays off
digitalWrite(LOAD_RELAY_PIN, LOW);
digitalWrite(CHARGE_RELAY_PIN, LOW);
}
void loop() {
// Read battery voltage
int sensorValue = analogRead(VOLTAGE_SENSOR_PIN);
batteryVoltage = (sensorValue / 1023.0) * 5.0 * ((10.0 + 5.6) / 5.6); // Adjust for voltage divider
// Update LCD Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Battery: ");
lcd.print(batteryVoltage);
lcd.print("V");
// Check voltage levels and control relays
if (batteryVoltage > FULL_CUT_VOLTAGE) {
digitalWrite(CHARGE_RELAY_PIN, LOW); // Stop charging
lcd.setCursor(0, 1);
lcd.print("Charging: OFF");
} else {
digitalWrite(CHARGE_RELAY_PIN, HIGH); // Start charging
lcd.setCursor(0, 1);
lcd.print("Charging: ON ");
}
if (batteryVoltage < LOW_CUT_VOLTAGE) {
digitalWrite(LOAD_RELAY_PIN, LOW); // Disconnect load
lcd.setCursor(0, 1);
lcd.print("Load: OFF ");
} else {
digitalWrite(LOAD_RELAY_PIN, HIGH); // Connect load
lcd.setCursor(0, 1);
lcd.print("Load: ON ");
}
delay(1000); // Update every second
}