Introducción
Modbus TCP is a robust and widely used protocol for communication over Ethernet in industrial automation systems. It allows seamless integration between devices, providing reliable data exchange and control. This guide demonstrates how to implement Modbus TCP communication with Arduino and ESP32-based PLCs as both master and slave devices.
It is important to note that the Ardbox does not provide Ethernet communication and, therefore, does not support Modbus TCP. However, other Arduino and ESP32-based PLC families like M-Duino and ESP32 14 PLC are fully equipped for Ethernet communication, enabling Modbus TCP functionality.
In this tutorial, we cover the necessary hardware and software setups, including how to configure the devices and write the Modbus TCP master and slave programs using the Arduino IDE and the Industrial Shields Boards. By following this guide, you'll gain a clear understanding of Modbus TCP communication and its implementation in industrial PLC systems. For more in-depth information about Modbus RTU, check out our
For more in-depth information about Modbus TCP, check out our Modbus Course.
Requisitos
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
- Ethernet cable
Software
Implementación
Configuración del hardware
Firstly, power the PLC between 12 and 24V and connect the devices with an Ethernet cable.
Implementación del software
Maestro
Para programar el PLC Modbus Maestro, sigue estos pasos:
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. Selecciona tu placa y modelo según tu PLC:
- For M-Duino PLC Family: Tools > Board > Industrial Shields AVR Boards > M-Duino [Extension] family and Tools > Model > M-Duino [Module]
- 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. Configura los parámetros para la conexión Ethernet:
- MAC: Specify a MAC address for the master device.
- IP: Assign a static IP address to the master device.
3. Make sure that the following configurations are the same as in the slave device:
- Slave IP: The static IP of the slave device.
- Slave port: Default is often port 502, the standard port for Modbus TCP.
4. Finally, upload the sketch at Sketch > Upload.
Referencia de la API
Modbus proporciona múltiples códigos de función (FC) para leer y escribir los diferentes coils y registros. Nuestra librería ofrece funciones para enviar mensajes usando los diferentes códigos de función. Estas son las funciones disponibles:
Description:
Lee el contenido de un número específico de coils desde un dispositivo esclavo (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:
Lee el contenido de un número específico de entradas discretas desde un dispositivo esclavo (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:
Lee el contenido de un número específico de holding registers desde un dispositivo esclavo (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:
Lee el contenido de un número específico de input registers desde un dispositivo esclavo (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:
Escribe un único coil con valor 0 o 1 en un dispositivo esclavo (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:
Escribe un valor en un único holding register en un dispositivo esclavo (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:
Escribe valores en una secuencia de coils en un dispositivo esclavo (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:
Escribe valores en una secuencia de holding registers en un dispositivo esclavo (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.
Esclavo
En caso de usar un PLC como dispositivo Modbus Esclavo, sigue estos pasos:
1. Navigate to File > Examples > Modbus or File > Examples > Tools40 > Modbus, and select the Modbus RTU examples code.
2. Selecciona tu placa y modelo según tu PLC, igual que con el maestro.
3. Asegúrate de que la velocidad de transmisión, el modo dúplex y los ajustes de comunicación serie sean los mismos que en el dispositivo maestro.
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 TCP con PLCs basados en Arduino y ESP32