Source: https://mokxi.com/libraries/pzem004tv30
Updated: 2026-11-22

Libraries / Sensors

Arduino library · Sensors

# PZEM004Tv30: an AC energy meter over Modbus

Yes. You can use the PZEM004Tv30 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (AC energy meter, PZEM-004T v3.0). The example below, "Volts, amps, watts and kWh from a PZEM-004T with PZEM004Tv30", runs on the Arduino Uno R3.

Run the example in Mokxi See the calls

Built in on Mokxi: #include <PZEM004Tv30.h> and it builds, with nothing to install. Free, and no account needed.

## What PZEM004Tv30 does

The PZEM-004T v3.0 is a module that meters mains power. It measures the AC voltage across its terminals and the current through its coil, and reports voltage, current, real power, accumulated energy, frequency and power factor over a TTL serial line using Modbus-RTU. Jakub Mandula’s PZEM004Tv30 library turns each value into one function call.

The calls cost less than they look. Each one reads all ten measurement registers in a single request and caches them, and the others return the cached values for the next 200 ms, so reading all six in a row is one trip on the wire. A failed read, meaning no answer or a bad CRC within 100 ms, returns NAN, which is the first thing to test for. The energy count lives in the meter and keeps growing until resetEnergy() clears it.

The example uses SoftwareSerial on an Uno, with the meter’s TX into pin 2 and its RX from pin 3. A Mega can use Serial1 to Serial3, and a classic ESP32 reaches the meter with PZEM004Tv30 pzem(Serial2, 16, 17). On the ESP32-S3, C3 and C6 boards in Mokxi, Serial1 and Serial2 share the serial monitor, so a meter there is never reached and every reading is NAN. In Mokxi the mains values are sliders on the pzem004t part; on a real build the AC side carries mains voltage and belongs in a closed enclosure.

Library Manager name | PZEM004Tv30

Author | Jakub Mandula

Include | #include <PZEM004Tv30.h>

Boards | Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | AC energy meter, PZEM-004T v3.0

## Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU.

In Mokxi it is built in: #include <PZEM004Tv30.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "PZEM004Tv30" by Jakub Mandula from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add PZEM004Tv30 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.2.1, so a sketch written here builds there too.

What to know in the simulator: The mains is set on the part’s sliders. On the classic ESP32 boards PZEM004Tv30 pzem(Serial2, 16, 17) talks to it over the real UART2.

- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- Arduino Pro Mini 3.3 V
- ESP8266 NodeMCU

## The calls you will use

In the order a sketch meets them.

### PZEM004Tv30 pzem(pzemSWSerial)

Creates the driver on a SoftwareSerial port, declared as SoftwareSerial pzemSWSerial(2, 3), and starts it at 9600 baud. A second argument picks a meter address.

### PZEM004Tv30 pzem(Serial2, 16, 17)

The classic ESP32 form: a hardware UART with its RX and TX pins.

### pzem.voltage()

RMS voltage in volts, or NAN when the meter did not answer.

### pzem.current()

Current in amps; pzem.power() is real power in watts.

### pzem.energy()

Energy counted since the last reset, in kWh. Print it with three decimals to see small loads add up.

### pzem.pf()

Power factor from 0 to 1; pzem.frequency() is the mains frequency in Hz.

### pzem.resetEnergy()

Clears the meter’s energy counter and returns true when the meter confirms.

### pzem.setAddress(0x01)

Gives a meter its own Modbus address so several can share one serial line. readAddress() asks the meter for its current one.

### pzem.setPowerAlarm(1500)

Sets an alarm threshold in watts. getPowerAlarm() returns true while power is above it.

## Example: Volts, amps, watts and kWh from a PZEM-004T with PZEM004Tv30

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

### Wiring

- Arduino Uno R3 2 to AC energy meter, PZEM-004T v3.0 TX
- Arduino Uno R3 3 to AC energy meter, PZEM-004T v3.0 RX
- Arduino Uno R3 5V to AC energy meter, PZEM-004T v3.0 5V
- Arduino Uno R3 GND to AC energy meter, PZEM-004T v3.0 GND

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### Error reading voltage

voltage() returned NAN because no valid answer came back. Check that the meter’s TX goes to the board’s receive pin and its RX to the transmit pin, and that the SoftwareSerial pins match the wiring.

### The first reading after reset is 0

The library skips reads during its first 200 ms. Put delay(250) in setup() before the first call, as the example does.

### Two meters on one line return the same values, or garbage

Both answer the general address 0xF8. Connect one meter at a time and call setAddress() to give each a different address, then create one PZEM004Tv30 object per address.

### Every reading is NAN on an ESP32-S3, C3 or C6

On those boards in Mokxi, Serial1 and Serial2 share the serial monitor rather than reaching the part. Use a classic ESP32 with Serial2 on pins 16 and 17, or an Uno or Mega.

## Questions and answers

### Why is energy 0.000 kWh when the example starts?

The count starts from zero on a fresh meter and grows as power flows. At 480 W it takes a while to show in the third decimal place.

### Can I test PZEM code without connecting to mains?

In Mokxi, yes: the pzem004t part’s sliders set the voltage, current, power factor and frequency, and the sketch reads them over the same Modbus requests.

## Projects that use PZEM004Tv30

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

ESP32 PZEM-004T energy monitor A PZEM-004T v3.0 AC energy meter on the ESP32’s Serial2: voltage, current, power, kWh, frequency and power factor over Modbus. PZEM-004T energy monitor A PZEM-004T v3.0 AC energy meter on Modbus: the Uno prints the voltage, current, power, kWh, frequency and power factor.

## Keep going

### Learn

- Arduino PZEM-004T and ZMPT101B AC Energy Meter
- UART Serial Explained: Baud, Frames and the Wire

### Parts

- AC energy meter, PZEM-004T v3.0

### More sensors libraries

- DHT sensor library
- DHT20
- VL53L0X
- Adafruit_VL53L0X
- HX711 Arduino Library
- MPU6050
- Adafruit MPU6050
- BME280
- SparkFun BME280
- MCP4725
- DHT sensor library for ESPx
- SparkFun BMP180 (SFE_BMP180)
- ADS1115_WE
- Adafruit DPS310

Every built-in library · Open the editor

## Try PZEM004Tv30 without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
