Power Monitoring System Using Pzem-004t and Bharath Pi

by bharatpi in Circuits > Arduino

1696 Views, 1 Favorites, 0 Comments

Power Monitoring System Using Pzem-004t and Bharath Pi

710ddunW2ZL._SL1200_.jpg

Creating an IoT project based on a PZEM sensor and current coil involves measuring electrical parameters like voltage, current, power, etc., and then transmitting this data to a cloud platform for monitoring and analysis. The preferences library can be used to store and retrieve configuration settings on the microcontroller (e.g., Bharath Pi) to ensure the device operates according to your preferences even after power cycles.

PZEM- 004T is a veritably complete detector module, it has current, voltage, power, power factor, frequency, and indeed an energy cadence. , we're going to find out how to use it with BHARATH PI and ESP- Back in the day, when we want to make an energy cadence, it was relatively hard. We need numerous detectors, like the current, and voltage, not to mention frequency and power factor detectors. We need to make that power factor detector by ourselves. And also the AC voltage detector in the request is occasionally just unstable I don’t know why.  But now we got this PZEM- 004T detector that actually solves all that problem, this module includes all our requirements in energy or power cadence. Indeed more complete with frequency and power factor detectors.  

Supplies

Artboard-1-e1686767346235.jpg

Hardware Components

1. PZEM-004T Energy Monitor module

2. Bharath pi Board

3. Jumper wires

Software apps and online services

We are going to use Arduino IDE for programming the Bharat Pi. The Bharat Pi can be connected using a USB Type-C connector. Serial drivers are required to be installed for the detection of Bharat Pi. You can download the serial drivers from this depending on the operating system.

·      Arduino IDE

·      CH341 serial drivers for Bharat Pi


Circuit Connection

DSC_0711.jpg

1. Connect the V+ pin of the PZEM-004T to a 5V power supply pin on the Bharath pi board.

2. Connect the V- pin of the PZEM-004T to the GND (Ground) pin on the

Bharath pi board.

3. Connect the RX pin of the PZEM-004T to a digital pin on the Bharath pi board (e.g., Pin 16).

4. Connect the TX pin of the PZEM-004T to another digital pin on the Bharath pi board (e.g., Pin 17).

Important Notes:

1. The TX pin of the PZEM-004T should be connected to the RX pin of the Bharath Pi board, and the RX pin of the PZEM-004T should be connected to the TX pin of the  Bharath Pi board. This is because the PZEM-004T uses serial communication, and the communication direction is from the PZEM sensor to the Bharath Pi board.

2. Depending on your specific project, you may also need to connect the current coil (CT sensor) to the I (Current) pin of the PZEM-004T. The current coil should be clamped around the wire carrying the AC current you want to measure.

3. Always follow safety precautions when working with electrical circuits and power sources. Ensure that you are well aware of the voltage and current levels you are dealing with, and take necessary precautions to avoid electrical hazards.

Once the circuit is correctly connected, you can use the appropriate code on your Bharath Pi board to read data from the PZEM-004T sensor and perform any necessary calculations or transmit the data to a cloud platform for further analysis and monitoring.

Story

710ddunW2ZL._SL1200_.jpg
247488209-2de0aa2c-7df7-481e-a941-b4c3681b5366.png

1.  PZEM Sensor and Current Coil:

·       PZEM-004T Energy Monitor: The PZEM-004T is an integrated module designed for measuring electrical parameters in an AC circuit. It can measure voltage, current, power, energy, and other electrical parameters accurately. The sensor uses non-invasive current transformers (CTs) or current coils to measure the current flowing through a wire without the need for cutting or interrupting the circuit.

·       Current Coil (CT Sensor): A current coil is a type of current transformer used to measure alternating current (AC) flowing through a conductor. The CT sensor used with the PZEM-004T is generally non-invasive, which means it can be clamped around the wire without disconnecting it, making it easy to install and safe to use.

2. Preferences Library:

·       Purpose: The Preferences library is commonly used in Arduino and ESP8266/ESP32 projects to store and retrieve configuration settings on the microcontroller's non-volatile memory (NVRAM). NVRAM is a memory that retains data even after the power is disconnected, making it ideal for storing user preferences or configuration parameters.

·       Namespace: Preferences allow you to define a unique namespace, which is like a container for all the settings related to a particular project or application. This avoids conflicts with other projects using the same library.

·       Preferences Methods:

·     begin: Initializes the Preferences library. It takes the namespace and a flag that indicates whether to erase the existing preferences or not. Setting the second argument to false ensures that existing preferences are not erased when the device restarts.

·     putX: These methods store values of various data types (e.g., putInt, putFloat, putString) in the preferences identified by a key (name). For example, putInt("someKey", 42) stores the integer value 42 with the key "someKey".

·      getX: These methods retrieve values of various data types (e.g., getInt, getFloat, getString) from the preferences using the corresponding key.

·     Use Case: In the IoT project, the Preferences library can be used to store calibration values or user preferences related to the PZEM sensor, like the Wi-Fi SSID and password for connecting to the internet, cloud platform API keys, calibration factors, or any other configuration settings.

What is Bharat Pi:

In this project, we are using Bharat Pi which has both ESP32 microcontroller and a SimCom A7672S 4G/LTE module.

Bharat Pi is an IoT prototyping board for students, innovators, startups, and developers. A simplified board with an all-in-one compute, network and storage on a single board to build rapid prototypes.

Bharat Pi bundles the power of ESP32, 4G/LTE module in a Arduino form factor. This makes Bharat Pi compatible with all Arduino and ESP32 programs and shields

Wireless monitoring can be done using WiFi or with a Sim based 4G/LTE connectivity.

Bharat Pi is a rugged board with good quality and could be used even for production and live customer deployments!



Hardware Setup

aaaaaaaa.jpg
Untitled Diagram.drawio (2).png

1.  Hardware Setup:

·       The PZEM-004T sensor is connected to the microcontroller (e.g., Arduino, ESP8266, or ESP32) using jumper wires. The current coil is clamped around the wire carrying the AC current to be measured.

·      The microcontroller is also connected to a Wi-Fi module (if not built-in) to enable internet connectivity.

2.  Preferences Initialization:

At the beginning of the program, the Preferences library is initialized with a unique namespace, which will be used to store the configuration settings.

3.  Calibration (optional):

Depending on the specific PZEM sensor and current coil used, calibration might be necessary to ensure accurate readings. Calibration factors or correction values can be stored in the Preferences.

4.  Reading and Storing Data:

The microcontroller reads electrical parameters (e.g., voltage, current, power) from the PZEM sensor at regular intervals.

The obtained data is then stored in the Preferences using appropriate keys (e.g., "voltage", "current", "power").

Above diagram shows the complete wiring illustration, don’t forget to give the detector mains input and also to sense the current, one of the lines for cargo should go through the ct detector.

 


Note: Before making any connections, ensure the circuit is disconnected from the main power source to avoid accidents.

Code Snippet

Screenshot 2023-07-26 160314.png
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Preferences.h>
#include <PZEM004Tv30.h>

// LCD settings
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

// PZEM settings
PZEM004Tv30 pzem(Serial2, 16, 17); // RX, TX

// Preferences settings
Preferences preferences;
unsigned long lastUpdateTime = 0;
float totalPowerConsumed = 0.0;
float totalCurrentConsumed = 0.0;
float currentConsumedToday = 0.0;
float powerConsumedToday = 0.0;

void setup() {
 // Initialize the LCD
 Serial.begin(115200);
 Serial.begin(115200);
 lcd.begin();
 lcd.setCursor(0, 0);
 Serial.println("power monitoring");
 delay(500);
 lcd.print("Power Monitoring");
 lcd.setCursor(0, 1);
 lcd.print("by refillbot.com");
 Serial.println("by refillbot.com");
 delay(500);

 delay(2000);
 lcd.clear();
 lcd.begin();
 lcd.backlight();

 preferences.begin("power_prefs", false);

 totalPowerConsumed = preferences.getFloat("totalPowerConsumed", 0.0);
 totalCurrentConsumed = preferences.getFloat("totalCurrentConsumed", 0.0);

 // Print initial values on LCD
 lcd.setCursor(0, 0);
 lcd.print("Power: -- W");
 lcd.setCursor(0, 1);
 lcd.print("Current: -- A");
 delay(2000);
}

void loop() {
 unsigned long currentTime = millis();
 if (currentTime - lastUpdateTime >= 1000) {
  lastUpdateTime = currentTime;

  // Read the voltage, current, power, and frequency from the PZEM
  float voltage = pzem.voltage();
  float current = pzem.current();
  float power = pzem.power();
  float frequency = pzem.frequency();

  // Calculate energy consumed in 1 second
  float energyConsumed = power / 3600.0;

  // Calculate energy consumed in one day (24 hours)
  float energyConsumedOneDay = energyConsumed * 24.0;

  // Calculate power consumed in one day
  powerConsumedToday += energyConsumed;

  // Calculate current consumed in one day
  currentConsumedToday += current;

  // Update total cumulative values
  totalPowerConsumed += energyConsumed;
  totalCurrentConsumed += current;

  // Save the cumulative values to Preferences library
  preferences.putFloat("totalPowerConsumed", totalPowerConsumed);
  preferences.putFloat("totalCurrentConsumed", totalCurrentConsumed);

  lcd.setCursor(0, 0);
  lcd.print("Power: ");
  lcd.print(power);
  lcd.print(" W");

  lcd.setCursor(0, 1);
  lcd.print("Current: ");
  lcd.print(current);
  lcd.print(" A");

  Serial.print("Power: ");
  Serial.print(power);
  Serial.println(" W");

  Serial.print("Current: ");
  Serial.print(current);
  Serial.println(" A");

  delay(4000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Frequency: ");
  lcd.print(frequency);
  lcd.print(" Hz");

  lcd.setCursor(0, 1);
  lcd.print("Energy: ");
  lcd.print(energyConsumed, 3);
  lcd.print(" Wh");
  delay(4000);

  Serial.print("Frequency: ");
  Serial.print(frequency);
  Serial.println(" Hz");

  Serial.print("Energy: ");
  Serial.print(energyConsumed, 3);
  Serial.println(" Wh");

  delay(4000);

  lcd.begin();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Power C: ");
  lcd.print(powerConsumedToday, 3);
  lcd.print(" Wh");

  lcd.setCursor(0, 1);
  lcd.print("Voltage C: ");
  lcd.print(totalPowerConsumed, 3);
  lcd.print(" Wh");
  delay(4000);

  Serial.print("Power Consumption Today: ");
  Serial.print(powerConsumedToday, 3);
  Serial.println(" Wh");

  Serial.print("Voltage Consumption Total: ");
  Serial.print(totalPowerConsumed, 3);
  Serial.println(" Wh");

  delay(1000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Current_c: ");
  lcd.print(currentConsumedToday, 3);
  lcd.print(" A");

  lcd.setCursor(0, 1);
  lcd.print("Current_T: ");
  lcd.print(totalCurrentConsumed, 3);
  lcd.print(" A");
  delay(4000);

  Serial.print("Curren _C _T: ");
  Serial.print(currentConsumedToday, 3);
  Serial.println(" A");

  Serial.print("Current Total: ");
  Serial.print(totalCurrentConsumed, 3);
  Serial.println(" A");

  delay(1000);
 }
}



Application

1.  Home Energy Monitoring: Implementing the PZEM sensor in residential settings allows homeowners to monitor real-time power consumption, voltage, current, and power factor. This data helps in identifying energy-hungry appliances, optimizing energy usage, and reducing electricity bills.

2.  Industrial Power Management: In industrial environments, power monitoring systems can be used to track the energy consumption of different machines, production lines, and processes. This information aids in identifying energy inefficiencies and optimizing operations for better energy management and cost savings.

3.  Data Centers: Data centers require precise monitoring of power usage and cooling systems. PZEM sensors can be used to monitor power consumption and temperature to ensure efficient operations and prevent equipment failures due to overheating.

4.  Smart Grid Integration: Power monitoring systems can be integrated into smart grid infrastructure to enable real-time tracking of power consumption across the grid. This data can help utility companies manage peak loads, balance energy distribution, and implement demand-response strategies.

5.  Renewable Energy Systems: PZEM sensors can be used in conjunction with renewable energy systems like solar panels and wind turbines to monitor the energy generated, consumed, and fed back to the grid. This enables users to optimize the usage of renewable energy and track the system's performance.

6.  Building Automation: In commercial buildings, power monitoring systems can be integrated into building automation systems to monitor and control energy consumption, lighting, HVAC systems, and other electrical devices to enhance energy efficiency and sustainability.

7.  Energy Auditing: Power monitoring systems are valuable tools for energy auditors to assess energy usage patterns in residential and commercial buildings. The collected data helps auditors identify areas for energy conservation and recommend improvements.

8.  Electrical Equipment Monitoring: PZEM sensors can be employed to monitor the health and performance of electrical equipment such as motors, transformers, and generators. Anomalies in power consumption or power factor can indicate potential issues or maintenance requirements.

9.  IoT-Based Energy Management: Integrating PZEM sensors with IoT platforms enables remote monitoring and control of power consumption. IoT applications can analyse data, generate insights, and trigger actions based on energy consumption patterns.

10.              Research and Education: Power monitoring systems using PZEM sensors are also useful for research purposes, academic projects, and educational demonstrations to understand electrical behaviour and study power-related phenomena.