// Battery Voltage Monitor and Alarming Unit
#include <LiquidCrystal.h>
const int rs =8, en =9, d4 = 5, d6 =3, d7=2;
LiquidCrystal lcd(8, 9, 5, 4, 3, 2); // Define pins of Display to be used
int analogInput = A0; // Define A0 as Analogue input to be used
float battery= 0.0; // Define Variable “battery” as a float
float battery_voltage = 0.0; // Define Variable “battery_voltage “ as float
int battery_voltage_raw = 0; //Raw values measured at A0 analogue port starting with value 0
const int LED_Buzzer = 30; // Define LED/Buzzer Output pin 30
const int Relay = 31; // Define relay Output pin 31
//======================================================
//Setup Routine
//======================================================
void setup ()
{
pinMode(analogInput, INPUT); // Configure “analogInput (A0) an a Input
pinMode(30, OUTPUT); // Configure Pin 30 as an Output
pinMode(31, OUTPUT); // Configure Pin 31 as an Output
lcd.begin(20, 4); // Define display to used
lcd.print("Battery Monitor"); // Print Battery Monitor On Display
}
//======================================================
// Loop to measure Battery Voltage (analogue input)
//======================================================
void loop_Analog1()
{
battery_voltage_raw = analogRead(analogInput); // Read raw value 0 -1024 (10bit Analogue to digital conversion)
battery = (battery_voltage_raw * 5.0) / 1024; // convert 0 -1024 raw to 0 - 5V Value
battery_voltage = battery * 6; // Define Voltage measuring scale 6*5=30/ Display voltage value from 0 - 30 Volts
}
//======================================================
// Display loop - Show real time voltage of battery
//======================================================
void loop_LCD1()
{
lcd.setCursor(0, 1); // Set cursor on line 1 character 0
lcd.print("Battery = "); // display Battery V on display
lcd.print(battery_voltage); // print variable value on the display
lcd.setCursor(15, 1); // Set cursor on line 2 character 15
lcd.print("V "); // display V on display
delay(500);
}
//======================================================
// Providing a Battery low Warning using a LED and Buzzer
//======================================================
void loop_LEDBuzzer1()
{
if ( battery_voltage <12.0 ) { // If battery voltage falls below 12Volts then...
digitalWrite (LED_Buzzer, HIGH); // High LED / Buzzer Output Pin
lcd.setCursor(0,3); // Set Cursor position
lcd.print("Battery low Warning "); // Print warning message on display
} else {
digitalWrite (LED_Buzzer, LOW);} // If battery voltage is above 12V keep LED/Buzzer output off
}
//======================================================
// Relay output disconnecting load
//======================================================
void loop_Relay1()
{
if (battery_voltage <11.5) { // If Batteery voltage falls below 11.0 volts........
digitalWrite (Relay, HIGH); // Switch ON Relay Output Pin
lcd.setCursor(0,3); // Set cursor position
lcd.print("Battery low Alarm "); // Print Battery low alarm message on display
} else {
digitalWrite (Relay, LOW);} // If battery voltage is above 11.5Volt do not switch on Relay
}
//======================================================
// Run Sub Loops
//======================================================
void loop ()
{
loop_Analog1(); // Run Analogue loop
loop_LCD1(); //Run Display Loop
loop_LEDBuzzer1(); // Monitor Battery Voltage Loop
loop_Relay1(); // Disconnecting the load loop
if(battery_voltage <11.3){
digitalWrite(LED_Buzzer,LOW);
lcd.setCursor(0,4);
lcd.print("load_disconnected");
}
}