www.webx.dk Start page
OZ2CPU Radioamateur mainpage
Homemade electronics Stuff old and new
Peacefair pzem-021 energy meter hacked SPI to SERIAL out AVR mega 88


The Peacefair PZEM-021 energy meter is very popular and super cheap at ebay
the unit is very good and usefull for all sorts of projects and to be used when working with mains powered electronics.

This energy meter is very well known and very much used by electronic geeks out there,
however it comes with NO WAY to connect it to the outside world,
with this hack its readings can go anywhere the user wishes, PC ? live online ? webpage ? test system ? what ever


Here is how the back unit looks like.


The first HACK: EXTERNAL DC Supply removed the 1uF 275V capacitor and 51 ohm powe resistor,
this way an external DC supply can be added to the 470uF 16V cam as shown, this mod makes it possible to use this unit with a variotrafo,
since now it can readout voltage down to ZERO, use a FLOATING ISOLATED DC supply offcourse !! (see bottom of page for variotrafo mod)

I used the DC supply for this unit while working with this project, this way unit was NOT connected to mains, PC and AVR programmer, is then safe to touch.

The inside parts of the Energy meter :
Renergy RN8208G the energy meter IC, SPI or SERIAL MODE, in this unit it is fixed to SPI OUT
HOLTEK HT1621D the LCD panel driver, SPI input of digits to drive, from the CPU.
STM32F030F4P6 the main CPU is from ST a powerfull 32 bit coretex ARM, impressive.
Note where the signals to the SERIAL converter mega88 is connected.


Inside the Energy meter chip RN8208G


A very good examply of hos this energy meter IC can be used, this project do something very similar to this.


The pinput of the RN8208G chip


The pin out of the main CPU STM32F030F4P6

Lets HACK the SPI between the energy meter chip and the main CPU, by listen in parallel to what is going on.

First some scope pictures. of the needed signals :
R9 right side = (DATA FROM ENERGY CHIP)
U2 pin 12 (CLOCK TO ENERGY CHIP)
U2 pin 11 (WORD LATCH TO ENERGU CHIP)
U2 7 (LATCH to DISPLAY CHIP, USED TO SYNC DATA BYTES INTO CORRECT VARIABLES)


First a zoom out to see the big picture : pakage repeat time is over 200mS, so plenty of time to handle data, and resend, once we got it


The entire pakage : little delay between each 8 clocks, reveal 8 bits decode is possible.
first data is 8 bits command to RN8208G, note data from RN8208G is floating while receiving command.
then 3 x 8 bits of VOLTAGE info is delivered on the SPI data from RN8208G, then a LATCH signal to indicate done, and start of next
the next is CURRENT, also command, 3 x 8 bits. latch. the last is WATTS, a bit different, command, and 4 x 8 bits, latch..


Here we zoom in on the first data part, VOLTAGE, note the command byte read 255 since data out from RN8208G is floating while it receive command.
The energy meter reads 0V in this case, the last part here shown as 181 is mV resolution, we simply choose not to use that byte


The next part zoomed in, is the CURRENT, same system, again we choose not to use the last mA byte


The last part is the WATTS, here they use an amazing 32 bits of resolution ! we choose only to use the first 3 bytes.
The value 0 0 3 makes the energy meter read out 0.0 watts so this prove no need for the last byte


The watts part again, here the energy meter is loaded with 50.0 watt and read out this value in the LCD.
this means the value 0 33 28 is converted ?? or calibrated some how, to display as 50.0 watt, our serial converter will do the same


Zoom in of clock edge vs data timing, so AVR data sample can be configured as the correct FALLING edge.

THE HACK AVR MEGA 88 SOURCE CODE DOWNLOAD: M88_spi_energymeter.zip
The software is ONLY FREE to use for own private projects.

ISOLATION NEEDED :
please note the MEGA 88 must run on 3.3V like the STM32 CPU, there are two voltage regulators on this unit, one is 5V and one is 3.3V
also note the entire energy meter unit is DIRECTLY CONNECTED TO MAINS VOLTAGE !!
so you need to add OPTOCOUPLER from MEGA88 serial out, to your PC serial input, I use a USB FTDI232 chip for this.
You could maybe also add a little 433MHZ wireless transmitter on the data from MEGA88, so your near by loacted receiver can see the data.
-----------------------------


Here a variotrafo with old defective Volt meter upgraded with this type of energy meter
used the external supply mod, and tiny 230V to 10V supply to drive the meter, this way it can read out from ZERO volts.

-----------------------------


Here my old analog test box was upgraded with this type of energy meter,
now I can much easier see the usage of smaller equipment I repair or construct.

-----------------------------


top : vario-transformator with this wattmeter,
again a little external dc supply was used, so it can read down to 0V

Bottom : isolation transformator, sometimes i use them together, or seperate.


100% own case design, 3D printed in one piece, just becourse it is possible :-)


the top scale is PCB, scale is carefully calibrated to show correct voltage,
then milled using my CNC pcb router.

-----------------------------

Thomas Scherrer OZ2CPU 2016
If you found this page usefull, why not consider donate a bit.. see my contact page please.