Skip to the content.

MycilaPZEM004Tv3

License: MIT Continuous Integration PlatformIO Registry

Arduino / ESP32 library for the PZEM-004T v3 Power and Energy monitor

Please read this good article first:

And also have a look at this inspiring and well documented GitHub project:

This project is an adaptation of the project above, focusing only on ESP32 and add support for async reading and async energy reset.

    volatile float current = 0; // A
    volatile float energy = 0; // kWh
    volatile float power = 0; // W
    volatile float powerFactor = 0;
    volatile float voltage = 0; // V
    volatile uint8_t frequency = 0; // Hz

Usage

Have a look at all the examples in the examples folder.

There is a getter for each metric.

Blocking mode

Mycila::PZEM pzem;

void setup() {
  pzem.begin(&Serial1, 14, 27);
}

void loop() {
  if (pzem.read()) {
    // access values
  }
  delay(1000);
}

Non-Blocking mode (async)

Mycila::PZEM pzem;

void setup() {
  pzem.begin(&Serial1, 14, 27, MYCILA_PZEM_DEFAULT_ADDRESS, true);
}

Calling read() is not necessary.

Energy reset

pzem.resetEnergy();

Read and set address

pzem.readAddress();
pzem.readAddress(true); // will read teh address and use it as the new address

pzem.setAddress(0x42);

Start a PZEM with a specific address

pzem.begin(&Serial1, 14, 27, address);

Json support

Json support is optional. If you need it, please add this compilation flag to activate it: -D MYCILA_PZEM_JSON_SUPPORT and do not forget to include the ArduinoJson library:

#include <ArduinoJson.h>

Multiple PZEM-004T v3 devices on the same RX/TX port

  1. Connect the first PZEM-004T v3 to the RX/TX port of the ESP32
  2. Restart and set the address of the first PZEM-004T v3 (you can use the SetAddress.ino file)
  3. Disconnect the first PZEM-004T v3 and connect the second one instead
  4. Restart and set the address of the second PZEM-004T v3
  5. Connect both PZEM-004T v3 to the RX/TX port of the ESP32
  6. Restart and use both with their own address

Reference material