#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define VOLTAGE_SENSOR_PIN 39 // Analog pin for the voltage sensor
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define ACS712_PIN 34 // Analog pin for the ACS712 sensor
void setup()
{
// Initialize serial communication and LCD
Serial.begin(9600);
pinMode(VOLTAGE_SENSOR_PIN, INPUT);
pinMode(ACS712_PIN, INPUT);
lcd.init();
lcd.backlight();
}
void loop()
{
// Read the battery voltage from the sensor
int sensorValueVoltage = analogRead(VOLTAGE_SENSOR_PIN);
float voltageBattery = (sensorValueVoltage / 1023.0) * 5.0;
// Calculate current using the ACS712 sensor
int sensorValueACS = analogRead(ACS712_PIN);
float voltageCurrent = sensorValueACS * (5.0 / 1023.0);
float current = voltageCurrent / 0.1; // 100mV per A sensitivity of ACS712
// Calculate power (Power = Voltage * Current)
float power = voltageBattery * current;
// Display voltage on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Batt_Vtg: ");
lcd.print(voltageBattery, 2);
lcd.print(" V");
// Display current on LCD
lcd.setCursor(0, 1);
lcd.print("Current: ");
lcd.print(current, 2);
lcd.print(" A");
// Display power on LCD
lcd.setCursor(0, 2); // Assuming we have enough space for this line
lcd.print("Power: ");
lcd.print(power, 2);
lcd.print(" W");
// Display values in Serial Monitor
Serial.print("Batt_Voltage: ");
Serial.print(voltageBattery, 2);
Serial.println(" V");
Serial.print("Current: ");
Serial.print(current, 2);
Serial.println(" A");
Serial.print("Power: ");
Serial.print(power, 2);
Serial.println(" W");
delay(1000);
}