Introduction
Modbus RTU is a widely used communication protocol in industrial automation, known for its simplicity, reliability, and ability to operate over long distances. It facilitates communication between devices such as Programmable Logic Controllers (PLCs), sensors, and actuators. In this blog post, we will explore how to implement Modbus RTU communication using Industrial Shields Arduino and ESP32 based PLCs, showcasing both master and slave configurations.
Whether you're looking to establish communication between two PLCs or connect your PLC to other Modbus compatible devices, this tutorial provides a comprehensive guide to both hardware and software implementation. You'll learn about the necessary components, how to wire and configure the hardware, and how to program Modbus functionality using the Arduino IDE and the Modbus library.
By the end of this tutorial, you'll be able to set up a robust Modbus RTU communication system with ease, leveraging Industrial Shields versatile PLCs for your automation projects. For more in-depth information about Modbus RTU, check out our Modbus Course.
Requirements
Hardware
- 1 x Arduino / ESP32 based PLC (Modbus Master)
- 1 x Arduino / ESP32 based PLC or device with Modbus (Modbus Slave)
- Power supply (12 - 24V)
- B or micro B type cable, to program the Arduino / ESP32 based PLC
- A twisted pair cables, for RS485
Software
Implementation
Hardware setup
Firstly, power the PLC between 12 and 24V, wire the cables through RS-485 and set up the corresponding switches of your PLC. You can check this RS-485 tutorial, to make sure all your configurations are good.
Software implementation
Master
In order to program the Modbus Master PLC, follow these steps:
1. Navigate to File > Examples > Modbus or File > Examples > Tools40 > Modbus, and select one of the Modbus RTU examples. These examples demonstrate various Modbus functionalities.
2. Select your Board and Model according to your PLC:
- For M-Duino PLC Family: Tools > Board > Industrial Shields AVR Boards > M-Duino [Extension] family and Tools > Model > M-Duino [Module]
- For Ardbox PLC Family: Tools > Board > Industrial Shields AVR Boards > Ardbox [Extension] family and Tools > Model > Ardbox [Module] HF+ w/HW RS-485
- For ESP32 PLC Family: Tools > Board > Industrial Shields ESP32 Boards > ESP32 PLC Family and Tools > Model > ESP32 PLC [Module]
- For ESP32 PLC 14: Tools > Board > Industrial Shields ESP32 Boards > 14 IOS PLC Family
3. Make sure that the following configurations are the same as in the slave device:
- Baudrate: The communication speed (e.g., 9600, 19200).
- Duplex mode: Either half-duplex or full-duplex, depending on your setup.
- Serial communication settings: Typically SERIAL_8E1 (8 data bits, even parity, 1 stop bit).
4. Finally, upload the sketch at Sketch > Upload.
API Reference
Modbus provides multiple function codes (FC) to read and write the different coils and registers. Our library provides some functions to send messages using the different function codes. These are the functions available:
Description:
Reads the contents of a specified number of coils from a slave device (FC=01).
Syntax:
readCoils(slave, addr, quantity);
Parameters:
- slave: The address of the slave device.
- addr: The starting address of the coils.
- quantity: The number of coils to read.
Returns:
Returns true if the operation was successful, false otherwise.
Description:
Reads the contents of a specified number of discrete inputs from a slave device (FC=02).
Syntax:
readDiscreteInputs(slave, addr, quantity);
Parameters:
- slave: The address of the slave device.
- addr: The starting address of the discrete inputs.
- quantity: The number of discrete inputs to read.
Returns:
Returns true if the operation was successful, false otherwise.
Description:
Reads the contents of a specified number of holding registers from a slave device (FC=03).
Syntax:
readHoldingRegisters(slave, addr, quantity);
Parameters:
- slave: The address of the slave device.
- addr: The starting address of the holding registers.
- quantity: The number of holding registers to read.
Returns:
Returns true if the operation was successful, false otherwise.
Description:
Reads the contents of a specified number of input registers from a slave device (FC=04).
Syntax:
readInputRegisters(slave, addr, quantity);
Parameters:
- slave: The address of the slave device.
- addr: The starting address of the input registers.
- quantity: The number of input registers to read.
Returns:
Returns true if the operation was successful, false otherwise.
Description:
Writes a single coil to either 0 or 1 in a slave device (FC=05).
Syntax:
writeSingleCoil(slave, addr, value);
Parameters:
- slave: The address of the slave device.
- addr: The address of the coil.
- value: The value to write to the coil.
Returns:
Returns true if the operation was successful, false otherwise.
Description:
Writes a value to a single holding register in a slave device (FC=06).
Syntax:
writeSingleRegister(slave, addr, value);
Parameters:
- slave: The address of the slave device.
- addr: The address of the holding register.
- value: The value to write to the holding register.
Returns:
Returns true if the operation was successful, false otherwise.
Description:
Writes values to a sequence of coils in a slave device (FC=15).
Syntax:
writeMultipleCoils(slave, addr, values, quantity);
Parameters:
- slave: The address of the slave device.
- addr: The starting address of the coils.
- values: The values to write to the coils.
- quantity: The number of coils to write.
Returns:
Returns true if the operation was successful, false otherwise.
Description:
Writes values to a sequence of holding registers in a slave device (FC=16).
Syntax:
writeMultipleRegisters(slave, addr, values, quantity);
Parameters:
- slave: The address of the slave device.
- addr: The starting address of the holding registers.
- values: The values to write to the holding registers.
- quantity: The number of holding registers to write.
Returns:
Returns true if the operation was successful, false otherwise.
Slave
In case of using a PLC for the Modbus Slave device, follow these steps:
1. Navigate to File > Examples > Modbus or File > Examples > Tools40 > Modbus, and select the Modbus RTU Slave example code.
2. Select your Board and Model according to your PLC as with the master.
3. Make sure that the baudrate, duplex mode and serial communication settings are the same as in the master device.
4. The Modbus RTU Slave code is an example for an M-Duino 21+, this means that the pin mapping will need to be modified in case of using another PLC. Check our Modbus Course for further information.
4. Finally, upload the sketch at Sketch > Upload.
Modbus RTU with Arduino and ESP32 based PLCs