#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ACS712_PIN = 34;
const int BUZZER_PIN = 32;
const float ALARM_THRESHOLD = 15.0;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("V:0.00 I:0.00A");
pinMode(BUZZER_PIN, OUTPUT);
analogReadResolution(12);
// Send initial zero current command
Serial.println("current=0.0");
}
void loop() {
static unsigned long lastSweepUpdate = 0;
static float sweepCurrent = -20.0;
// Automatic sweep demonstration (comment if not needed)
if (millis() - lastSweepUpdate > 1000) {
lastSweepUpdate = millis();
sweepCurrent += 1.0;
if (sweepCurrent > 20.0) sweepCurrent = -20.0;
Serial.print("current=");
Serial.println(sweepCurrent, 1);
}
// Read sensor
int raw = analogRead(ACS712_PIN);
float voltage = raw * (5.0f / 4095.0f);
float current = (voltage - 2.5f) * 8.0f; // 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 check
if (fabs(current) > ALARM_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH);
lcd.setCursor(0, 1);
lcd.print("ALARM! >15A ");
} else {
digitalWrite(BUZZER_PIN, LOW);
lcd.setCursor(0, 1);
lcd.print("Status: Normal ");
}
delay(100);
}