Open links in new tab
  1. Modbus TCP is a protocol used for communication between devices over a TCP/IP network. It is widely used in industrial automation systems. In this guide, we will demonstrate how to set up an Arduino as a Modbus TCP slave using the Ethernet shield.

    Setting Up the Arduino

    To set up the Arduino as a Modbus TCP slave, you will need the following libraries:

    • SPI.h

    • Ethernet.h

    • ArduinoRS485.h

    • ArduinoModbus.h

    Here is a sample code to get you started:

    #include <SPI.h>
    #include <Ethernet.h>
    #include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
    #include <ArduinoModbus.h>

    // Enter a MAC address and IP address for your controller below
    // The IP address will be dependent on your local network
    // gateway and subnet are optional:
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip(192, 168, 1, 177);
    IPAddress myDns(192, 168, 1, 1);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 0, 0);

    EthernetServer server(502);
    ModbusTCPServer modbusTCPServer;

    void setup() {
    // initialize the ethernet device
    Ethernet.begin(mac, ip, myDns, gateway, subnet);

    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
    }

    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
    while (true) {
    delay(1); // do nothing, no point running without Ethernet hardware
    }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
    }

    // start listening for clients
    server.begin();

    Serial.print("Modbus server address:");
    Serial.println(Ethernet.localIP());

    // start the Modbus TCP server
    if (!modbusTCPServer.begin()) {
    Serial.println("Failed to start Modbus TCP Server!");
    while (1);
    }

    // configure the LED
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);

    // configure a single coil at address 0x00
    modbusTCPServer.configureCoils(0x00, 1);
    }

    void loop() {
    // wait for a new client:
    EthernetClient client = server.available();

    // when the client sends the first byte, say hello:
    if (client) {
    // a new client connected
    Serial.println("new client");

    // let the Modbus TCP accept the connection
    modbusTCPServer.accept(client);

    while (client.connected()) {
    // poll for Modbus TCP requests, while client connected
    modbusTCPServer.poll();

    // update the LED
    updateLED();
    }

    Serial.println("client disconnected");
    }
    }

    void updateLED() {
    // read the current value of the coil
    int coilValue = modbusTCPServer.coilRead(0x00);

    if (coilValue) {
    // coil value set, turn LED on
    digitalWrite(LED_BUILTIN, HIGH);
    } else {
    // coil value clear, turn LED off
    digitalWrite(LED_BUILTIN, LOW);
    }
    }
    Copied!
  1. TCP Examples | arduino-libraries/ArduinoModbus | DeepWiki

    Apr 30, 2025 · This document explains the Modbus TCP examples provided with the ArduinoModbus library. These examples demonstrate how to implement Modbus TCP client and server functionality …

  2. Arduino Modbus TCP Slave (server): IDE example

    May 9, 2019 · That's a simple combination of a Ethernet server sketch with the WiFiModbusServerLED sketch example of the linked library (not tested):

  3. Arduino - Modbus | Arduino Tutorial

    As described earlier, An Arduino can communicate with software/app, HMI device, or another Arduino via Modbus protocol. This tutorial takes communication between two Arduino as an example of Modbus communication.

  4. Arduino Ethernet Modbus Server Example · GitHub

    Mar 6, 2025 · Arduino Ethernet Modbus Server Example. GitHub Gist: instantly share code, notes, and snippets.

  5. Modbus TCP example for INDUSTRIAL uses

    Mar 25, 2020 · 👇 ESP32 and Arduino Modbus TCP example with Ethernet shield assembled to a PLC Arduino for Industrial application. Check the Modbus TCP master client library.

  6. How to convert Arduino into a Modbus device

    Sep 26, 2023 · In this article, we’ll turn Arduino into a Modbus device using Modbus ASCII/RTU and Modbus TCP. To do so, we’ll set-up communication between two Arduino boards on Modbus.

  7. People also ask
  8. Modbus - Arduino Tutorial - Circuits DIY

    Apr 14, 2023 · Interfacing two Arduino UNO microcontrollers with one another using the Modbus communication protocol is a fascinating project that allows you to control an LED light remotely. This project involves setting up a serial connection …

  9. Arduino® & Modbus Protocol | Arduino Documentation

    Dec 4, 2023 · This article contains information about the Modbus serial communication protocol and how it can be used with Arduino hardware. The different elements are highlighted, compatible libraries and boards are shown …

  10. ModbusTCPSlave/examples/ModbusTCPSlaveEthernetExample

    Feb 8, 2025 · On the ModbusRTU Test Shield, the UART switch was set to HW to avid interference with the SPI interface. This example also requires a Modbus client on the same network as the …

  11. Coding Modbus TCP/IP for Arduino - Node-RED, MQTT, …

    May 17, 2024 · Through straightforward explanations and hands-on examples, readers are led through the process of establishing Modbus communication with Arduino, enabling seamless data exchange between devices over a network.