In this project, the Rpi-PICO2W board is used to interface with the electrical energy / power monitoring module PZEM-004Tv4. The PZEM-004Tv4 measures the electrical quantities of a sigle phase electric supply line which includes: AC Voltage, Current, Power, Power Factor, Frequency, Energy. On the PZEM-004Tv4 board, the electrical measurement section and the data communication HW are electrically isolated. That makes interfacing it with Rpi Pico/2/w safe. (Safe from isolation point of view only. But working with such modules, which are connected to AC supply, needs lot precuations and use of safe handling practices to prevent dangerous electrical shock.)
The physical interface is simple. The PZEM-004T module require connecting only 4 wires: Vcc, Gnd, TxD, RxD. The PZEM module accepts 5V or 3.3V for Vcc. The TxD and RxD lines are TTL level UART communication interface lines. From the Pico2W board - the UART 1 is used to interface with PZEM-004Tv4.
Though the physical interface is over TTL level UART lines, but the protocol provide on PZEM-004T is modbus-RTU. Baudrate: 9600, Data format: Databits: 8, StopBit: 1, No parity.
Refer the datasheet/user manual, provided in the documents folder, which cover the various commands supported by the PZEM-004T(but v3 and not v4) and it's response to those commands. The communication between PICO2W and PZEM-004tv4 is using only 3 commands:
- Pico initialises the PZEM-004T. (First PICO sets it's own UART for the required baud rate and data frame requirements of PZEM-004T, and then initialise it).
- Set's the slave address of PZEM-004tv4 as modbus-rtu protocol is to used.
- Finally reads the electrical parameters, at continuous interval, as measured by PZEM-004Tv4.
The important code, to handle communication with PZEM-004Tv4, is covered by below mentioned files:
- pzem-004t_modbus-rtu.c
- pzem-004t_modbus-rtu.h
- modbus-crc-16.h (not used in this implementation). These file are available from here: reference https://github.com/PaU1570/PZEM-004T_UART_Pico
The purpose of main.c is only to read and print the electrical parameters from pzem-004tv4 using above mentioned 3 files.
Raspberri Pi Pico 2 W, VSCode, pico sdk 2.2