Communication Modbus Arduino as Slave

posted in: Microcontroller, PLC | 1

Modbus is an industrial communication protocol standard that operates on the principle of “Master – Slave”. One Master can connect to many Slaves. Currently, most industrial devices use Modbus, so in order for the arduino to communicate with PLCs, HMIs, inverters, etc., we must be able to put the Modbus protocol into Arduino. You should read the articles about Modbus first to understand more before programming.

Today’s content is:

“Use Modbus RS485 to communicate between arduino and Modbus Poll”

  1. Prepare
  • Modbus Poll software , Modbus Poll is a Modbus master emulator mainly designed to help Modbus device developers or others who want to test and simulate the Modbus protocol. 
  • UART TTL to RS485 communication converter circuit (This circuit to convert UART to RS485 output of the circuit you can connect to the communication port of other Modbus devices).
  • USB to RS485 Converter (This circuit helps to connect RS485 to the computer’s USB port)
  • ModbusRtu Library for Arduino

2. Wiring diagram

3. Programming

Here I use register au16data[0] to switch off Led pin 12 and register au16data[12] to store analog value of pin A0.

#include <ModbusRtu.h>

// data array for modbus network sharing
uint16_t au16data[16] = {
  0, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };

 *  Modbus object declaration
 *  u8id : node id = 0 for master, = 1..247 for slave
 *  port : serial port
 *  u8txenpin : 0 for RS-232 and USB-FTDI 
 *               or any pin number > 1 for RS-485
Modbus slave(1,Serial,2); // this is slave @1 and RS-485 chân 2

void setup() {
  Serial.begin(9600,SERIAL_8E1); // baud-rate at 9600

void loop() {
  slave.poll( au16data, 16 );
  if (au16data[0]==1) digitalWrite(12,HIGH);
  else digitalWrite(12,LOW);
  au16data[12] = analogRead(A0);

4. Connect Modbus Poll to Arduino

Go to Connection > Connect install as follows

Where 9600 is the Baud rate and Even Parity is the error checking mode corresponding to the command line Serial.begin(9600,SERIAL_8E1);

After connecting, go to Setup > Read/ Write Definition.

Slave ID above I set to 1, you can change other values ​​in the code, choose Function 03 (Read the document to understand more about Modbus), The address is 0 and the number is 16 registers.


To turn on the LED on pin 12, double-click on the 0 register and change the Value to 1.

Leave a Reply

Your email address will not be published.