Main Content

MODBUS RS485 Communication Between Master and Slave Devices Using Arduino Board

This example shows how to use the Simulink® Support Package for Arduino® Hardware to implement a MODBUS® RS485 asynchronous, serial communication between MODBUS master and slave devices. It also shows the four modes of operation: Master Read, Master Write, Slave Read, and Slave Write.

Supported Arduino Boards:

  • Arduino MKR Zero

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino MKR 1000

  • Arduino MKR WIFI 1010

  • Arduino Leonardo

  • Arduino Micro

  • Arduino Nano 3.0

  • Arduino Nano 33 IoT

  • Arduino Uno

  • Arduino Robot Control board

  • Arduino Robot Motor board

Introduction

Simplicity, efficient communication, and streamlined and fast data communication make MODBUS RTU RS485 one of the most widely used serial communication protocols in electronics and instrumentation industries. In this client/server architecture, the master device acts as a client and the slave device acts as a server. Each device on the RS485 network is referenced by a unique 8-bit address or identifier.

This example shows the capabilities of the MODBUS RS485 protocol using two Arduino® boards acting as master and slave, with the Arduino MODBUS Master model deployed on the former and the Arduino MODBUS Slave model deployed on the latter.

The example also deals with utilizing different modes of operation for the master and slave device.

  • Master Read: Master device reads data from slave device register(s) over RS485 network

  • Master Write: Master device writes data to slave device register(s) over RS485 network

  • Slave Read: Slave device reads data from the slave device registers over RS485 network

  • Slave Write: Slave device writes data to the slave device registers over RS485 network

This table categories the different slave device registers referenced by the MODBUS master and slave devices.

   Register Type   |   Register Size   |   Allowed Master Operation on Register
-----------------------------------------------------------------------------------
 Coil              |       1-bit       |            Read and Write
 Discrete Input    |       1-bit       |            Read
 Holding Register  |       16-bit      |            Read and Write
 Input Register    |       16-bit      |            Read

Prerequisites

We recommend that you complete the Getting Started with Arduino® Hardware and Communicating with Arduino® Hardware examples.

Required Hardware

  • Two Arduino boards

This example uses one Arduino MKR 1000 board as a master and another Arduino MKR 1000 board as a slave. You can use any of the Arduino boards from the list of suggested boards.

  • Two micro USB cables

  • Two RS485 shields

Dependencies

Implementing this example requires you to successfully deploy these Simulink® models on each of the supported Arduino boards:

   Simulink Model Name    |          Deployed Hardware         |            Purpose
------------------------------------------------------------------------------------------------
1. arduino_modbus_slave    |   First of the two Arduino board   |   Behaves as a slave device
2. arduino_modbus_master   |   Second of the two Arduino board  |   Behaves as a master device

Task 1: Connection Setup

The MODBUS master and slave devices must be connected on the same RS485 network for successful communication.

Task 2: Configure Master Model and Calibrate Parameters

This support package provides a preconfigured model for the MODBUS master.

To open this model, run this command at the MATLAB® Command Window:

open_system('arduino_modbus_master')

The Simulink model for the master can be divided into two parts, Write to Slave and Read from Slave, based on the type of operation the master can perform on the slave device registers. The default values for the Simulink master model are preconfigured in this example depending on the slave register size and the allowed master operations.

MODBUS Master Configuration for Write to Slave Mode of Operation

The master write operation is valid on coil and holding registers. This example uses two MODBUS RS485 Master Write blocks to implement the write operation on the coil registers and the holding registers, respectively.

To perform the 1-bit write operation on the coil registers, change the position of the slider switches. The On position depicts that data 1 is written on these registers, whereas the Off position depicts that data 0 is written on these registers. Two slider switches are used in this model, whose output is multiplexed and fed as an input to one of the MODBUS RS485 Master Write blocks.

You can configure the following parameters to write data from the slider switches to the coil registers:

  1. Enter the unique 8-bit slave address of the identifier in the Slave address parameter. In this model, it is configured as 1.

  2. Select the Write Multiple Coils option in the Function parameter.

  3. To notify the master to write data to two coil registers, specify the address of the first coil register in the Coil Address parameter. The default value is 0.

  4. Enter 2 in the Number of Coils parameter.

Similarly, to perform the 16-bit master write operation on the holding registers, this example uses a counter that counts values from 0 to 255. The output from the free-running counter is fed as an input to the second MODBUS RS485 Master Write block.

You can configure the following parameters to write data from the free-running counter to the holding registers:

  1. Enter the unique 8-bit slave address of the identifier in the Slave address parameter. In this model, it is configured as 1.

  2. Select the Write Multiple Holding registers option in the Function parameter.

  3. To notify the master to write data to the holding registers, specify the address of the first coil register in the Holding Register Address parameter. The default value is 0.

  4. Enter 1 in the Number of Holding registers parameter.

MODBUS Master Configuration for Read from Slave Mode of Operation

The master read operation is valid on all the slave device registers (coil, holding, discrete input, and input). Two MODBUS RS485 Master Read blocks are used to implement the read operation on input and discrete input registers, respectively.

A 1-bit pulse generator is used to write data to the discrete input registers. The data read from the discrete input registers is available on the data port of the MODBUS Master Read block for the discrete input register. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A lamp indicator is used to signify the 1-bit read from the discrete input register.

You can configure the following parameters to read 1-bit pulse generator data from the discrete input register:

  1. Enter the unique 8-bit slave address of the identifier in the Slave address parameter. In this model, it is configured as 1. Ensure to configure the same Slave address as used in the MODBUS RS485 Slave Write block for the discrete input register.

  2. Select the Read Discrete input option in the Function parameter.

  3. To notify the master to read data from one discrete input register, specify its address in the Discrete Input Address parameter. Ensure to configure the same discrete input register address as used in the MODBUS RS485 Slave Write block for the discrete input register. The default value is 0.

  4. Set the Sample Time parameter.

Similarly, this example uses a free-running counter to write data to the input register. The data read from the input register is available on the data port of the MODBUS RS485 Master Read block for the input register. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A Display block is used to show the 16-bit data read from the input register.

You can configure the following parameters to read the counter data from the input register:

  1. Enter the unique 8-bit slave address of the identifier in the Slave address parameter. In this model, it is configured as 1. Ensure to configure the same Slave address as used in the MODBUS RS485 Slave Write block for the input register.

  2. Select the Read Input register option in the Function parameter.

  3. To notify the master to read data from one input register, specify its address in the Input Register Address parameter. Ensure to configure the same input register address as used in the MODBUS RS485 Slave Write block for the input register. The default value is 0.

  4. Set the Sample Time parameter.

Task 3: Configure Slave Model and Calibrate Parameters

This support package provides a preconfigured model for the MODBUS slave.

To open this model, run this command at the MATLAB® Command Window:

open_system('arduino_modbus_slave')

The Simulink model for the slave can be divided into two parts, Write to Master and Read from Master, based on the type of operation the slave device can perform on its registers. The default values for the Simulink slave model are preconfigured in this example.

MODBUS Slave Configuration for Write to Master Mode of Operation

This example uses two MODBUS RS485 Slave Write blocks to implement the write operation on the input register and the discrete input register, respectively.

To perform the 16-bit slave write operation on the input registers, this example uses a counter that counts values from 0 to 255. The output from the free-running counter is fed as an input to the MODBUS RS485 Slave Write block for the input register.

You can configure the following parameters to write data from the free-running counter to the input registers:

  1. Select the Write Input register option in the Function parameter.

  2. To notify the master to read data from one input register, specify its address in the Input Register Address parameter. Ensure to configure the same input register address as used in the MDOBUS RS485 Master Read block for the input register. The default value is 0.

Similarly, this example uses a 1-bit pulse generator to write data to the discrete input register.

You can configure the following parameters to write 1-bit data from the pulse generator to the discrete input register:

  1. Select the Write Discrete Input option in the Function parameter.

  2. To notify the master to read data from one input register, specify its address in the Discrete Input Address parameter. Ensure to configure the same discrete input register address as used in the MDOBUS RS485 Master Read block for the input register. The default value is 0.

MODBUS Slave Configuration for Read from Master Mode of Operation

This example uses two MODBUS RS485 Slave Read blocks to implement the read operation on the coil and holding registers, respectively.

The slave device reads the data written by the master device using two slider switches. This 1-bit data read from the coil registers is available on the data port of the MODBUS RS485 Slave Read block for the coil registers. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A lamp indicator is used to signify the 1-bit data read from the coil registers.

You can configure the following parameters to read 1-bit slider switch data from the coil registers:

  1. Select the Read Coil option in the Function parameter.

  2. To notify the slave to read data from the coil registers, specify its address in the Coil Address parameter. Ensure to configure the same coil register address as used in the MDOBUS RS485 Master Write block for the coil register. The default value is 0.

  3. Set the Sample Time parameter.

Similarly, the slave device reads the free-running counter data fed as an input to the master device. This 16-bit data read from the holding register is available on the data port of the MODBUS RS485 Slave Read block for the holding register. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A Display block is used to show the 16-bit data read from the holding register.

You can configure the following parameters to read the 16-bit counter data from the holding register:

  1. Select the Read Holding register option in the Function parameter.

  2. To notify the slave to read data from the holding registers, specify its address in the Holding Register Address parameter. Ensure to configure the same holding register address as used in the MDOBUS RS485 Master Write block for the holding register. The default value is 0.

  3. Set the Sample Time parameter.

Task 4: Run Model on Master and Slave Devices

1. Connect the Arduino boards acting as the slave device and the master device to the host computer.

2. On the Hardware tab of the Arduino MODBUS Slave Simulink model, click Monitor & Tune.

3. On the Hardware tab of the Arduino MODBUS Slave Simulink model, click Monitor & Tune.

4. Coil register:

  • MODBUS RS485 Master Read block: Change the position of the slider switches to write data to the coil registers.

  • MODBUS RS485 Slave Read block: Verify the status of the read operation on the coil registers. Observe the lamp displaying the corresponding data obtained from the coil registers. Verify if the lamp status exhibits the correct data written on the coil registers.

5. Holding register:

  • MODBUS RS485 Master Write block: The counter starts writing data ranging from 0 to 255 to the holding register.

  • MODBUS RS485 Slave Read block: Check the status of the read operation on the holding register. Observe the counter value corresponding to the data obtained from the holding register and verify if the same value is displayed on the Display block.

6. Input register:

  • MODBUS RS485 Slave Write block: The counter starts counting from the value that ranges from 0 to 255.

  • MODBUS RS485 Master Read block: Check the status of the read operation on the input register. Observe the counter value corresponding to the data obtained from the input register and verify if the same value is displayed on the Display block.

7. Discrete Input register:

  • MODBUS RS485 Slave Write block: The 1-bit pulse generator starts toggling between 0 and 1.

  • MODBUS RS485 Master Read block: Check the status of the read operation on the discrete input register. Observe the lamp displaying the corresponding data obtained from the discrete input register. Verify if the lamp status exhibits the correct data written on the discrete input register.

Other Things to Try

  • Implement the MODBUS master and slave models to read and write from and to multiple slave device registers for all the register types.

  • Use MODBUS slave devices such as temperature sensors, humidity sensors, and so on and communicate real-time data to the Arduino board acting as the master device.

  • Implement a monitoring system using PID controller as the slave device and the Arduino board as the master device.