How to Use Modbus with Arduino: Simple Guide and Example
To use
Modbus with Arduino, include a Modbus library like ModbusMaster, set up the serial communication, and define the Modbus slave or master behavior. Use functions like readHoldingRegisters() or writeSingleRegister() to communicate over RS485 or serial lines.Syntax
Using Modbus on Arduino typically involves these steps:
- Include the Modbus library: This provides Modbus functions.
- Initialize Modbus object: Define master or slave and communication pins.
- Begin communication: Set baud rate and serial port.
- Read or write registers: Use functions like
readHoldingRegisters()orwriteSingleRegister(). - Poll or respond: Call
modbus.poll()regularly to handle requests.
arduino
#include <ModbusMaster.h> ModbusMaster node; void setup() { Serial.begin(9600); // Start serial communication node.begin(1, Serial); // Slave ID 1, use Serial port } void loop() { uint8_t result; result = node.readHoldingRegisters(0x00, 1); // Read 1 register at address 0 if (result == node.ku8MBSuccess) { uint16_t data = node.getResponseBuffer(0); // Use data } delay(1000); }
Example
This example shows how to read a holding register from a Modbus slave device with ID 1 over serial communication.
arduino
#include <ModbusMaster.h> ModbusMaster node; void setup() { Serial.begin(9600); // Start serial communication at 9600 baud node.begin(1, Serial); // Set slave ID to 1 Serial.println("Modbus Master Started"); } void loop() { uint8_t result; result = node.readHoldingRegisters(0x00, 1); // Read 1 register at address 0 if (result == node.ku8MBSuccess) { uint16_t data = node.getResponseBuffer(0); Serial.print("Register 0 value: "); Serial.println(data); } else { Serial.print("Error reading register: "); Serial.println(result); } delay(2000); // Wait 2 seconds before next read }
Output
Modbus Master Started
Register 0 value: 123
Register 0 value: 123
Register 0 value: 123
Common Pitfalls
- Wrong baud rate: Ensure both Arduino and Modbus device use the same baud rate.
- Incorrect slave ID: The slave ID must match the device you want to communicate with.
- Wiring errors: RS485 requires correct wiring of A/B lines and a common ground.
- Not calling
poll()regularly: This function processes Modbus messages and must be called often. - Buffer overflow: Reading too many registers at once can cause errors.
arduino
/* Wrong way: Not initializing Modbus or wrong slave ID */ // node.begin(2, Serial); // Wrong slave ID if device is 1 /* Right way: Correct slave ID and initialization */ // node.begin(1, Serial);
Quick Reference
Here is a quick cheat sheet for common ModbusMaster functions:
| Function | Description |
|---|---|
| begin(slaveID, serialPort) | Initialize Modbus with slave ID and serial port |
| readHoldingRegisters(address, count) | Read registers starting at address |
| writeSingleRegister(address, value) | Write a single register value |
| getResponseBuffer(index) | Get data from response buffer |
| poll() | Process Modbus messages (for slave mode) |
Key Takeaways
Use a Modbus library like ModbusMaster to simplify communication on Arduino.
Match baud rate and slave ID between Arduino and Modbus device for successful communication.
Call modbus.poll() regularly if using slave mode to handle requests.
Check wiring carefully, especially for RS485 connections (A/B lines and ground).
Read or write registers in small chunks to avoid buffer errors.