#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the LCD (use 0x27 or change to 0x3F based on your LCD module)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Constants for voltage and current calculations
const float voltageDividerRatio = 11.0; // Divider ratio for voltage sensor
const float currentSensorSensitivity = 0.185; // Sensitivity of ACS712 (e.g., 5A version)
const float referenceVoltage = 5.0; // Arduino ADC reference voltage
const int adcResolution = 1024; // ADC resolution (10-bit)
float voltage = 0.0;
float current = 0.0;
float power = 0.0;
void setup() {
// Initialize the LCD
lcd.begin(16, 2); // Initialize with 16 columns and 2 rows
lcd.backlight(); // Turn on LCD backlight
// Display startup message
lcd.setCursor(0, 0);
lcd.print("Smart Energy");
lcd.setCursor(0, 1);
lcd.print("Meter Starting");
delay(2000);
lcd.clear();
// Initialize Serial Monitor for debugging
Serial.begin(9600);
}
void loop() {
// Simulate voltage reading from potentiometer (connected to A0)
int voltageRaw = analogRead(A0);
voltage = (voltageRaw * referenceVoltage / adcResolution) * voltageDividerRatio;
// Simulate current reading from potentiometer (connected to A1)
int currentRaw = analogRead(A1);
float currentVoltage = currentRaw * referenceVoltage / adcResolution;
current = (currentVoltage - 2.5) / currentSensorSensitivity; // Assuming 2.5V is the zero-current reference
// Display values on the LCD
lcd.setCursor(0, 0);
lcd.print("V:"); lcd.print(voltage, 1); lcd.print("V ");
lcd.print("I:"); lcd.print(current, 2); lcd.print("A");
lcd.setCursor(0, 1);
// Debugging output on Serial Monitor
Serial.print("Voltage: "); Serial.print(voltage, 1); Serial.println(" V");
Serial.print("Current: "); Serial.print(current, 2); Serial.println(" A");
Serial.println("-------------------");
delay(1000); // Update every second
}