Modbus TCP with Arduino and ESP32 based PLCs

Implement Modbus TCP master and slave with M-Duino and ESP32 PLCs — Ethernet setup, Arduino IDE configuration, and full API reference
June 19, 2026 by
Modbus TCP with Arduino and ESP32 based PLCs
Boot & Work Corp. S.L, Martí Schnabel

Introduction

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 TCP, check out our Modbus Course.

Requirements

Hardware

Software

Implementation

Hardware setup

Firstly, power the PLC between 12 and 24V and connect the devices with an Ethernet cable.

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 TCP 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 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. Set up the configurations to configure the Ethernet:

  • MAC: Specify a MAC address for the master device.
  • IP: Assign a static IP address to the master device.

And 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.

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 TCP 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 TCP 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.

​Search in our Blog

Modbus TCP with Arduino and ESP32 based PLCs
Boot & Work Corp. S.L, Martí Schnabel June 19, 2026
Share this post
Tags

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

PLC Comparison