A simple and flexible Modbus RTU slave library for Arduino.
The MBModbusRTUSlave
library allows Arduino devices to function as Modbus RTU slaves over a serial connection. It supports reading and writing registers via Modbus function codes 0x03
(Read Holding Registers) and 0x06
(Write Single Register), with provisions for LED control based on a designated register value. The library is designed to be customizable, with configurable slave address, LED pin, LED register index, and baud rate.
- Supports Modbus RTU protocol (function codes
0x03
and0x06
). - Configurable slave address, LED pin, and LED register index.
- Flexible baud rate setting via a variable.
- LED control tied to a specific register value (turns ON when the register equals 1).
- Lightweight and easy to integrate into Arduino projects.
- 1.0.0 (Released: March 07, 2025)
- 1.1.0 (Released: April 08, 2025)
- S.Mersin (electrocoder) [email protected] (Assisted by Grok)
- MIT License (See LICENSE section for details)
-
Download the Repository:
- Clone this repository or download it as a ZIP file:
git clone https://github.com/electrocoder/MBModbusRTUSlave.git
- Clone this repository or download it as a ZIP file:
-
Install to Arduino IDE:
- Move the
MBModbusRTUSlave
folder to your Arduinolibraries
directory:- Windows:
Documents/Arduino/libraries/
- macOS/Linux:
~/Documents/Arduino/libraries/
- Windows:
- Restart the Arduino IDE.
- Move the
-
Include in Your Sketch:
- Add the library to your sketch with:
#include <MBModbusRTUSlave.h>
- Add the library to your sketch with:
- Define the register count in your Arduino code:
This example initializes a Modbus slave with default settings and controls.
#include <MBModbusRTUSlave.h>
#define RXD2 16 // ESP32 Serial2
#define TXD2 17 // ESP32 Serial2
long mymodbusBaudRate = 9600; // Customizable baud rate
const uint16_t myRegisterCount = 20;
MBModbusRTUSlave modbus(0x01, &Serial2, 26, myRegisterCount); // Slave address: 0x01, Serial port name: Serial2, RS485 Module Pin: 26, Register count: 10
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
Serial.begin(9600); // DEBUG message
Serial2.begin(9600, SERIAL_8N2, RXD2, TXD2);
pinMode(26, OUTPUT);
modbus.begin(mymodbusBaudRate);
Serial.println("Modbus started");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
modbus.writeFloatRegister(0, 123.45); // Float method
modbus.writeRegister(2, 66); // Uint16_t method
}
// Read Register
if (modbus.readRegisters()) {
Serial.println("Modbus to read");
}
// Read Float and Integer Register
Serial.print(modbus.getFloatRegister(0));
Serial.print(" ");
Serial.print(modbus.getRegister(2));
Serial.println("");
delay(10);
}
Turn LED ON: Send 01 06 00 05 00 01 [CRC] (writes 1 to register 5).
Turn LED OFF: Send 01 06 00 05 00 00 [CRC] (writes 0 to register 5).
Read Registers: Send 01 03 00 00 00 0A [CRC] (reads 10 registers starting from 0).
You can adjust settings at runtime:
modbus.setSlaveAddress(0x02); // Change slave address to 0x02
modbus.setLedPin(12); // Change LED pin to 12
modbus.setLedRegisterIndex(7); // Use register 7 for LED control
modbus.setModbusBaudRate(115200); // Change baud rate to 115200
Any Arduino-compatible board with a serial interface (e.g., Uno, Mega, Nano).
An LED connected to the specified pin (default: pin 13, onboard LED on most Arduino boards).
A Modbus master device or software (e.g., Modbus Poll, QModMaster) for testing.
None (uses only the Arduino core library).
Contributions are welcome! Feel free to: Fork this repository.
Create a new branch for your feature or bug fix.
Submit a pull request with a clear description of your changes.
This project is licensed under the MIT License. See the LICENSE file for details. Acknowledgments Developed with assistance from Grok, created by xAI.
Inspired by the need for a simple Modbus RTU slave implementation on Arduino.
For questions or support, open an issue on this repository or reach out to [[email protected] (mailto:[email protected])].