Arduino Master & ESP8266 Slave Modbus RTU (TTL)

In many occasions we have communicated our ESP8266 with Arduino via AT commands, but there are certain cases in which it is required to communicate both devices and send and/or receive large numbers of registers, almost in real time, although there are protocols such as I2c, and SPI with Libraries already implemented in arduino, we will use Modbus RTU, par excellence the communication protocol of field for devices of monitoring and control since it is easy its implementation, is safe, fast and approves of errors.

For our test we have an board  Arduino MEGA 2560 R3 and Module ESP8266 12E, communicated via modbus RTU connected via direct 3-wire, TTL voltages 0 to 5v.

trialcommand.com

 

Arduino Configuration

In this case, it has been configured as a modbus master, using the second serial port (Serial1) of our board, since it has 4 ports available, modifications were made to the master modbus library based on previous tests.

Configuration ESP8266

It has been configured as a slave modbus, in this case the modbus protocol works through an extra serial port generated with the SoftwareSerial library, which allows to create a serial port generated by software with interruptions, it is recommended not to use it at very high speeds in this Case at 9600 bauds, for more information we recommend these previous references.





Tests

The test is simple, we will use 2 registers in this case to verify the reading and writing of both devices since in certain tests they show the devices or just reading or just typing.

Arduino Master & ESP8266 Slave Modbus RTU (TTL 0-5v )

Arduino IDE  Code – Arduino

The arduino has a N/A button connected between pin 7 and GND configured as a pullup input, arduino read the status of pin 7 and send the value in the Holding Register [4].

Make a constant reading of the Holding Register [5] and print the value in the Serial terminal.

Note: Download of libraries at the end of the Post.





Arduino IDE Code – ESP8266

The ESP8266 will read the Holding Register [4] and activate the pin 14 (D5) as an output and in turn has a led diode.

Send in the Holding Register [5] an integer with a random value from 1 to 9999.

Note: Note that between the two devices there is a shift of 1 position between the holding registers, this characteristic is common in modbus.

Note: Download of libraries at the end of the Post.

Connections

Since the communication is TTL (0-5v), the serial1 of arduino mega 2560 pins (Tx1 -18) (Rx1 – 19) is connected to the port of ESP8266 created in softwareserial pins D7 (13), D8 (15) RX , TX, to cross RX -Tx between both, following individual descriptions.

ESP8266 12E  NodeMCU

trialcommand.com

Arduino MEGA 2560 R3

trialcommand.com

Recommendations

We recommend a speed of 9600 bauds for a good performance, since the serial software library is not Uart own the ESP8266, at high speeds there could be bit errors in the frame, since it works with interruptions.

Modifications have been made to the master and slave libraries for this specific test, the modifications are especially in the assignments and declaration of the serial ports.

trialcommand.com

Conclusions

  • We consider this an excellent solution given that a common and robust industrial protocol is used that would easily apply to communicate with an HMI, PLC, Meter, PAC, SCADA and / or other class of industrial controllers.
  • A great benefit is the modbus is the facility of transmitting a large number of registers, it should be noted that it allows for 16-bit integer registers.

References

Donwloads Libraries

Leave a Reply