#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define POT_PIN 34 // Potentiometer connected to GPIO 34 (Analog Input)
#define RELAY_PIN 23 // Relay connected to GPIO 23
#define BUTTON_PIN 18 // Push button connected to GPIO 18
#define LED_PIN 19 // LED Indicator connected to GPIO 19
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool loadState = false; // Track load ON/OFF state
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Enable internal pull-up resistor
lcd.setCursor(0, 0);
lcd.print(" Load Monitor ");
}
void loop() {
// Simulate Voltage/Current Sensor with Potentiometer
int sensorValue = analogRead(POT_PIN);
float voltage = (sensorValue / 4095.0) * 3.3; // Convert to Voltage
// Read Push Button for Manual Load Control
if (digitalRead(BUTTON_PIN) == LOW) {
loadState = !loadState; // Toggle Load State
digitalWrite(RELAY_PIN, loadState);
digitalWrite(LED_PIN, loadState);
delay(300); // Debounce delay
}
// Display Data on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage, 2);
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print("Load: ");
lcd.print(loadState ? "ON " : "OFF");
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print("V, Load: ");
Serial.println(loadState ? "ON" : "OFF");
delay(1000); // Update every 1 second
}