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.
Materials and where to buy them
- Module ESP32 (Socket tusistemita.com)
- Converter XY-K485 Isolated (TTL to RS485)
- Power Meter PZEM-016 Modbus Slave.
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 PZEM-016 Slave Modbus RTU (RS485)
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.
Modbus RTU: Master frame send
Modbus RTU: Slave Frame Reception
The PZEM-016 meter Modbus RTU slave (RS485), meter response.
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
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.
Videos: Basic Test Modbus RTU
- Explanatory and detailed video.
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.
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.
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.