#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define POT_PIN 34 // Potentiometer pin
#define BUZZER_PIN 25 // Buzzer pin
#define ACS712_PIN 35 // ACS712 output pin
LiquidCrystal_I2C lcd(0x27, 16, 2);
float voltage = 100.0; // Initial voltage
float current = 0.0; // Current measurement
float serialCurrent = 0.0; // Current from serial input
bool useSerialCurrent = false; // Flag to use serial input
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(BUZZER_PIN, OUTPUT);
noTone(BUZZER_PIN);
analogReadResolution(12);
// Initialize display
lcd.setCursor(0, 0);
lcd.print("V:100.00 I:0.00A");
lcd.setCursor(0, 1);
lcd.print("SYSTEM NORMAL ");
Serial.println("System Ready");
Serial.println("Send 'current=X.XX' to set current");
}
void loop() {
// Handle serial commands
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim();
if (input.startsWith("current=")) {
serialCurrent = input.substring(8).toFloat();
useSerialCurrent = true;
Serial.print("Current set to: ");
Serial.print(serialCurrent, 2);
Serial.println("A");
}
}
// Read voltage from potentiometer (80-120V range)
int potValue = analogRead(POT_PIN);
voltage = (potValue / 4095.0) * 40.0 + 80.0;
// Get current (from serial or sensor)
if (useSerialCurrent) {
current = serialCurrent;
} else {
// Read from ACS712 simulation
int sensorValue = analogRead(ACS712_PIN);
float outputVoltage = sensorValue * (5.0 / 4095.0);
current = (outputVoltage - 2.5) * 8.0; // 20A/2.5V = 8
}
// Update LCD
lcd.setCursor(2, 0); // Voltage
lcd.print(voltage, 2);
lcd.print("V ");
lcd.setCursor(11, 0); // Current
lcd.print(current, 2);
lcd.print("A ");
// Alarm system
if (voltage < 100.0) {
tone(BUZZER_PIN, 1000);
lcd.setCursor(0, 1);
lcd.print("LOW VOLTAGE! ");
}
else if (voltage > 110.0) {
tone(BUZZER_PIN, 1500);
lcd.setCursor(0, 1);
lcd.print("HIGH VOLTAGE! ");
}
else if (fabs(current) > 15.0) {
tone(BUZZER_PIN, 2000);
lcd.setCursor(0, 1);
lcd.print("OVER CURRENT! ");
}
else {
noTone(BUZZER_PIN);
lcd.setCursor(0, 1);
lcd.print("SYSTEM NORMAL ");
}
delay(200);
}