Measuring home energy consumption with the PZEM004T and ESP8266

First of all a very BIG WARNING: This project works with AC mains current, which, where I live, is 220V AC, meaning that extra precautions must be taken, since risk of serious injuries and/or death is possible.

The PZEM004T
The Peacefair PZEM004T device (available at the usual far east shopping web sites) is a device that can measure energy consumption by monitoring a live AC mains wire using an inductor as the measuring sensor. Take notice there are currently at least (3/2020) two versions: V2.0 which uses the standard serial protocol, and V3.0 that uses Modbus. This project uses V2.0.

One of the wires that carries the current (normally the AC power phase) goes through the inductor so that the current that flows through it can be measured and hence the other measurements, including power consumption, can be also measured.

The PZEM004T can be bought with two types of inductor, one that opens up and can clip on the wire of interest, and the other type that requires to disconnect and connect the wire of interest, so that it passes through the inductor core. I’ve chosen the former, since in this case I do not need to do any disconnection/connections on the electric mains board, and so it is way safer and easier to add and remove the measurement device.

PZEM 004T

The PZEMM04T outputs the collected data through an opto-coupled isolated serial port that allows to retrieve values for voltage, current/intensity, current power consumption and energy accumulated consumption.

The device that connects to the PZEM serial port must provide power to it (5V), and so the serial port data lines are 5V level, which means that we should use a 5v to 3.3V level converter to connect to the ESP8266. While there are several hacks to make the PZEM004T serial port to use 3.3V on the serial port, and hence have 3.3V data lines, I just used a simple level converter to connect the serial port to the ESP8266, and avoid in this way any modifications to the PZEM-004T. The serial port connector is a 4 Pin JST-XH connector.

So the basic schematics for using the PZEM004T is as simple as the following highly professionally drawn schematic shows:

PZEM004T And Wemos D1 connection schematic

Two things of notice:

  • The Ground connection – The serial port uses the same ground as the Wemos D1.
  • The Power supply – Wemos D1 is powered through the 5V pin, NOT through the 3.3v pin, since we need 5V to power up the PZEM serial port.

The level converter is just a simple, cheap I2C level converter, used in this case to level convert the serial data lines.
Also the above schematic shows that the TX and RX pins connect to the Wemos D6 and D5 pins, since I’ll be using software serial, but the depicted connections are just an example, since the pins to be used can be software defined.
In my code I use the connections the other way around ( D5-TX, D6-RX) so beware to how the pins are connected and how they are defined at the software level.

Powering up the ESP8266 Wemos D1
I’ll be using the Wemos D1 ESP8266 based boards, as we can see on the above schematics (associated to a prototype shield to solder the connections and the level converter), we need to power it up using 5V. The ESP8266 uses 3.3v, but the Wemos board has a 5V input and a 5V to 3.3V converter, so no issues there. The PZEM004T on the other hand uses 220V, and since the ESP8266 will be near the PZEM004T, it makes sense to get the 5V CC from the 220V AC to power up the Wemos D1 board.

The 220V AC to 5V CC can be achieved in several ways, and since I’ll be installing all this in a DIN case on my home electricity mains board, the easiest solution is just to buy a 5V output 220V based DIN power supply for around 10/15€. This is the easiest and safest solution.

There are other solutions, including the one that I’m using that is based on the 5V HLK-PM01 based modules. This requires some assembly and also be aware that there are fake HLK modules around.

Do not connect the HLK-PM01 without the associated protection components, namely fuses, VDR, and the most important component the thermal fuse of 72ºC (Celsius!) that will cut off the power to anything after it (including the VDR) if the temperature of the HLK module or it’s surroundings rises above the 72ºC temperature. I’ve not soldered the thermal fuse, since the heat from the soldering iron can destroy it, just used a two terminal with screws to connect it.

The schematic used is the following one:

5V Power supply

The PZEM-004T, the HLK based power supply and the Wemos D1 ESP8266 module are inside a double length project DIN case so that all components can be safely installed on the mains electricity board.

Since all is self contained on the DIN case, all is needed is to clip the inductor on the main phase wire entering the mains board (and it is easy since the inductor is an open clip on type), and connect the components to the 220V AC power. I’ve derived the power from one of the circuit breakers that already protects a house circuit, which adds an additional layer of protection.

The software
On the next post I’ll discuss the software for driving the ESP8266 to gather data from the PZEM004T and how it works.
The firmware for driving this is already available at: https://github.com/fcgdam/PowerMeter

15 thoughts on “Measuring home energy consumption with the PZEM004T and ESP8266

  1. Do you use the PZEM004T coil transformer with the phase line or the neutral line? I have seen in your diagram that its being used with the phase line but in most other diagrams I have seen it with neutral line. Can i use it with phase line and get the correct reading?

    1. The phase and neutral should carry the same current otherwise the differential protection would trip out and cut power (leak to earth for example). So either phase or neutral can be used, but I use it on the phase.

  2. Hi, I’m trying to connect to my PZEM004T but something goes wrong. I’m using ESP2866 and your test code.

    1. I really can’t help even in good will with such a short description of “goes wrong”.
      Did you check and double check the following:
      – Connections
      – Pins to TX and RX
      – Level shifter

      Without a more specific data it’s a bit hard to help.

    1. Check out v30 branch on the git repository and check it if it works with version v30. Since I don’t have such version I’m unable to do any tests. Just ported the code for the new v30 library. If the library has issues I’ll be unable to do anything really…

      1. Many thanks for your response and commitment, I’ll will test it out and report back.

        kindest regards:
        Jorge

  3. Thanks for your article. You made sure the AC-DC converter is properly protected, but I have the feeling the PZEM004T board has no such protections (I see no fuse on pictures, but I don’t own such a board to check). Am I right? If so, isn’t it dangerous?

    1. I wouldn’t connect this to mains power without some kind of protection, at least with a fuse and a thermal fuse.
      If it is dangerous? Without some protection (such as fuses) it might be calling for trouble. It is better to add some protection and be safe than sorry.

      1. I see no protection for the PZEM004T on your schematic. Did you actually protect it that way? Would sharing the protections between the HLK-PM01 and the PZEM004T be enough (that is, connecting AC1 and AC2 input of HLK-PM01 to the mains input of PZEM004T)?

        Don’t get me wrong, I’m not trying to be fussy here 😉 But some people who don’t know much about electricity/electronic (like me) might find your article and decide to realize what you describe without having a clue about the extra protection required for the PZEM004T. So I think the more exhaustive is the article about that the better.

      2. Yes, you are right, in the schematics there is no protection for the PZEM004T, just for the HLK. The reason is that I connected the PZEM004T after a 16A circuit breaker and so it is protected by that.
        I’ve chosen the 16A breaker because it was the lower available on main boards, but if there is one even lower available, like 6A, it should be use that.

        So the protection is not in the schematics, but I’ve installed after the mains and a 16A circuit breaker. If something goes wrong those breakers should pop right up and cut power to both the PZEM004t and the HLK. The HLK has it’s own set of protections since I really don’t trust these Chinese voltage converters because there are some HLK fakes floating around.

  4. I made this project and works great, thanks really appreciated. but i was not able to do OTA update the firmware. Plus is there any way that i could change SSID and password without uploading entire code, like changing AP mode with web server ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.