#include <LiquidCrystal_I2C.h>
// Constants for ACS712 current sensor
const int sensorIn = 34; // Pin where the OUT pin from sensor is connected on Arduino
int mVperAmp = 66; // This is the 5A version of the ACS712 - use 100 for 20A Module and 66 for 30A Module
double Voltage_curr = 0;
double VRMS = 0;
double AmpsRMS = 0; // Variables for ACS712 Sensor
const int sensorIn2 = 35; // Pin where the OUT pin from sensor is connected on Arduino
int mVperAmp2 = 66; // This is the 5A version of the ACS712 - use 100 for 20A Module and 66 for 30A Module
double Voltage_curr2 = 0;
double VRMS2 = 0;
double AmpsRMS2 = 0; // Variables for ACS712 Sensor
// Initialize the LCD library with I2C address and LCD size
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Relay
const int relayin = 8;
// Power factor warning LED
const int pf_warning_led = 9;
// Declare the power factor variable (initialize with a value, e.g., 1.0)
float pf = 1.0;
void setup() {
Serial.begin(115200);
analogReadResolution(12); // Set ADC resolution for ESP32
// Initialize the LCD connected
lcd.init();
// Turn on the backlight on LCD
lcd.backlight();
pinMode(relayin, OUTPUT);
digitalWrite(relayin, LOW);
pinMode(pf_warning_led, OUTPUT);
}
void loop() {
Read_curr();
Read_curr2();
relay();
power_factor_warning();
delay(1000); // Adjust the delay to slow down the loop for easier readability
}
float getVPP(int sensorpin) {
float result;
int readValue; // Value read from the sensor
int maxValue = 0; // Store max value here
int minValue = 4096; // Store min value here ESP32 ADC resolution
uint32_t start_time = millis();
while ((millis() - start_time) < 1000) // Sample for 1 sec
{
readValue = analogRead(sensorpin);
// Check if you have a new maxValue
if (readValue > maxValue) {
maxValue = readValue;
}
if (readValue < minValue) {
minValue = readValue;
}
}
// Subtract min from max and scale it
result = ((maxValue - minValue) * 3.3) / 4096.0; // ESP32 ADC resolution 4096
return result;
}
void Read_curr() {
Voltage_curr = getVPP(sensorIn); // Get peak-to-peak voltage from sensor
VRMS = (Voltage_curr / 2.0) * 0.707; // RMS Voltage (root 2 is 0.707)
AmpsRMS = (VRMS * 1000) / mVperAmp; // Convert to Amps (mV to Amps)
// Display current and voltage on the LCD
lcd.setCursor(0, 0);
lcd.print("Curr: ");
lcd.print(AmpsRMS);
lcd.print(" A");
lcd.setCursor(2, 0);
lcd.print("Volt: ");
lcd.print(AmpsRMS);
lcd.print(" A");
Serial.print("Current: ");
Serial.print(AmpsRMS);
Serial.println(" A");
}
void Read_curr2() {
Voltage_curr2 = getVPP(sensorIn2); // Get peak-to-peak voltage from sensor
VRMS2 = (Voltage_curr2 / 2.0) * 0.707; // RMS Voltage (root 2 is 0.707)
AmpsRMS2 = (VRMS2 * 1000) / mVperAmp2; // Convert to Amps (mV to Amps)
// You can use this section for a second sensor if necessary
// For now, it's unused but can be extended if needed.
}
void relay() {
// Logic to turn off relay based on current
if (AmpsRMS > 15.0) {
digitalWrite(relayin, HIGH);
lcd.setCursor(0, 1);
lcd.print("Over Load");
while (1); // Stop further code execution if over load condition
}
}
void power_factor_warning() {
// Assuming pf calculation or value assignment is done elsewhere
if (pf < 0.85) {
digitalWrite(pf_warning_led, HIGH); // Turn on LED if PF is low
}
else {
digitalWrite(pf_warning_led, LOW); // Turn off LED if PF is okay
}
}