Modbus RTU Master Slave I/O

Introduction

This tutorial guides you through setting up Modbus RTU communication between a master and a slave device using two NEXTuino devices. The master device reads digital and analog inputs from the slave and sends data to control its outputs, while the slave responds with data controlling its own outputs based on commands received from the master.

Note

The NEXTuino BSP, for MAXI and MEGA, inside pins_arduino.h contains the following definitions:

// ArduinoRS485 library support
#define RS485_SERIAL_PORT           Serial3
#define RS485_DEFAULT_TX_PIN        14
#define CUSTOM_RS485_DEFAULT_DE_PIN 75
#define CUSTOM_RS485_DEFAULT_RE_PIN 76

These allow the use of the default instance of the RS485 library:

RS485Class RS485(RS485_SERIAL_PORT, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);

Which is also the default RS485 interface for ModbusRTUServer and ModbusRTUClient — the default instances for RTU in the ArduinoModbus library.

Prerequisites

  • Installed Arduino IDE
  • NEXTuino Mega or Maxi PLC board for both master and slave devices
  • Basic understanding of Modbus RTU communication and pin setup
  • Connect two NEXTuino boards using the RS485 pins

Example Code

Download the example Modbus RTU master and slave code from the GitHub repository:

Step-by-Step Guide

Step 1: Setup the Arduino Environment

  1. Connect your NEXTuino boards (both master and slave) to your computer.
  2. Open the Arduino IDE.
  3. Ensure the appropriate NEXTuino Mega board and port are selected in the Tools menu.
  4. Include the necessary libraries:
    • ArduinoRS485 library for Modbus RTU communication
    • ArduinoModbus library for handling Modbus RTU protocol
    • NEXTuino library to handle NEXTuino-specific pins

Step 2: Initialize Modbus RTU Communication and Serial Debugging

  1. In the setup() function of both master and slave:
    • Initialize Modbus RTU communication by calling ModbusRTUClient.begin(9600) for the master and ModbusRTUServer.begin(1, 9600) for the slave.
    • Initialize serial communication for debugging using Serial.begin(9600).

Step 3: Configure Digital and Analog Pins

Master Configuration:

  • Digital Inputs: NEXTuino_A0 to NEXTuino_A10 as inputs
  • Relay Outputs: NEXTuino_R0 to NEXTuino_R10 as outputs
  • Analog Inputs: NEXTuino_A11 to NEXTuino_A15 as inputs
  • PWM Outputs: NEXTuino_D0 to NEXTuino_D4 as outputs

Slave Configuration: Same digital input-output configuration as the master.

Step 4: Sending and Receiving Data

Master Code (loop function)

  1. Read digital inputs (NEXTuino_A0 to NEXTuino_A10)
  2. Write input states to the slave’s coils using ModbusRTUClient.coilWrite()
  3. Read analog inputs, split 10-bit values, write to slave’s holding registers
  4. Read slave’s discrete inputs and input registers to update master outputs

Slave Code (loop function)

  1. Use ModbusRTUServer.poll() to check for incoming Modbus requests
  2. For digital outputs: read coil values from master, update digital output pins
  3. For analog outputs: read holding register values, set PWM outputs

Step 5: Synchronizing Outputs

  • Master’s digital outputs (NEXTuino_R0 to NEXTuino_R10) are updated based on slave’s discrete inputs
  • Master’s analog outputs (NEXTuino_D0 to NEXTuino_D4) are updated based on slave’s input registers

Step 6: Debugging and Monitoring

  1. Add serial print statements to monitor Modbus communication
  2. Ensure the Serial Monitor is open at the correct baud rate (9600 baud)
  3. Use LEDs or external devices to visualize output state changes

Troubleshooting

  • Modbus Communication Issues: Verify the slave ID and Modbus address in both master and slave codes
  • Serial Debugging: Ensure Serial Monitor is open at 9600 baud
  • Wiring: Double-check RS485 connections — ensure A+ and B- terminals are properly connected
  • Pin Setup: Verify correct pins are configured as inputs/outputs on both devices

Conclusion

You have successfully established Modbus RTU communication between a NEXTuino master and slave. This tutorial can also serve as a base for extending the I/Os of a NEXTuino with another NEXTuino using Modbus RTU and RS485.