Computer Related > PZEMs Computing Issues
Thread Author: Kevin Replies: 29

 PZEMs - Kevin
As promised, a (non-technical) writeup about PZEMs.

What is a PZEM and what does it do?

A PZEM is a little device about 8cm x 3cm that measures the AC electrical power flowing through a cable. It does this by continuously measuring the current flow in the cable and the voltage applied to the cable. It's a bit more intelligent than a simple voltmeter and ammeter though because it also detects the timing of the current and voltage and can therefore calculate the Power Factor and Frequency. Knowing the Power Factor allows it to calculate the real power consumption which is what your domestic electricity meter records and what you are billed for by your supplier. It is accurate to 1% or better, typically 0.5%. The data interface is 5V TTL levels so it can communicate with any PC or microcontroller that will tolerate 5V signals.

Does it need to be installed by an electrician?

Not really. The PZEM has a terminal block with 4 screw terminals at one end. Two of the terminals are for attaching the current sensor (more later) and the other two are for connecting to the supply voltage. The current sensor can be clipped around the cable being monitored without disconnecting it and the voltage terminals can simply be connected to a 13A plug and plugged into the nearest mains socket.

OK that looks like an interesting toy but there are different PZEMs being sold, what's the difference?

The core gubbins of all PZEMs I've seen on sale are essentially the same although I've also seen ads for 'Version X' or 'Version Y' etc. I think these are probably just different revisions to the PCB or chips used. Read the ads and decide for yourself if 'Version Y' has something that 'Version X' doesn't.
Where they do differ is in what extra bits are supplied with the PZEM. Almost without exception they are supplied as just a bare unit and you have the option to include a plastic case (sometimes called a shell), a USB interface cable or both. There are also different types of current sensor (transformer) available. You have to choose a measurement range of either 0 - 100A or 0 - 10A, and how the transformer is fixed to the mains cable. It can be either 'solid' core or 'split' core. Solid or closed core means that you have to disconnect and isolate the mains to slip the transformer around the cable. Split or open core means that you can just hinge the two halves apart and clip them around the cable without disconnecting.

Which one should I buy?"

Unless you are building your own enclosure for a permanent professional looking installation* go for a 100A split core unit already in a case, plus the USB interface (NB. Some suppliers do not have a USB cable option so make sure that it's included or added). This means that as soon as you receive it you're ready to try it out and decide if you can be bothered to go any further. All you need to do is connect a 13A plug, clip the current sensor around the cable, install the software and possibly a driver, hook up your laptop and you're ready to go.

* I've noticed that there is now a version available that is already built into an enclosure complete with multiple 7-segment displays that show the running readings. I don't know any technical details so you're on your own with that one.

Mmm. Connecting a 240V doohickey to my laptop? I'm not sure about that."

The data interface is a 4 wire socket on the PZEM. Two wires carry data which are isolated from the 240V section of the PZEM by opto-isolators. The other two wires carry a 5V supply to power the isolators. Unless the unit is physically damaged or subject to condensation or water ingress there shouldn't be any problem.

Where do I buy one?

I bought mine about 18 months ago from BangGood but you can now get them from Amazon or eBay etc. (Other online retailers are available). Prices and delivery times vary so check around and double check that you've selected a 100A split-core with case and USB cable before you hit the 'Buy' button!

I'll show some example output and explain how mine is semi-permanently installed and talks over Wifi in another post but I think that's enough to get started. If I've missed something or anything's not clear, just shout.

 PZEMs - smokie
I'm in. Autumn/Winter project so I need a shopping list for Ali Express :-)

This one appears to come with the split CTR and the USB thing. tinyurl.com/mtjwx429 so I ordered one, will arrive early Sept apparently.

What would I also need for the WiFi (or more preferably Zigbee!) ? Is that the ESP board?
 PZEMs - Kevin
I'll add some info about what I use for WiFi this evening but this is where it get's a little more involved - to try and keep it simple and fit in with whatever you might already have.
 PZEMs - Zero
Yeah incorporated one of those (without the USB) in my high power home brew 13.8v power supply a while back

As Smokes says. Part two please, communication & data collection/analysis .
 PZEMs - Crankcase
Appreciate your time on this one Kevin. Already learned enough to save considerable time shopping about.
 PZEMs - Crankcase
Ok, well Mr Amazon is tricky on this. Finding, as Kevin suggested, a unit that ticks all the boxes of "100A split core cased with USB" took a little hunting, but here, I think, is one, for £18.58.

www.amazon.co.uk/Voltmeter-PEACEFAIR-Multi-function-Frequency-Accuracy/dp/B08JH7MN4J/

Any other suggestions or corrections before buy button gets pressed?
 PZEMs - smokie
Yes, appreciated Kevin, thanks :-)

CC did you see the AliExpress one in my link? Choose the third pic along, that seems to fit the bill perfectly - and delivery time isn't so far from your Amazon one (though as you'll know, not always to be trusted!).

I so nearly bought two because when i see it working I'll almost def want a second and maybe a third, but managed to resist. I'm proud of me, even if no-one else is :-)
Last edited by: smokie on Sun 14 Aug 22 at 10:05
 PZEMs - Crankcase
I didn't, Smokie, because my pihole blocks it. I've never gone to AliExpress so not noticed it must be on a blacklist.

I could bypass it but I'm happier with Amazon anyway to be honest.

Mind you, I just looked at the meter cupboard and it's cables everywhere. There are eight coming out of the bottom of the meter alone, and a mahoosive one coming in with 450V written in it, so sorting out what's what will be fun.
 PZEMs - smokie
I was trying with PiHole again a while back, then AdGuard then NextDNS. I still seemed to have trouble with some stuff, though I didn't try very hard I must admit. That's another Autumn project...

I do wonder if your Amazon one is also coming direct from China anyway, given the leadtime.
 PZEMs - Kevin
CC,

Two possible opportunities with that one:

First - The one I have was called a PZEM-004T I think, in a green transparent case. Although the functionality between the two looks identical I can't say with any certainty that the command set is the same although it probably is.

Second - The USB interface on that one only has two terminals, presumably for data so you would need to provide interface power from a seperate supply. Mine (and others I've seen) has a 4-wire connection that also provides power from USB.

Like this one. (Mine was cheaper but supplied from China)

tinyurl.com/bdya564v
 PZEMs - sherlock47
'clip the current sensor around the cable'

Surely you must clip around just L ( or N)

Not a problem if installing next to a meter or consumer unit, but needs a simple adaptor Plug/socket if you are going to try to monitor individual appliances.
 PZEMs - smokie
That's a good point, I'll need to rig up a suitable extension when (if!!) I get to that stage.
 PZEMs - Kevin
Yes,

it would be more accurate to say that it measures the power in a 'conductor' than a 'cable'.
 PZEMs - Kevin
By popular request:

PZEM Communication 1

To get the data out of the PZEM we need something that can talk to it over the serial interface, decode the data into something readable and send it to wherever we want it to go.
Luckily all the hard work has been done for us and open source code is available that will run on a cheap microcontroller with inbuilt WiFi and serial comms capability.
There are different versions of the microcontroller but they all come from a family of chips called ESP8266. I have used a version known as the ESP12E because it has more I/O pins and memory available than other versions and I wanted to connect multiple different devices to it.
I had to build all my own circuitry with power supply and a programming interface but you can now get a pre-built board that has the ESP12E with everything else included.

Like this one:

www.ebay.co.uk/itm/193721046862?hash=item2d1aac974e:g:GWIAAOSw-W1iqaWd

The onboard micro-usb acts as both power supply and a serial port for programming.

I need to check how the latest code builds have been packaged so I'll stop here. The ESP code that I use is called ESPEasy ( www.letscontrolit.com/wiki/index.php/ESPEasy ) so it's worthwhile browsing there.
 PZEMs - Kevin
Right, I checked the latest code status and it's good news - PZEM support is now included in an official package. That means that you don't need to use firmware that I cobbled together and pay my extortionate support fees.

You can get the latest packages1 from the GitHub repository linked on the ESPEasy page.

Assuming you use Windows.

1) Go to www.letscontrolit.com/wiki/index.php/ESPEasy
2) Click on "GitHub nightly builds"
3) Click on "Latest" on the RHS next to "Release xxx-YYYMMDD"
4) Scroll down the page until you reach "Assets" with a list of files with the same date as (3).
5) Download the file called ESPEasy_ESP82xx_xxx_YYYMMDD.zip and unpack it into a directory of your choice.
6) Change to the 'bin' sub-directory and unpack the "ESPEasy...energy_ESP8266_4M1M.bin.gz" file into the same directory.
7) Connect your ESP12E board to your PC with a standard USB micro cable.
8) Go back up to the main directory and run "ESP.Easy.Flasher.exe"2 as Administrator.
9) The flash tool should detect which COM ports are available and given you a drop down list. Select the COM port connected to your ESP12E board. It is usually shown as an FTDI USB port but if it is not shown you might need to install the driver. The most commonly used chip is the CH340 and you can get the driver here:

learn.sparkfun.com/tutorials/how-to-install-ch340-drivers/all

10) Once your PC is seeing the ESP, click on the "Firmware (.bin)" tab and select "blank_4MB.bin".

Some online ESP suppliers ship the ESP with a test program or a pseudo Hayes modem type utility and flashing the ESP NVRAM with a blank image ensures that you're starting with a clean ESP. If you want to save what's pre-loaded on the ESP there is a command-line tool called "esptool.exe" in the same directory that allows you to read the ESP NVRAM and write it to a local file.

11) Click "Flash ESP Easy FW" and wait till it finishes.
12) Now, from the "Firmware" drop-down list select the "energy" .bin file you unpacked earlier.
13) In the other tabs tick the box for "Post flash action" and enter the name of the unit3, a number(optional), the SSID and password of your WiFi network and (if you want/need) a fixed IP address.
14) Flash the firmware and wait for it to finish and say "DONE".

You should now have an ESP12E loaded with ESPEasy and reachable on your WiFi network. Open a browser tab, enter the ESP's IP-Address and have a poke around. The interesting bits for you at the moment will be the Controllers tab which tells you what type of host the ESPEasy can send data to, and the Devices tab which lists all the devices and sensors that the "energy" package knows about.

1 ESPs have limited memory so it is currently not possible to build a single firmware image that covers every device that ESPEasy can control. The firmware has therefore been split up into packages containing support for commonly used collections of devices. PZEM support is in the "energy" package. I compile my own package supporting specific devices that I use. The development environment is PlatformIO under Visual Studio Code (all free) if anyone wants to brew their own.

2 There is a bug in the flash tool. If you leave it loaded after it's finished flashing the ESP it starts to use more CPU than it should. Probably spinning on a read() call.

3 The post flash action should set the ESP network hostname to the unit name and number ie. PZEM01 but it didn't work for me. It set the hostname to energy-1. You can change it to what you want in the "Config" tab and click 'Submit'.

I'll post about wiring the ESP to the PZEM and adding it to ESPEasy in another post.
 PZEMs - smokie
Crumbs, this is starting to look a bit complicated! But I do like a challenge.

Many thanks again for going to the trouble of detailing the processes.
 PZEMs - Kevin
It isn't particularly complicated. That recipe basically equates to 'download this file, connect the board to your PC and run the flash program'.

I've tried to include enough info that anyone could do it by following it step-by-step but also a bit more of the 'what and why' for the inquisitive.

I think you'd all probably get there eventually if I just made a list of bits but I didn't want the abuse if it didn't work at the first attempt.
 PZEMs - Zero

>> but I didn't want the abuse if it didn't work at the first attempt.

How long did you spend in the IT game? you must be immune by now surely?
 PZEMs - Crankcase
Once again, thanks for such a detailed post seconded. At first read it did look a bit heavy, but a deep breath and a go round and yes, it's just a few steps.

So just need to get my head around the best way forward with all my cabling first.
 PZEMs - car4play
All good info Kevin - thanks. Very informative ;-)

As I'm an information lover and would like to monitor just about every circuit there is, and also, believe it or not, haven't actually got into any arduino coding, here is the solution I propose:

- base the whole lot on home assistant - (www.home-assistant.io). Install either on a pi or spare pc, or in my case I run it in docker on a synology NAS. This provides the interface and long term data and allows you to then automate based on power consumption etc. e.g. "when solar power and batteries are low, turn off x, y z"
- use an ESP32 - just buy the devkit board on EBay or Amazon.
- flash the board using ESP home (esphome.io). You need to buy the USB - serial flasher
(on Amazon "USB to TTL Serial adapter ")
- buy an SCT-013 for each circuit you want to monitor
- this is the practical bit. On a perf board wire up a circuit of a couple of resistors and a capacitor for each SCT-013. Circuit like this (savjee.be/uploads/2019-07-home-energy-monitor/wiring-arduino.webp). Use 2 pin DC power sockets soldered on for each SCT-013
- the rest is easy. For each SCT-013, choose one of the 15 ADC inputs into the ESP32.
- create an ESP.yaml config using the ADC config of esphome.io
- you might need to calibrate the readings coming in using various lamda functions you add into the ESP.yaml config - but they are literally one line each
- add in ESP home integration into home assistant and watch the readings come in
Done!


Disclaimer
I haven't actually made this circuit up and used it. I have although a couple of ESP's using similar to read pond or hydroponic water levels, ECs etc and turn on pumps valves etc. using this same kind of procedure, so I don't see a problem with the above.
 PZEMs - Kevin
I was hoping to get away with just the "More Informative Than a Smart Meter" bit.
 PZEMs - Kevin
Connecting the PZEM to the ESP

This is pretty simple, only 4 wires but there's a slight complication - the PZEM needs 5V for the opto-couplers and data but the ESP uses 3.3V. This means that we need both 5V and 3.3V supplies and to convert the voltage of the data signals between PZEM and ESP. Fortunately it's quite easy with one extra bit of cheap HW - a level shifter*.

Like these: tinyurl.com/2ewujcdb

For a permanent setup you're going to need to do a little bit of soldering to put everything on a bit of veroboard (or get someone to do it for you) but to just play around and get things working you'll probably get away with one of these: tinyurl.com/4rpacft4

The next bit assumes you're using the solderless breadboard but if you're confident just go ahead and go straight to veroboard.

Looking at the breadboard pic, the power module supplies power and ground to the outermost two rows on each side of the breadboard. They run the whole length of the board as a single row and are marked with a red or blue line. Jumpers on the power module allow you to select either 5v or 3.3v for each side.
Between the power rails, the 10 holes running width-wise form two rows of 5 pins each. These 5 pins are electrically connected. In cross-section the board is therefore: 5V_GND_XXXXX_YYYYY_5V_GND. Does that make sense?

So, to get the ESP running we just plug the ESP board lengthwise each side of the breadboard centreline, set one side of the power board to 5V and run jumper wires between those power rails and the 5V and GND pins on the ESP board. (The ESP board has a 5V input pin usually marked as "VIN" and an onboard 3.3V regulator.) When we connect power (USB or barrel connector) to the power board the ESP should start running and be reachable on your network.

When that's working we need to disconnect the power and add the level shifter...Next time...

* Some recipes for connecting a PZEM to 3.3V logic show how to connect without level shifters. I wouldn't recommend it.

A photo of my PZEM installed: ibb.co/8YC4T1x

The ESP, level shifter and voltage regulator in a box: ibb.co/S76Lc0X

A comparison of the data from a smart meter and the PZEM: ibb.co/tzQXrnC
 PZEMs - car4play
I didn't know level shifters were a thing. They could be useful.
Up to now I've just dropped the 5V across a potential divider made up of a 1K and 2K resistor in series with the 2K to ground, giving an output at the middle of:
5V x 2/(1+2) = 3.3V
 PZEMs - Kevin
Voltage dividers are perfectly acceptable in many cases but can't be used in others because they don't work in both directions or they alter the impedance significantly.
Level shifters are bi-directional, fast and are capable of driving heavier loads that your source may be able to - think cables or remote devices.

A voltage divider will work for our PZEM but it means opening the case and soldering a resistor to the PCB. I don't like that because it's fiddly and there's 240V in there. It also means that you can't just swap the PZEM for another if it isn't working as it should.
 PZEMs - Kevin
A level shifter is a simple chip that converts one logic level to another logic level. For example converting a 3.3V signal to a 5V signal. It can do it in both directions and has loads of advantages over a voltage divider.
As mentioned above, for connecting a PZEM to the ESP it's much safer and easier to maintain.
The ones I linked to come with four shifters on the pcb and six connections down each side. Four connections for signals and two for voltage supply. The lower voltage connectors are marked 'LV' and the higher voltage ones 'HV' so all we need to do to connect the PZEM to the ESP is:

1) Supply 5V and GND to the HV and GND pins on the shifter and the PZEM.
2) Connect the PZEM TX and RX lines to HV1 and HV2 on the shifter.
3) Supply 3.3V and GND to the LV and GND pins on the shifter.
4) Connect the data pins we want to use on the ESP to LV1 and LV2.

It easier to understand with a diagram: ibb.co/SXG4BXD
 PZEMs - smokie
This was delivered today. tinyurl.com/hpxuuv92 , a PEACEFAIR 80-260VAC 100A Multifunction Meter, Voltage and Frequency Tester, PZEM-016

I honestly think this project may be beyond me (I don't usually do anything requiring soldering for starters, or which mentions breadboards) but I'll work out what else I need to buy and get it on it's way.

Looks like I need

A usb with four terminals not two
The WiFI board mentioned above - NodeMCU V3 ESP8266 ESP-12E CH340 Lua IOT Wireless Wifi Modul Developmet Board UK
The level shifter mentioned above Youmile 10Pcs Level Converter 5V to 3.3V Bi-Directional Logic Module 4 Channels IIC I2C for Arduino with DuPont Cable

Is there anything else?
 PZEMs - Kevin
Smokie, is it one with a solid current transformer and not the split version? If so you'll need to either isolate and disconnect the incoming live conductor to slip the CT over the cable or buy a split core transformer (I think they're available seperately on ebay).

Hang on before ordering the other stuff though. I think it's gonna be a PITA if everyone needs to buy stuff they're only likely to use once so I'll do you a deal.

If you act as email and shipping agent, and there aren't too many needed, I'll send you a few boards with headers, level shifter and terminals pre-soldered for you to send to anyone wanting one. Gratis. All you'll need to do is plug in the ESP, wire up the PZEM and 5V supply and mount it in a box.
 PZEMs - smokie
Too late, I ordered everything :-) but I like the idea of having a half built project delivered!!! And I don't mind being a mail agent, if there's enough interest.

The one I ordered was the one with the split CT, as per link in my post above - but the third pic along, not the default one.
 PZEMs - Kevin
My apologies for not updating this for a while. I got sidetracked by other things.

I'll try and finish it off later today or tomorrow.
 PZEMs - smokie
Many of my bits have turned up from China but I'm saving the project till later in the winter... :-)
Latest Forum Posts