Projects / Whole-room energy meter with PZEM-004T

Whole-room energy meter with PZEM-004T

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32ABS

    Have the mains side installed

    By an electrician, in a closed box.

  2. ESP32

    Wire only the TTL side

    5 V, GND, TX, RX.

  3. ESP32

    Read one value

    Print the voltage.

  4. ESP324.98V

    Read all values

    Current, power, energy.

  5. ESP3212.9 mASSD1306

    Add the OLED

    Show live values.

  6. ESP32

    Add the web page

    Refresh every 3 seconds.

  7. ESP32ABS

    Compare with a known load

    A 100 W lamp reads about 100 W.

In short

How do you build a whole-room energy meter with PZEM-004T?

A PZEM-004T module with a clamp CT measures voltage, current, power and units used by a room, and shows them on a web page. You need ESP32 DevKit V1, PZEM-004T energy meter module, SSD1306 OLED Display, 5 V 2 A USB adapter and ABS project box (100 × 60 × 25 mm). It takes 7 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
PZEM mains terminals and CTWired by a qualified electrician in a closed, insulated enclosureMains side: never wire or touch it yourself
PZEM 5V and GND (TTL side)ESP32 VIN (5 V) and GNDLow voltage side power
PZEM TXESP32 GPIO16 (RX2)Data to the ESP32 (check its level: use a divider if it is 5 V)
PZEM RXESP32 GPIO17 (TX2)Data to the module
OLED SDA and SCLESP32 GPIO21 and GPIO22I2C
OLED VCC and GNDESP32 3V3 and GNDPower

Code

The PZEM-004T is a small meter module that measures the mains itself and reports the numbers over a serial link, with the low-voltage side kept separate from the mains side by isolation. That makes it a good way to learn about energy without building anything dangerous yourself. The mains terminals and the clamp-on current sensor must be wired by a qualified electrician, in a closed, insulated enclosure, with the right fuse or breaker, and the clamp goes around the live wire only, never around both conductors together. After that the ESP32 only listens on the low-voltage serial side. The module remembers the energy it has counted, and it can be reset with the library if you wish. Check the voltage level of the module's TX pin; if it is 5 V, put a resistor divider on the line to the ESP32. Measurements are good for learning and for finding what uses power, but are not a certified meter for billing. Put your own Wi-Fi details in the placeholders and keep the page on your own network, since it is not encrypted. Do not open the enclosure while it is powered. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Energy meter reader for the PZEM-004T v3: shows voltage, current, power and energy on an OLED and a web page.
// Libraries: PZEM004Tv30, Adafruit SSD1306, Adafruit GFX, WebServer. Board: ESP32 Dev Module.
// The mains side of the PZEM must be wired by a qualified electrician; this sketch only reads the low-voltage serial side.
#include <WiFi.h>
#include <WebServer.h>
#include <PZEM004Tv30.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const char* SSID = "YOUR_WIFI_NAME";
const char* PASS = "YOUR_WIFI_PASSWORD";
PZEM004Tv30 pzem(Serial2, 16, 17);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
WebServer server(80);
float v = 0, a = 0, w = 0, kwh = 0, hz = 0;
unsigned long lastRead = 0;
void readMeter() {
  float nv = pzem.voltage();
  if (isnan(nv)) return;                             // no reply: keep the old values
  v = nv; a = pzem.current(); w = pzem.power(); kwh = pzem.energy(); hz = pzem.frequency();
}
void handleRoot() {
  String s = "<html><body style='font-family:sans-serif'><h3>Room energy</h3>";
  s += "Voltage " + String(v, 1) + " V<br>Current " + String(a, 2) + " A<br>Power " + String(w, 0) + " W<br>";
  s += "Energy " + String(kwh, 3) + " kWh<br>Frequency " + String(hz, 1) + " Hz";
  s += "<script>setTimeout(function(){location.reload()},3000)</script></body></html>";
  server.send(200, "text/html", s);
}
void setup() {
  Serial.begin(115200);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(SSD1306_WHITE);
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED) delay(300);
  Serial.println(WiFi.localIP());
  server.on("/", handleRoot);
  server.begin();
}
void loop() {
  server.handleClient();
  if (millis() - lastRead > 2000) {
    lastRead = millis();
    readMeter();
    oled.clearDisplay(); oled.setTextSize(1); oled.setCursor(0, 0);
    oled.print("V "); oled.print(v, 1); oled.print("  A "); oled.println(a, 2);
    oled.setTextSize(2); oled.setCursor(0, 20); oled.print(w, 0); oled.print(" W");
    oled.setTextSize(1); oled.setCursor(0, 48); oled.print(kwh, 3); oled.print(" kWh");
    oled.display();
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

192.168.1.42

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
VoltageAbout mains voltage, steady
Known lampPower close to its rating
Appliance offPower near zero
EnergyKeeps counting after a restart

If it does not work

No readings

Check TX and RX are not swapped and the module has 5 V.

Garbled values

Check the baud rate and the level of the TX line.

Current reads zero

Check the clamp is closed and around the live wire only.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the whole-room energy meter with PZEM-004T?

ESP32 DevKit V1, PZEM-004T energy meter module, SSD1306 OLED Display, 5 V 2 A USB adapter and ABS project box (100 × 60 × 25 mm).

How long does the whole-room energy meter with PZEM-004T take to build?

About 180 minutes for a intermediate maker, in 7 steps.

How do I know the whole-room energy meter with PZEM-004T works?

Voltage: About mains voltage, steady. Known lamp: Power close to its rating. Appliance off: Power near zero.

No readings: what should I check?

Check TX and RX are not swapped and the module has 5 V.

Garbled values: what should I check?

Check the baud rate and the level of the TX line.

Current reads zero: what should I check?

Check the clamp is closed and around the live wire only.