Вы находитесь на странице: 1из 29

News Articles Tutorials Projects TI Training Forum Contact Enter your search tex

ARDUINO

RS-485 MODBUS Serial Communication


using Arduino UNO as Slave
By Pramoth Apr 24, 0
Thangavel 2019

LATEST POSTS

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


SPONSORED

DC Charging (Pile) Station block diagram

Serial Communication Protocols

RS-485 MODBUS Serial Communication using Arduino UNO as Slave

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.

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


SPONSORED SEARCHES
Brushed vs Brushless Motors: Operation,
Open to Buy Software Construction and Applications
Receiver Software

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

Three-phase Brushless Motor Controller


What is the difference between Modbus ASCII and Modbus RTU?
ICs with Sine Wave Drive
Modbus RTU and Modbus ASCII talks the same protocol. The only difference is that the bytes
being transmitted over the wire are presented as binary with RTU and as readable ASCII with
Modbus RTU. Modbus RTU will be used in this tutorial.

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.

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


RS-485 Serial Communication
RS-485 is an asynchronous serial communication protocol which doesn’t not require clock. It
uses a technique called differential signal to transfer binary data from one device to another.

So what is this differential signal transfer method??

NEWS ARTICLES PROJECTS

Three-phase Brushless
Motor Controller ICs with
Sine Wave Drive

LVAC Power Capacitors


With ESTAspring Features
Differential signal method works by creating a differential voltage by using a positive and Lever-Operated Spring Terminal Connection
negative 5V. It provides a Half-Duplex communication when using two wires and Full-
Cirrus Logic’s CS43131 and
Duplex requires 4 fours wires.
CS43198 DACs Deliver
By using this method: Exceptional Audio Fidelity for Mobile Devices

RS-485 supports higher data transfer rate of 30Mbps maximum.


MEMS Chip Combines
It also provides maximum data transfer distance compared toRS-232 protocol. It Accelerometer with High-
transfers data up to 1200-meter maximum. Accuracy Temperature Sensor for Superior
Precision
The main advantage of RS-485 over RS-232 is the multiple slave with single Master
while RS-232 supports only single slave. Lattice Radiant Software
Can have a maximum of 32 devices connected to RS-485 protocol. 1.1 FPGA Design Tools
Accelerates Design Reuse
Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
Another advantage of the RS-485 is immune to the noise as they usedifferential ACTIVE FORUM TOPICS
signal method to transfer.
Safety Distress Circuit
RS-485 is faster compared to I2C protocol. Roy Replies: 2

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

Log in to post questions


Pin-Out of RS-485:

User login
E-mail or username *

Pin Password *
Pin Description
Name

VCC 5V
Create new account
Request new password

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Non-inverting Receiver Input Log in
A Non-Inverting Driver Output

Inverting Receiver Input


B
Inverting Driver Output

GND GND (0V)

R0 Receiver Out (RX pin)

RE Receiver Output (LOW-Enable)

DE Driver Output (HIGH-Enable)

DI Driver Input (TX pin)

USB to RS-485 Converter Module

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


This is an USB to RS485 Converter Adapter module which supports WIN7, XP, Vista, Linux,
Mac OS and provides an easy to use RS485 interface by means of using COM port in the
computer. This module is plug-and-play device. There are no command structures, whatever is
sent to the Virtual COM Port is automatically converted to RS485 and vice versa. The module
is completely self-powered from the USB bus. So, no need of external power supply for operation.

It shows up as a Serial/COM port and is accessible from applications or hyper-terminal.This


converter provides half-duplex RS-485 communication. The Baud rate range is 75 bps to 115200
bps, maximum up to 6 Mbps.

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 Software


Modbus Master Software application is needed tosend data to slave Modbus RS-485 Arduino
device via COM.

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:

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


The second byte sent by the Master is the Function code. This number tells the slave
which table to access and whether to read from or write to the table.

Supported Register Function codes:

Function Code Action Table Name

04 (04 hex) Read Analog Input Registers

03 (03 hex) Read Analog Output Holding Registers

06 (06 hex) Write single Analog Output Holding Register

16 (10 hex) Write multiple Analog Output Holding Registers

Supported Coil Function codes:

Function Code Action Table Name

02 (02 hex) Read Discrete Input Contacts

01 (01 hex) Read Discrete Output Coils

05 (05 hex) Write single Discrete Output Coil

15 (0F hex) Write multiple Discrete Output Coils

CRC:

CRC stands for Cyclic Redundancy check. It is two bytes added to the end of every Modbus
message for error detection.

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Tools Required
Hardware

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

Simply Modbus Master

Circuit Diagram

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


SPONSORED SEARCHES
Circuit Simulator

Code and Programming

Circuit Connection between MAX-485 TTL to RS-485 converter module and Arduino UNO:

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Arduino UNO MAX-485 TTL to RS-485 Converter Module

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:

MAX-485 TTL to RS-485 USB to RS-485 Module

Converter Module Connected with PC

A A

B B

Circuit Connections between Arduino UNO and 16x2 LCD display:

16x2 LCD Arduino UNO

VSS GND

VDD +5V

To control pin of potentiometer for


V0
contrast/brightness control of 16x2 LCD

RS 8

RW GND

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


E 9

D4 10

D5 11

D6 12

D7 13

A +5V

K GND

Circuit Connection between 2 LEDs, Servo Motor and Arduino UNO:

Arduino UNO LED1 LED2 Servo Motor

Anode through 1k
2 - -
resistor

Anode through 1k
5 - -
resistor

6 - - PWM pin (Orange)

+5V - - +5V (RED)

GND Cathode GND Cathode GND GND (Brown)

Programming Arduino UNO for RS-485 MODBUS Slave


The Arduino UNO is configured as Modbus Slave. Also, Arduino UNO is attached with two LEDs
and one Servo Motor. So the slave Arduino is controlled from the Master Modbus Software.
Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
The communication between the Arduino UNO and the Modbus Master Software is
accomplished by using the RS-485 module. For connecting it with PC, the USB to RS-485
converter module is used. And the Arduino UNO with MAX-485 TTL to RS-485 converter
module, the whole setup will look file follows:

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.

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Programming has some major steps which will be explained below.

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.

uint16_t modbus_array[] = {0,0,0};

In setup function, firstly the LCD is set in 16x2 mode and a welcome message is displayed and
cleared.

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();

After this, LED1 and LED2 pins are set as output pins.

pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);

The servo pulse pin connected to PWM pin 6 of Arduino is attached.

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);

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


The Modbus slave is set at 9600 baudrate.

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.

First to turn ON or OFF the LED1 modbus_array[0] is used.

if (modbus_array[0] == 0) //Depends upon value in modubus_array[0] w


ritten by Master Modbus
{
digitalWrite(led1,LOW); //LED OFF if 0
lcd.setCursor(0,0);
lcd.print("L1:OFF");
}
else
{
digitalWrite(led1,HIGH); //LED ON if value other than 0
lcd.setCursor(0,0);
lcd.print("L1:ON");
}

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Next to turn ON or OFF the LED2 modbus_array[1] is used.

if (modbus_array[1] == 0) //Depends upon value in modbus_array[1] wri


tten by Master Modbus
{
digitalWrite(led2,LOW); //LED OFF if 0
lcd.setCursor(8,0);
lcd.print("L2:OFF");
}
else
{
digitalWrite(led2,HIGH); //LED ON if value other than 0
lcd.setCursor(9,0);
lcd.print("L2:ON");
}

Next to set the angle of the Servo motor themodbus_array[2] used and value is printed in the
16x2 LCD display.

int pwm = modbus_array[2];


servo.write(pwm);
lcd.setCursor(0,1);
lcd.print("Servo angle:");
lcd.print(pwm);
delay(200);
lcd.clear();

This finishes programming Arduino UNO for working it as MODBUS Slave. The next step will be
testing it as Modbus Slave.

Testing the Arduino UNO as Rs485 Modbus Slave


Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
After the circuit connections are completed and the code is uploaded to the Arduino UNO, its
time to connect the USB to RS-485 module with the PC where the Simple Modbus Master
software is installed.

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.

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


2. After the Simply Modbus Master Write is opened. Set the parameters

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.

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Setting Parameters for Arduino UNO as RS-485 Modbus Slave

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.

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Controlling Servo using Arduino UNO as RS-485 Modbus Slave

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


You can see both LED status is ON and servo angle at 90 degree.

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.

5. Now writing 135 to 40003 and 40001 as 0 and 40002 as 1.

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.

Find the complete code and a Demonstration video below.

RECOMMENDED TI WHITEPAPERS

Taking charge of electric


vehicles – both in the vehicle and on the grid
This paper will attempt to explain onboard chargers, how they work and why they’re used. It
will also explain charging stations and how they...

Which new semiconductor


technologies will speed electric vehicle charging
adoption?
Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
Read this whitepaper to learn more about EV charging systems and their design.

Code
//RS-485 Modbus Slave (Arduino UNO)
//Circuit Digest

#include<ModbusRtu.h> //Library for using Modbus in Arduino


#include<LiquidCrystal.h> //Library for using 16x2 LCD display
#include <Servo.h> //Library for using Servo Motor

#define led1 2 //Define as 2 led1


#define led2 5 //Define as 5 led2

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();

pinMode(led1,OUTPUT); //Led1 set as OUTPUT


Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
pinMode(led2,OUTPUT); //Led2 set as OUTPUT
servo.attach(6); //Servo PWM pin 6
bus = Modbus(1,1,4); //Modbus slave ID as 1 and 1 connected via RS-485 and 4 connected to
DE & RE pin of RS-485 Module
bus.begin(9600); //Modbus slave baudrate at 9600
}

void loop()

{
bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0])); //Used to receive or
write value from Master

if (modbus_array[0] == 0) //Depends upon value in modubus_array[0] written by Master Modbus


{
digitalWrite(led1,LOW); //LED OFF if 0
lcd.setCursor(0,0);
lcd.print("L1:OFF");
}
else
{
digitalWrite(led1,HIGH); //LED ON if value other than 0

lcd.setCursor(0,0);
lcd.print("L1:ON");
}

if (modbus_array[1] == 0) //Depends upon value in modbus_array[1] written by Master Modbus


{
digitalWrite(led2,LOW); //LED OFF if 0
lcd.setCursor(8,0);
lcd.print("L2:OFF");
Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
}
else

{
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

servo.write(pwm); //Write Received value (0 to 180) from Modbus Master


lcd.setCursor(0,1);
lcd.print("Servo angle:");
lcd.print(pwm); //Prints Angle in 16x2 LCD display.
delay(200);
lcd.clear();
}

Video

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


TAGS ARDUINO UNO ARDUINO MODBUS RS485

SERIAL SERVO
COMMUNICATION MOTOR

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


Get Our Weekly Newsletter!
Subscribe below to receive most popular news, articles and DIY projects from Circuit Digest

Email Address *

Name

Country
United States of America

Subscribe

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf


PREVIOUS POST NEXT POST
Interfacing Vibration Sensor Module Installing and Testing Mosquitto
with Arduino MQTT Broker on Raspberry Pi for IoT
Communication

COMMENTS

LOG IN OR REGISTER TO POST COMMENT

TI TRAINING VIDEOS

Solutions for Fast Charging Electric Vehicle Supply Equipment (EVSE) Design

EV Charging station - Introduction

Introduction to EV charging (pile) station standards

The Future of Renewable Energy

Level 1 and 2 AC Charging (Pile) Station Design Considerations

EV Charging Station System Solutions

Commonly Overlooked Sub-Systems in EV Charging (Pile) Stations

Level 3 EV / DC Charging (Pile) Station Design Considerations

High Voltage Solutions in HEV/EV Part I - On Board Chargers and Charging


Stations

High Voltage Solutions in HEV/EV Part II - DC/DC Converters and Traction


Inverters
Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf
Design considerations for USB Type-C™ power delivery

How to Design Multi-kW DC/DC Converters for Electric Vehicles (EVs) - EV


System Overview

TI Technology Enables High-Power EV DC Fast Charging Stations

EV Charging (Pile) Station Subsystem Analysis

CATEGORIES POPULAR NEWSLETTER

Embedded ROBOTICS 555 CIRCUITS Sign Up for Latest News


Electronics
Connect with us on social media and stay
updated with latest news, articles and projects! ARDUINO PROJECTS Enter your email
Power Electronics

Analog Electronics RASPBERRY PI PROJECTS


Subscribe
Internet of Things
ELECTRONICS NEWS
Audio Electronics
ELECTRONICS FORUM Hardware Startup?

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

Convertido de web en PDF a https://www.htmlapdf.com con el api html a pdf