Basic Modbus RTU Master RS485 Test with ESP32 + XY-K485 + PZEM-016 (without libraries)

In this opportunity we will carry out tests and explanation of the Modbus RTU protocol implemented in ESP32 requesting data from the Single-phase consumption meter PZEM-016.

The intent of this tutorial is:

1. Explain in a simple way the creation and operation of an industrial protocol, in this case the Modbus RTU protocol used and implemented in a large number of industrial applications.

2. Indicate the advantages of the XY-K485 module as a TTL to RS485 converter.

  • Does not require DE and RE enablement, only requires Rx and Tx.
  • Power from 3.3V to 30VDC.
  • Insulation, thermal fuses and protection (TVS bidirectional transient suppressor diode) in RS485 communication.
  • LEDs for indicating Rx reception and Tx transmission.

3. This implementation in ESP32 is purely explanatory, not to apply it directly to a real industrial application, since the basics of the Modbus RTU protocol are taken in a didactic way.

Recomendado Version en Español: Prueba Básica Modbus RTU Maestro RS485 con ESP32 + XY-K485 + PZEM-016 (sin librerías)

Materials and where to buy them

Connections

A great advantage of the XY-K485 converter is that it does not require DE / and RE to receive reception, only with Rx and Tx do we carry out RS485 communication.

XY-K485 converter RS485

Next Recommended Tutorial: TTL to RS485 Converter.

 

Meter Connections

Meter PZEM-016 Slave Modbus RTU (RS485)

Recommended Tutorial: Initial Review PeaceFair PZEM-016 Modbus RTU RS485 Meter

Modbus Registers of the Meter PZEM-016

It has 10 Reading registers and 2 Writing registers.

  • The 10 reading registers correspond to Measurements (Voltage, Current, Power, Accumulated Power, Frequency and Power Factor), it has an alarm record for maximum instantaneous power limit (Watts).
  • The 2 write registers correspond to value in alarm (Watts) and the Slave address.

Note: Although Accumulated power can be reset, the specific record is not mentioned in the documentation.

Mounting

 

Modbus RTU: Master frame send

ESP32 sends master frame, requesting registers from meter PZEM-016 by RS485.

Documentation:

Real Implementation:

Modbus RTU: Slave Frame Reception

The PZEM-016 meter Modbus RTU slave (RS485), meter response.

Documentation:

Real Implementation:

Test 1: ESP32 Code Bridge Mode USB to Serial

A code was created to create a USB to Serial bridge using ESP32.

 

Test 2: Modbus RTU simulator

The ESP32 bridged mode addresses frames between Serial to Serial2, operating equivalent to an FTDI.

The modbus simulator allows monitoring the sending and receiving frames, allowing greater ease.

Test 3: PeaceFair PZEM-014 Master Software

The ESP32 in bridge mode also allows the PZEM-016 to communicate with the PeaceFair Software.

Other Recommended

Test Arduino IDE

A frame was created to send the ESP32 in Modbus Master mode and Slave reception processing, this code performs the following.

  • Sent frame Master.
  • Slave Frame Reception.
  • Conversion of Byte to Int.
  • Standardization of Records to Measurements.

This example only performs reading of registers, in future tutorials we will explain how writing modbus registers works.

We simply send the request frame for 10 Modbus registers and the PZEM-016 Meter responds with 20 Bytes equivalent to 10 registers.

Note: Important, this implementation in Arduino IDE basic, we do not use the validation methods of CRC or Checksum.

Note: Download code and examples at the end of the article.

Serial Debugging

To validate the correct ESP32 and PZEM-016 communication, the serial port for debugging HEX frames was enabled.

 

Videos: Basic Test Modbus RTU

  • Explanatory and detailed video.

Conclusions

As initially mentioned this tutorial is simply a brief introduction or explanation of how the Modbus RTU protocols specifically work on RS485.

This is a basic or didactic implementation without taking into account the validation methods of the CRC or Checksum protocol, since it is a test with no industrial purpose.

The PZEM-016 meter has 5VDC to power devices, in this case do not use it, but it should allow powering our ESP32 and the XY-K485.

Although we are using an ESP32 this time we will not use FreeRTOS, to simplify the tutorial, in future tests we will implement it.

This example is the start of another variety of tests, with some modifications we could add MQTT or HTTP / HTTPS communication with IoT platforms.

 

Advantage

Regarding hardware specifically, the XY-K485 converter (TTL to RS485), does not require enablement for Send and Receive (RE & DE), facilitates implementation in other microcontrollers, without flow control.

Downloads

Example: ESP32 Serial / Usb Bridge

823 Downloads

Example: ESP32 Modbus Basic Master

1366 Downloads

Recommended

Leave a Reply