ARDUINO UNO CODE FOR SIMPLE ENERGY METER

 #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 previous accumulated power

double powerprev = 0;


void setup() {

  Serial.begin(9600);


  // 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) ----

  // Read and process voltage sensor values

  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)


  // Add the current power to the previous accumulated power

  powerprev += power;


  // ---- 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 (previous power)

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Power Prev: ");

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

  lcd.print("W");


  delay(3000);


  // ---- Print to Serial Monitor ----

  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\tPrev Power: ");

  Serial.print(powerprev, 2);

  Serial.println(" W");


  delay(1000);  // Delay for stability

}


Comments