arduino uno to esp32 code

 #include <Wire.h>

#include <LiquidCrystal_I2C.h>

#include <math.h>  // For sqrt function


// Set the LCD address (usually 0x27 or 0x3F)

#define I2C_ADDR 0x27

#define LCD_ROWS 2

#define LCD_COLS 16


// Create an instance of the LCD class for I2C

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);


const int currentPin = A0;  // Current sensor on analog pin A0

int sensitivity = 66;       // Sensitivity of the current sensor (adjust for your sensor)

int adcValue = 0;

int offsetVoltage = 2500;   // Offset voltage in millivolts for the current sensor

double adcVoltage = 0;

double currentValue = 0;


// Variables for voltage sensor (ZMPT101B)

double sensorValue1 = 0;

int max_v = 0;

double VmaxD = 0;  // Max voltage

double Veff = 0;   // Effective voltage (RMS)


// Variable to store power consumption

double powerConsumed = 0;

unsigned long previousMillis = 0;  // To track 30-second interval


void setup() {

  // Initialize Serial Communication

  Serial.begin(115200);


  // Initialize the LCD

  lcd.init();

  lcd.backlight();


  // Initial display message

  lcd.setCursor(0, 0);

  lcd.print("Measuring...");

  delay(2000);

  lcd.clear();

}


void loop() {

  // ---- Current Sensor Logic ----

  adcValue = analogRead(currentPin);

  adcVoltage = (adcValue / 1024.0) * 5200;  // Convert ADC value to millivolts

  currentValue = ((adcVoltage - offsetVoltage) / sensitivity);  // Convert to Amps


  // ---- Voltage Sensor Logic (ZMPT101B) ----

  for (int i = 0; i < 100; i++) {

    sensorValue1 = analogRead(A1);  // Voltage sensor connected to A1

    if (analogRead(A1) > 511) {

      max_v = sensorValue1;  // Store max sensor value

    }

    delay(1);

  }


  // Calculate effective voltage (RMS)

  if (max_v != 0) {

    VmaxD = max_v;

    double VeffD = VmaxD / sqrt(2);

    Veff = (((VeffD - 420.76) / -90.24) * -210.2) + 210.2;  // Voltage scaling

  } else {

    Veff = 0;

  }


  // ---- Power Calculation ----

  double power = Veff * currentValue;  // Power in Watts (Voltage × Current)

  

  // ---- Calculate Power Factor ----

  double powerFactor = (power / (Veff * currentValue));  // Power factor (real power / apparent power)


  // Accumulate power to calculate power consumed (Watt-seconds)

  powerConsumed += power * (1.0 / 60.0);  // Power consumption over time (Watt-minutes)


  // ---- Display Values on I2C LCD ----

  lcd.clear();

  

  // Display voltage in mV

  lcd.setCursor(0, 0);

  lcd.print("V: ");

  lcd.print(Veff, 2);  // Print voltage with 2 decimal places

  lcd.print("V");


  // Display current in Amps

  lcd.setCursor(0, 1);

  lcd.print("I: ");

  lcd.print(currentValue, 2);  // Print current with 2 decimal places

  lcd.print("A");


  delay(3000);


  // Display power in Watts

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Power: ");

  lcd.print(power, 2);  // Print power with 2 decimal places

  lcd.print("W");


  delay(3000);


  // Display accumulated power (power consumed)

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Energy: ");

  lcd.print(powerConsumed, 2);  // Print power consumed with 2 decimal places

  lcd.print("Wh");


  delay(3000);


  // Display power factor

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("PF: ");

  lcd.print(powerFactor, 2);  // Print power factor with 2 decimal places


  delay(3000);


  // ---- Send Data to ESP32 ----

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= 30000) {  // Every 30 seconds

    previousMillis = currentMillis;


    // Send data to ESP32

    Serial.print("Voltage: ");

    Serial.print(Veff, 2);

    Serial.print(" V\tCurrent: ");

    Serial.print(currentValue, 2);

    Serial.print(" A\tPower: ");

    Serial.print(power, 2);

    Serial.print(" W\tEnergy: ");

    Serial.print(powerConsumed, 2);

    Serial.print(" Wh\tPower Factor: ");

    Serial.println(powerFactor, 2);


    delay(1000);  // Short delay after sending

  }

}


Comments