#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pin numbers for voltage sensors and relays
const int voltageSensor1Pin = A0; // Voltage sensor for Battery 1
const int voltageSensor2Pin = A1; // Voltage sensor for Battery 2
const int relaySignalPin1 = 2; // Pin connected to the relay signal input for Battery 1
const int relaySignalPin2 = 3; // Pin connected to the relay signal input for Battery 2
// Define voltage threshold for low battery (24V specification)
const float lowVoltageThreshold = 20.0; // 20.0V
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize relay signal pins as output
pinMode(relaySignalPin1, OUTPUT);
pinMode(relaySignalPin2, OUTPUT);
// Initialize LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
lcd.setCursor(0,0);
lcd.print("Battery 1: ");
lcd.setCursor(0,1);
lcd.print("Battery 2: ");
}
void loop() {
// Read voltage levels of both batteries
float voltage1 = analogRead(voltageSensor1Pin) * (5.0 / 1023.0) * 10; // Convert analog value to voltage (assuming reference voltage of 5V) and scale for 24V battery
float voltage2 = analogRead(voltageSensor2Pin) * (5.0 / 1023.0) * 10;
// Display voltage and state for Battery 1
lcd.setCursor(10,0);
lcd.print(voltage1, 1);
lcd.print("V ");
if (voltage1 < lowVoltageThreshold) {
lcd.print("(Discharging)");
} else {
lcd.print("(Charging)");
}
// Display voltage and state for Battery 2
lcd.setCursor(10,1);
lcd.print(voltage2, 1);
lcd.print("V ");
if (voltage2 < lowVoltageThreshold) {
lcd.print("(Discharging)");
} else {
lcd.print("(Charging)");
}
// Check voltage levels and set relay signal accordingly for Battery 1
if (voltage1 < lowVoltageThreshold) {
// Battery 1 is low, set relay signal high to connect to load
digitalWrite(relaySignalPin1, HIGH);
} else {
// Battery 1 is not low, set relay signal low to connect to charging
digitalWrite(relaySignalPin1, LOW);
}
// Check voltage levels and set relay signal accordingly for Battery 2
if (voltage2 < lowVoltageThreshold) {
// Battery 2 is low, set relay signal high to connect to load
digitalWrite(relaySignalPin2, HIGH);
} else {
// Battery 2 is not low, set relay signal low to connect to charging
digitalWrite(relaySignalPin2, LOW);
}
// Add a delay to prevent continuous looping too quickly
delay(1000); // Adjust delay time as needed
}