RS485 ModBus Serial Communication Using Arduino Uno As Slave
RS485 ModBus Serial Communication Using Arduino Uno As Slave
ARDUINO
LATEST POSTS
Modbus is a Serial Communication protocol which was discovered by Modicon in 1979 and it is DIY Location Tracker using GSM SIM800
and Arduino
used for transmitting data over serial lines between the industrial electronic devices. RS-485
Modbus uses RS-485 for transmission lines. It should be noted thatModbus is a software
protocol and not a hardware protocol. It is divided into two parts such as Modbus Master and
Modbus Slave. In RS-485 Modbus network there is one Master and 127 Slaves each with
unique address from 1 to 127.
Modbus are mostly used in PLCs (Programmable Logic Controllers). And apart from this, the
Modbus is also used in Healthcare, Transportation, Home Automation etc. Modbus has 255
function codes and there are mainly three popular versions of Modbus:
MODBUS RTU
MODBUS ASCII
MODBUS/TCP
This tutorial is about using RS-485 Modbus communication with Arduino UNO as Slave. Here
we install Simply Modbus Master Software in PC and control two LEDs and Servo Motor by
using RS-485 as transmission line. These LEDs and servo motor are connected with Slave
Arduino and controlled by sending values using Master Modbus Software. Since this tutorial Installing and Testing Mosquitto MQTT
uses RS-485, it is recommended to first go through RS485 Serial Communication between Broker on Raspberry Pi for IoT
Communication
Arduino Uno and Arduino Nano. RS485 can also be used with other controllers for serial
communication:
RS-485 Serial Communication between Raspberry Pi & Arduino UNO What is Capacitor Leakage Current and
How to Reduce It
Serial Communication Between STM32F103C8 and Arduino UNO using RS-485
Let’s begin by exploring some background about the RS-485 and Modbus.
Three-phase Brushless
Motor Controller ICs with
Sine Wave Drive
projects
viresh Replies: 0
sending message
Connecting RS-485 with Arduino
viresh Replies: 0
RS-485 Module can be connected to any microcontroller having serial port. For using RS-485 UART Communication between GSM and
module with microcontrollers, a module called 5V MAX485 TTL to RS485 which is based on GPS
Ankur Replies: 3
Maxim MAX485 IC is needed as it allows serial communication over long distance of 1200
meters. It is bidirectional and half duplex and has data transfer rate of 2.5 Mbps. This module what is this this resistor value?
requires a voltage of 5V. Duane Replies: 2
User login
E-mail or username *
Pin Password *
Pin Description
Name
VCC 5V
Create new account
Request new password
To use this device there are various Modbus Software available in the internet. In this tutorial a
software called Simply Modbus Software is used.
Simply Modbus Master is a data communication test software. You can download theSimply
Modbus Master from the given link and learn more about it by referringSoftware Manual.
Before using the software, it is important to get familiar with the following terminologies.
Slave ID:
Each slave in a network is assigned a unique unit address from 1 to 127. When the
master requests data, the first byte it sends is the Slave address. This way each slave knows
after the first byte whether or not to ignore the message.
Function code:
CRC:
CRC stands for Cyclic Redundancy check. It is two bytes added to the end of every Modbus
message for error detection.
Arduino UNO
MAX-485 TTL to RS-485 Converter Module
USB to RS-485 Converter Module
LED (2)
1k-Resistor (2)
16x2 LCD display
10k Potentiometer
Servo Motor SG-90
Software
Circuit Diagram
Circuit Connection between MAX-485 TTL to RS-485 converter module and Arduino UNO:
0(RX) RO
1(TX) DI
4 DE & RE
+5V VCC
GND GND
Circuit Connection between MAX-485 TTL to RS-485 Module and USB to RS-485 converter:
A A
B B
VSS GND
VDD +5V
RS 8
RW GND
D4 10
D5 11
D6 12
D7 13
A +5V
K GND
Anode through 1k
2 - -
resistor
Anode through 1k
5 - -
resistor
For using Modbus in Arduino UNO, a library <ModbusRtu.h> is used. This library is used for
communicating with RS-485 Modbus Master or Slave via RTU protocol. Download the Modbus
RTU and add the library in the sketch by followingSketch->include library->Add .zip Library.
Initially, include the required library. ModbusRTU library is for using RS-485 Modbus
communication, and the liquid crystal library is for using LCD with Arduino UNO, and the servo
library is for using Servo motor with Arduino UNO.
#include<ModbusRtu.h>
#include<LiquidCrystal.h>
#include <Servo.h>
Now the LED anode pins that are connected with Arduino pins 2 and 5 are defined as LED1 and
LED2.
#define led1 2
#define led2 5
Next the object for accessing Liquid Crystal class is declared with the LCD pins (RS, E, D4, D5,
D6, D7) that are connected with Arduino UNO.
LiquidCrystal lcd(8,9,10,11,12,13);
When LCD is done, Initialize servo object for class Servo. Also Initialize bus object for class
Modbus.
Servo servo;
Modbus bus;
Next for storing values for Modbus communication an array is declared with the three values
Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
initialized with zero.
In setup function, firstly the LCD is set in 16x2 mode and a welcome message is displayed and
cleared.
After this, LED1 and LED2 pins are set as output pins.
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
servo.attach(6);
Now for the Modbus communication the following parameters are set. First ‘1’ represents
Slave ID, second ‘1’ represents that it uses RS-485 to transfer data and ‘4’ represents RS-485
DE&RE pin connected to Arduino UNO.
bus = Modbus(1,1,4);
The loop starts with the definition of bus poll andbus.poll() is used to write and receive value
from the master Modbus.
bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0]));
This method is used to check if there is any data available at the serial port.
If there is any data available at serial port the Modbus RTU library will check the message
(check the device address, data length, and CRC) and perform the required action.
For example to write or read any value from master, the ModbusRTU must receive an unsigned
16-bit integer array and its length from the Master Modbus. This array carries the data that is
written from the master.
In this tutorial there are three arrays for LED1, LED2 and Servo motor angle.
Next to set the angle of the Servo motor themodbus_array[2] used and value is printed in the
16x2 LCD display.
This finishes programming Arduino UNO for working it as MODBUS Slave. The next step will be
testing it as Modbus Slave.
Open the device manager and check the COM port according to your PC where the USB to RS-
485 Module is connected and after that open the Simply Modbus Master 8.1.1 software.
1. After Simply Modbus Software is opened now open the Write option.
Mode in RTU, COM port according to your PC (mine was COM6), Baud at 9600, Data Bits 8,
Stop bit 1, Parity None and Slave ID as 1.
3. After that set first register as 40001 and values to write is 3 and the function code as 16
(Write Holding Register).
After that write 1 to 40001 (For LED1 on) and 1 to 40002 (For LED2 on) and 90 to 40003 (For
Servo Motor Angle) and then click SEND button.
4. After that enter 40001 as 1 and 40002 as 0 and 40003 as 180 and click SEND button.
Now Servo angle at 180 and the led1 is ON and led2 is OFF.
Now the servo position is at 135 and led1 is OFF and led2 is ON.
This is how RS-485 Modbus can be used in serial communication with Arduino UNO as Slave.
In next tutorial we will use the Arduino Uno as master in MODBUS commination.
RECOMMENDED TI WHITEPAPERS
Code
//RS-485 Modbus Slave (Arduino UNO)
//Circuit Digest
LiquidCrystal lcd(8,9,10,11,12,13); //initizlize lcd object with pins (RS,E,D4,D5,D6,D7) for class liquid
crystal
Servo servo; //Initilize servo object for class Servo
Modbus bus; //Define Object bus for class modbus
uint16_t modbus_array[] = {0,0,0}; //Array initilized with three 0 values
void setup()
{
lcd.begin(16,2); //Lcd set in 16x2 mode
lcd.print("RS-485 Modbus"); //Welcome Message
lcd.setCursor(0,1);
lcd.print("Arduino Slave");
delay(5000);
lcd.clear();
void loop()
{
bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0])); //Used to receive or
write value from Master
lcd.setCursor(0,0);
lcd.print("L1:ON");
}
{
digitalWrite(led2,HIGH); //LED ON if value other than 0
lcd.setCursor(9,0);
lcd.print("L2:ON");
}
int pwm = modbus_array[2]; //Depends upon value in modbus_array[1] written by Master Modbus
Video
SERIAL SERVO
COMMUNICATION MOTOR
Email Address *
Name
Country
United States of America
Subscribe
COMMENTS
TI TRAINING VIDEOS
Solutions for Fast Charging Electric Vehicle Supply Equipment (EVSE) Design
CALCULATORS
This website uses cookies to improve user experience. By using the website you are giving your consent to
set ©cookies. For Digest.
more information, read our cookie policy and privacy policy. OK, I Understand
Copyright 2019 Circuit All rights reserved. Privacy Policy | Cookie Policy | Terms of Use | Contact Us | Advertise