Skip to content

mandulaj/PZEM-004T-v30

master
Switch branches/tags
Code

PZEM-004T v3.0

GitHub issues GitHub license PlatformIO Build Buy me a Beer

LINKS & REFs

Arduino library for Peacefair PZEM-004T-10A and PZEM-004T-100A v3.0 Energy monitor using the ModBUS interface.

The Version 3.0 PZEM is an upgraded version of the older PZEM-004T for which you can find the library Here

Main features

  • Measures Voltage, Current, Power, Energy, Power Factor and Frequency (New in Version 3.0)
  • 247 unique programmable slave addresses
    • Enables multiple slaves to use the same Serial interface PZEM MulitDevice Demo (May need to use external transistors to drive multiple devices on one bus due to GPIO current limitations)
  • Internal Energy counter up to 9999.99kWh

Other features

  • Over power alarm
  • Energy counter reset
  • CRC16 checksum
  • Better, but not perfect mains isolation

Common issue:

  • Make sure the device is connected to the AC power! The 5V pin only powers the optocouplers, not the actual chip.
  • If you are not reading any data, try swapping the RX/TX wires.
  • Please be safe, AC is dangerous! If you don't know what you are doing, you can die! You are responsible for your own safety.

The module

PZEM-004T v3.0 Image

This module is an upgraded version of the PZEM-004T with frequency and power factor measurement features, available at the usual places. It communicates using a TTL interface over a Modbus-RTU like communication protocol but is incompatible with the older @olehs library found here: https://github.com/olehs/PZEM004T. I would like to thank @olehs for the great library which inspired me to write this one.

Manufacturer (optimistic) specifications

Function Measuring range Resolution Accuracy TODO: Realistic specifications
Voltage 80~260V 0.1V 0.5%
Current 0~10A or 0~100A* 0.01A or 0.02A* 0.5%
Active power 0~2.3kW or 0~23kW* 0.1W 0.5%
Active energy 0~9999.99kWh 1Wh 0.5%
Frequency 45~65Hz 0.1Hz 0.5%
Power factor 0.00~1.00 0.01 1%

* Using the external current transformer instead of the built in shunt

Compatibility

MCU Hardware Serial Software Serial Not Tested Examples Notes
ATmega168 X HardwareSerial SoftwareSerial
ATmega328 (Arduino Uno) () ✔️ HardwareSerial SoftwareSerial HW Serial conflicts with Debug output. It can be used however without having any Serial Console output
ATmega2560 (Arduino Mega) ✔️ ✔️ HardwareSerial SoftwareSerial
ESP8266 () ✔️ SoftwareSerial HW Serial conflicts with Debug output Serial
ESP32 ✔️ HardwareSerial SW Serial not really needed as ESP32 has 3 HW serials with configurable pins
STM32 BluePill X

Examples

Hardware Serial

This example uses Hardware Serial2 in order to interface with the PZEM module. Note that not all MCUs feature multiple Serial ports. It won't for example work on the Arduino Uno.

#include <PZEM004Tv30.h>

/* Hardware Serial2 is only available on certain boards.
 * For example the Arduino MEGA 2560
*/
#if defined(ESP32)
PZEM004Tv30 pzem(Serial2, 16, 17);
#else
PZEM004Tv30 pzem(Serial2);
#endif

void setup() {
    Serial.begin(115200);

    // Uncomment in order to reset the internal energy counter
    // pzem.resetEnergy()
}

void loop() {
        
    Serial.print("Custom Address:");
    Serial.println(pzem.readAddress(), HEX);

    // Read the data from the sensor
    float voltage = pzem.voltage();
    float current = pzem.current();
    float power = pzem.power();
    float energy = pzem.energy();
    float frequency = pzem.frequency();
    float pf = pzem.pf();

    // Check if the data is valid
    if(isnan(voltage)){
        Serial.println("Error reading voltage");
    } else if (isnan(current)) {
        Serial.println("Error reading current");
    } else if (isnan(power)) {
        Serial.println("Error reading power");
    } else if (isnan(energy)) {
        Serial.println("Error reading energy");
    } else if (isnan(frequency)) {
        Serial.println("Error reading frequency");
    } else if (isnan(pf)) {
        Serial.println("Error reading power factor");
    } else {

        // Print the values to the Serial console
        Serial.print("Voltage: ");      Serial.print(voltage);      Serial.println("V");
        Serial.print("Current: ");      Serial.print(current);      Serial.println("A");
        Serial.print("Power: ");        Serial.print(power);        Serial.println("W");
        Serial.print("Energy: ");       Serial.print(energy,3);     Serial.println("kWh");
        Serial.print("Frequency: ");    Serial.print(frequency, 1); Serial.println("Hz");
        Serial.print("PF: ");           Serial.println(pf);

    }

    Serial.println();
    delay(2000);
}

Output:

Custom Address:10
Voltage: 229.60V
Current: 0.10A
Power: 4.50W
Energy: 7.368kWh
Frequency: 50.0Hz
PF: 0.19

Software Serial

Using the <SoftwareSerial.h> library...

#include <PZEM004Tv30.h>
#include <SoftwareSerial.h>

/* Use software serial for the PZEM
 * Pin 11 Rx (Connects to the Tx pin on the PZEM)
 * Pin 12 Tx (Connects to the Rx pin on the PZEM)
*/

SoftwareSerial pzemSWSerial(11, 12);
PZEM004Tv30 pzem;

void setup() {
  Serial.begin(115200);
  
  pzem = PZEM004Tv30(pzemSWSerial);
}

void loop() {
         
    Serial.print("Custom Address:");
    Serial.println(pzem.readAddress(), HEX);

    // Read the data from the sensor
    float voltage = pzem.voltage();
    float current = pzem.current();
    float power = pzem.power();
    float energy = pzem.energy();
    float frequency = pzem.frequency();
    float pf = pzem.pf();

    // Check if the data is valid
    if(isnan(voltage)){
        Serial.println("Error reading voltage");
    } else if (isnan(current)) {
        Serial.println("Error reading current");
    } else if (isnan(power)) {
        Serial.println("Error reading power");
    } else if (isnan(energy)) {
        Serial.println("Error reading energy");
    } else if (isnan(frequency)) {
        Serial.println("Error reading frequency");
    } else if (isnan(pf)) {
        Serial.println("Error reading power factor");
    } else {

        // Print the values to the Serial console
        Serial.print("Voltage: ");      Serial.print(voltage);      Serial.println("V");
        Serial.print("Current: ");      Serial.print(current);      Serial.println("A");
        Serial.print("Power: ");        Serial.print(power);        Serial.println("W");
        Serial.print("Energy: ");       Serial.print(energy,3);     Serial.println("kWh");
        Serial.print("Frequency: ");    Serial.print(frequency, 1); Serial.println("Hz");
        Serial.print("PF: ");           Serial.println(pf);

    }

    Serial.println();
    delay(2000);
}

Output:

Custom Address:11
Voltage: 229.60V
Current: 0.10A
Power: 4.50W
Energy: 7.368kWh
Frequency: 50.0Hz
PF: 0.19

Links

Check LINKS & REFs page for additional DOC's and related links

Contributing

Many thanks to all the other contributors that add new features, find bugs and generally keep this project afloa.


Thank you to @olehs for inspiring this library.