#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define NTC_SENSOR_PIN A0 // Analog pin connected to the NTC thermistor
#define FAN_PIN 9 // Digital pin connected to the fan
#define VOLTAGE_SENSOR_PIN A1 // Analog pin for the voltage sensor
#define RELAY_PIN 2 // Digital pin connected to the relay
#define ACS712_PIN A2 // Analog pin for the ACS712 sensor
#define ACS712_ADDRESS 0x40 // Address of the ACS712 sensor
#define LCD_ADDRESS 0x27 // I2C address of the LCD
#define LCD_COLUMNS 20 // Number of columns in the LCD
#define LCD_ROWS 4 // Number of rows in the LCD
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
void setup() {
Serial.begin(960);
// Initialize LCD
lcd.init();
lcd.backlight();
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" WELCOME");
lcd.setCursor(0, 1);
lcd.print(" To EV");
lcd.setCursor(0, 2);
lcd.print( " Battery Management ");
lcd.setCursor(0, 3);
lcd.print(" system ");
delay(2000);
lcd.clear();
// Set ACS712 pin as input
pinMode(ACS712_PIN, INPUT);
// Set voltage sensor pin and relay pin modes
pinMode(VOLTAGE_SENSOR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
// Print initial messages on LCD
lcd.setCursor(0, 0);
lcd.print("Battery Voltage:");
lcd.setCursor(0, 2);
lcd.print("Temp:");
// Set fan pin mode
pinMode(FAN_PIN, OUTPUT);
}
void loop() {
// Read the analog value from the NTC thermistor
int sensorValueNTC = analogRead(NTC_SENSOR_PIN);
// Convert the analog value to resistance
float resistance = 10000.0 / ((1023.0 / sensorValueNTC) - 1.0); // Assuming a 10kΩ thermistor
// Use the Steinhart-Hart equation to calculate temperature
float steinhart;
steinhart = log(resistance / 10000.0); // (R/Ro)
steinhart /= 3950.0; // 1/B * ln(R/Ro)
steinhart += 1.0 / (25.0 + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // Convert to Celsius
// Print the temperature to the LCD display
lcd.setCursor(0, 2); // Set cursor to the third line
lcd.print("Temperature: ");
lcd.print(steinhart, 2); // Print temperature value with two decimal places
lcd.print(" C"); // Print unit
// Print the temperature to the serial monitor
Serial.print("Temp: ");
Serial.print(steinhart);
// Fan control based on temperature
if (steinhart < 25) {
analogWrite(FAN_PIN, 0); // Turn off fan
} else if (steinhart >= 25 && steinhart < 30) {
analogWrite(FAN_PIN, 128); // Set fan to half speed
} else {
analogWrite(FAN_PIN, 255); // Set fan to full speed
}
// Read the analog value from the voltage sensor
int sensorValueVoltage = analogRead(VOLTAGE_SENSOR_PIN);
// Convert the analog value to voltage (assuming 5V reference)
float voltageBattery = (sensorValueVoltage / 1023.0) * 5.0;
// Set threshold values for overflow and low battery
float overflowThreshold = 14.0; // Set your overflow threshold voltage here
float lowBatteryThreshold = 10.0; // Set your low battery threshold voltage here
float Battery = (12.0, 13.0); // Set your Normal battery voltage here
// Display voltage on LCD
lcd.setCursor(0, 1);
lcd.print("Voltage: ");
lcd.print(voltageBattery , 2); // Print voltage
lcd.print(" V");
// Check for overflow condition
if (voltageBattery > overflowThreshold) {
// Overflow condition detected, turn off relay to stop charging
digitalWrite(RELAY_PIN, LOW);
lcd.setCursor(0, 0);
lcd.print("Over Voltage ");
} else if (voltageBattery < lowBatteryThreshold) {
// Low battery condition detected, turn on relay to start charging
digitalWrite(RELAY_PIN, HIGH);
lcd.setCursor(0, 0);
lcd.print("Low Battery! ");
} else if (voltageBattery = Battery) {
// Normal operation, relay remains in its current state
lcd.setCursor(0, 0);
lcd.print("Medium Level ");
} else {
}
//delay(1000); // Delay for stability
// Read the analog value from the ACS712 sensor
int sensorValueACS = analogRead(ACS712_PIN);
// Convert the analog value to voltage
float voltageCurrent = sensorValueACS * (5.0 / 1023.0);
// Convert voltage to current using the ACS712 sensitivity (100mV/A)
float current = voltageCurrent / 0.1; // 100mV per A
// Display current on LCD
//lcd.clear();
lcd.setCursor(0, 3);
lcd.print("Current: ");
lcd.print(current, 2);
lcd.print(" A");
// Display current on Serial Monitor
Serial.print("Current: ");
Serial.print(current);
Serial.println(" A");
delay(1000); // Delay for stability
}