PZEM-004T v3.0 Arduino Library

repository·master·Indexed 43 days ago

https://github.com/mandulaj/pzem-004t-v30

An Arduino library for Peacefair PZEM-004T-10A and PZEM-004T-100A v3.0 energy monitors. It uses the Modbus-RTU protocol to measure voltage, current, power, energy, power factor, and frequency. Supports Hardware Serial, Software Serial, and multi-device configurations on a single bus.

Tokens
2.3K
Snippets
8
Records
12
Agent score
56%

What's inside PZEM-004T v3.0

  1. Configure multiple PZEM devices on one bus

    master

    The PZEM-004T v3.0 supports 247 unique programmable slave addresses, allowing multiple devices to share the same Serial interface.

    To use multiple devices:

    1. Set a unique custom address for each device using the PZEMChangeAddress example.
    2. Use the PZEMMultiDevice pattern to address them individually.
    3. Note: You may need external transistors to drive multiple devices on a single bus due to GPIO current limitations on some MCUs.
  2. Access PZEM-004T v3.0 documentation and schematics

    master

    For hardware setup, wiring, and technical specifications of the PZEM-004T v3.0, refer to the following resources:

    • Datasheet & User Manual: The PZEM-004Tv3.0 datasheet contains MODBUS commands and technical specifications.
    • Hardware Modifications & Schematics: TheHWcave provides reverse-engineered schematics, hardware modification (hwmod) guides, and Python interface software.
    • Basic Setup: Oleh's WiKi covers basic setup, 3.3v HWMOD, and connecting to ESP hardware serial.
    • Multi-device Serial Sharing: For instructions on how multiple devices can share the same UART lines via hardware modification, see the Multiple PZEMs serial sharing MOD.
  3. Troubleshoot PZEM-004T v3.0 connection issues

    master

    If you are experiencing issues with the module, check the following:

    • Power Requirement: The PZEM device must be connected to 230V AC power. The 5V pin only powers the optocouplers, not the main chip.
    • Wiring: Ensure both 5V and GND are connected; these are essential for the optocouplers.
    • Communication Errors: If you receive NaN values and only the TX LED is blinking, try swapping the RX/TX wires.
    • Address Mismatch: Ensure you are using the correct address. The default address is PZEM_DEFAULT_ADDR (0xF8), which only works for a single device on the ModBus bus. For multiple devices, you must set a custom address for each.
    • High Current Readings: If current readings are unexpectedly high (e.g., 0.5A for a 60W device), it is likely due to a low power factor (< 1).
  4. Initialize PZEM004Tv30 with Hardware Serial

    master

    Use the PZEM004Tv30 constructor with a hardware serial object. On boards like the ESP32, you can specify the RX and TX pins directly in the constructor. Note that on boards like the Arduino Uno, using Hardware Serial may conflict with debug output on the Serial console.

    #include <PZEM004Tv30.h>
    
    #if defined(ESP32)
    PZEM004Tv30 pzem(Serial2, 16, 17);
    #else
    PZEM004Tv30 pzem(Serial2);
    #endif
    
    void setup() {
        Serial.begin(115200);
    }
  5. Initialize PZEM004Tv30 with Software Serial

    master

    For MCUs without sufficient hardware serial ports, use the <SoftwareSerial.h> library. Pass the SoftwareSerial instance to the PZEM004Tv30 constructor.

    #include <PZEM004Tv30.h>
    #include <SoftwareSerial.h>
    
    SoftwareSerial pzemSWSerial(11, 12); // RX, TX
    PZEM004Tv30 pzem;
    
    void setup() {
      Serial.begin(115200);
      pzem = PZEM004Tv30(pzemSWSerial);
    }
  6. Read energy measurements from PZEM-004T v3.0

    master

    The library provides methods to read various electrical parameters. If a reading fails, the method returns NaN.

    Available measurement methods:

    • pzem.voltage(): Voltage (V)
    • pzem.current(): Current (A)
    • pzem.power(): Active Power (W)
    • pzem.energy(): Active Energy (kWh)
    • pzem.frequency(): Frequency (Hz)
    • pzem.pf(): Power Factor
    • pzem.readAddress(): Returns the current Modbus slave address.
    float voltage = pzem.voltage();
    float current = pzem.current();
    float power = pzem.power();
    float energy = pzem.energy();
    float frequency = pzem.frequency();
    float pf = pzem.pf();
    
    if(isnan(voltage)) {
        // Handle error
    }
  7. Configure debug logging

    master

    You can enable debug logging by defining PZEM004TV30_DEBUG in your build configuration. This enables the following macros:

    • DEBUG(...): Prints formatted text to the debug serial port.
    • DEBUGLN(...): Prints formatted text followed by a newline.
    • DEBUGBUF(buf, len): Prints a hex dump of a buffer.

    By default, PZEM004TV30_DEBUG_SERIAL is set to Serial. You can override this by defining PZEM004TV30_DEBUG_SERIAL to a different HardwareSerial instance.

    // In your build flags or at the top of your sketch
    #define PZEM004TV30_DEBUG
    #define PZEM004TV30_DEBUG_SERIAL Serial1
    
    // Usage in code
    DEBUG("Initializing...");
    DEBUGBUF(myBuffer, 10);
  8. Find PZEM-related libraries and integrations

    master

    If you are looking for software implementations or integrations for PZEM energy meters, consider these resources:

    • ESP32 RTOS (Non-blocking): Vortigont's pzem-edl lib is a non-blocking, event-driven library designed for PZEM-004Tv3.0 and PZEM-003 on ESP32.
    • Arduino (Legacy): Oleh's PZEM-004T lib is the original Arduino library for legacy versions of the device.
    • DC Version: Maxzerker's PZEM-017-v1 lib is for the DC version (PZEM-017-v1).
    • Tasmota Integration: Tasmota documentation provides details on embedded support for PZEM within the Tasmota firmware.
    • Zabbix Monitoring: zabbuino is a Zabbix agent firmware for Arduino that supports PZEM meters.
  9. Read energy measurements from PZEM-004T v3.0

    master

    Use the following methods to retrieve real-time electrical measurements. These methods return float values representing the measured parameters.

    float v = pzem.voltage();     // Voltage
    float i = pzem.current();     // Current
    float p = pzem.power();       // Power
    float e = pzem.energy();     // Energy
    float f = pzem.frequency();   // Frequency
    float pf = pzem.pf();         // Power Factor
  10. Manage device address and energy data

    master

    The library provides methods to modify the device's Modbus address, reset accumulated energy, and manage power alarms.

    // Address management
    bool success = pzem.setAddress(0x12);
    uint8_t addr = pzem.getAddress();
    
    // Energy management
    pzem.resetEnergy();
    
    // Power alarm management
    pzem.setPowerAlarm(1000); // Set alarm at 1000 Watts
    bool alarmActive = pzem.getPowerAlarm();
    
    // Scan for devices on the bus
    pzem.search();
  11. Initialize the PZEM004Tv30 class

    master

    The PZEM004Tv30 class can be initialized using different serial interfaces depending on your hardware (AVR, ESP8266, ESP32, etc.).

    For most boards, pass a reference to a HardwareSerial object. On ESP32, you must also specify the receive and transmit pins.

    Software Serial (AVR and ESP8266 only)

    If using AVR or ESP8266, you can pass a SoftwareSerial object. Note that the constructor PZEM004Tv30(uint8_t receivePin, uint8_t transmitPin, uint8_t addr) is deprecated; you should create a SoftwareSerial object first and pass it to the constructor.

    Stream Interface

    You can also pass any object that inherits from Stream.

    Default Address

    The default Modbus address is 0xF8 (PZEM_DEFAULT_ADDR).

    // Hardware Serial (Standard)
    PZEM004Tv30 pzem(Serial1);
    
    // Hardware Serial (ESP32 with custom pins)
    PZEM004Tv30 pzem(Serial1, RX_PIN, TX_PIN);
    
    // Software Serial (AVR/ESP8266)
    SoftwareSerial swSerial(RX_PIN, TX_PIN);
    PZEM004Tv30 pzem(swSerial);
    
    // Using a custom address
    PZEM004Tv30 pzem(Serial1, 0x12);