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
- Connect your NEXTuino boards (both master and slave) to your computer.
- Open the Arduino IDE.
- Ensure the appropriate NEXTuino Mega board and port are selected in the Tools menu.
- 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
- In the setup() function of both master and slave:
- Initialize Modbus RTU communication by calling
ModbusRTUClient.begin(9600)for the master andModbusRTUServer.begin(1, 9600)for the slave. - Initialize serial communication for debugging using
Serial.begin(9600).
- Initialize Modbus RTU communication by calling
Step 3: Configure Digital and Analog Pins
Master Configuration:
- Digital Inputs:
NEXTuino_A0toNEXTuino_A10as inputs - Relay Outputs:
NEXTuino_R0toNEXTuino_R10as outputs - Analog Inputs:
NEXTuino_A11toNEXTuino_A15as inputs - PWM Outputs:
NEXTuino_D0toNEXTuino_D4as outputs
Slave Configuration: Same digital input-output configuration as the master.
Step 4: Sending and Receiving Data
Master Code (loop function)
- Read digital inputs (
NEXTuino_A0toNEXTuino_A10) - Write input states to the slave’s coils using
ModbusRTUClient.coilWrite() - Read analog inputs, split 10-bit values, write to slave’s holding registers
- Read slave’s discrete inputs and input registers to update master outputs
Slave Code (loop function)
- Use
ModbusRTUServer.poll()to check for incoming Modbus requests - For digital outputs: read coil values from master, update digital output pins
- For analog outputs: read holding register values, set PWM outputs
Step 5: Synchronizing Outputs
- Master’s digital outputs (
NEXTuino_R0toNEXTuino_R10) are updated based on slave’s discrete inputs - Master’s analog outputs (
NEXTuino_D0toNEXTuino_D4) are updated based on slave’s input registers
Step 6: Debugging and Monitoring
- Add serial print statements to monitor Modbus communication
- Ensure the Serial Monitor is open at the correct baud rate (9600 baud)
- 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.