#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ACS712_PIN 34 // Current sensor pin
#define BUZZER_PIN 25 // Buzzer pin
#define POT_PIN 32 // Potentiometer pin (changed from 34 to avoid conflict)
#define ADC_VREF_mV 5000.0 // Voltage reference
#define ADC_RESOLUTION 4096.0 // 12-bit ADC
#define SENSITIVITY 125.0 // 125mV/A for ACS712-20A
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD 16x2
float voltage = 100.0; // Measured voltage (80-120V)
float current = 0.0; // Measured current
unsigned long lastUpdate = 0;
const long updateInterval = 200; // ms
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(ACS712_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
noTone(BUZZER_PIN);
// Initial display setup
lcd.setCursor(0, 0);
lcd.print("V: I: ");
lcd.setCursor(0, 1);
lcd.print("Status: Normal ");
}
void loop() {
if (millis() - lastUpdate >= updateInterval) {
lastUpdate = millis();
// Read voltage (80-120V range)
int potValue = analogRead(POT_PIN);
voltage = (potValue / 4095.0) * 40.0 + 80.0;
// Read current (-20A to +20A range)
int adcValue = analogRead(ACS712_PIN);
float voltage_mV = adcValue * (ADC_VREF_mV / ADC_RESOLUTION);
current = ((voltage_mV - 2500) / 2500.0) * 40.0;
// Update LCD display
updateDisplay();
// Check and handle alarms
checkAlarms();
// Serial monitor output
Serial.print("Voltage: ");
Serial.print(voltage, 2);
Serial.print("V | Current: ");
Serial.print(current, 2);
Serial.println("A");
}
}
void updateDisplay() {
// Update voltage display (line 1, position 2-7)
lcd.setCursor(2, 0);
lcd.print(voltage, 2);
lcd.print("V ");
// Update current display (line 1, position 11-16)
lcd.setCursor(11, 0);
lcd.print(current, 2);
lcd.print("A ");
}
void checkAlarms() {
// Voltage alarms
if (voltage < 100.0) {
tone(BUZZER_PIN, 1000);
lcd.setCursor(7, 1);
lcd.print("LOW VOLTAGE! ");
}
else if (voltage > 110.0) {
tone(BUZZER_PIN, 1500);
lcd.setCursor(7, 1);
lcd.print("HIGH VOLTAGE!");
}
// Current alarm
else if (abs(current) > 15.0) {
tone(BUZZER_PIN, 2000);
lcd.setCursor(7, 1);
lcd.print("OVER CURRENT!");
}
// Normal state
else {
noTone(BUZZER_PIN);
lcd.setCursor(7, 1);
lcd.print("Normal ");
}
}