PLC and Arduino Interaction Based On Modbus Protocol
PLC and Arduino Interaction Based On Modbus Protocol
511-519)
https://doi.org/10.9717/kmms.2017.20.3.511
ABSTRACT
This Paper introduces the design and communication method between PLC (Programmable Logic
Controller) and Arduino based on MODBUS Protocol. MODBUS connection can be established in a new
or existing system very easily, therefore we used this protocol in our proposed system. In the field of
automatic devices, multi-function serial port such as RS232, RS422, RS485, and so on creates a great
convenience to the developer. This proposed system used RS485 as a key mediator for data exchanging
on a connected network. We also believe that it will reduce the development cost in various automated
industry because this system can be reused or can be implemented any such PLC installed machines.
RS485 is used as a communication interface between PLC (as a slave) and Arduino (as a master), through
which a reliable network is created for safe and fast communication. Furthermore, RS485 allows multiple
devices(up to 32) to communicate at half duplex on a single pair of wires and provides a long connectivity
area (up to 1200 meters) as compare to other device, which makes it a user-friendly for various devices
in the automated industry. Moreover, Arduino can play as a mediator by connecting third party device
and setup a communication network with PLC.
Key words: PLC, RS485, MODBUS Protocol, Arduino, IoT
PLC comes with build in communication port, usu- sensors. This leads Arduino to the development of
ally 9-pin RS-232, RS-422, RS-485 and Ethernet. a variety of hardware extensions and software li-
Here we will be discussing on RS-485 port. Today braries that enable wired and wireless communica-
modern PLCs can be programmed in a variety of tion with the Internet[7].
ways, from ladder logic to any programming
languages. Under the IEC 61131-3[3 ,4] standard 2. MODBUS PROTOCOL OVERVIEW
PLC can be programmed using standard based
programming languages, in which ladder logic is Modbus protocol is a serial communication pro-
most commonly used. Ladder logic sometimes tocol where digital communication networks can be
called “relay logic” was designed to work like relay implemented over long distance effectively and al-
logic, an old programming language which was al- so in electrically noisy environments. Moreover,
ready in wide use. It is run by reading and writing Modbus can run over virtually in all communica-
from the memory table of inputs, outputs and in- tion media, including twisted pair wires, wireless,
termediate values. fiber optics, Ethernet, telephone modems, cell
Arduino is an open-source platform which is phones and microwave. Here multiple receivers can
used for building electronics projects. The main be connected in a linear network where the only
point of “Arduino Platform” is to allow for easy one will be master device and rest will be a slave.
and fast Platform. It is famous for its simple pro- Slave can’t transmit data without a request from
gramming and rich resources and can also be the master node, and also can’t communicate with
mount each other like bricks to add some more another slave. The master node issues a MODBUS
modules. Arduino hardware platform has the pow- request to the slave nodes in two modes i.e. unicast
er and resets circuitry setup with circuitry to pro- mode and broadcast mode[8].
gram and communicate with the microcontroller At the time of communication on MODBUS
over USB. It consists of microcontrollers and IDE network. The protocol determines how the con-
which helps to upload computer code to the phys- troller knows about its device address, recognize
ical board. It uses a simplified version of C++, message address to it, determine the action to be
making it easier to learn and use[5, 6]. It is a flexi- taken and extract the data or information contain
ble micro-controller and development environment in the message. MODBUS protocol takes place at
that can not only be used to control devices but level 2 of the OSI model. At the physical level,
also can be used to read data from all kind of MODBUS uses different physical interface like
RS485, RS232 etc. RS485 on a Two-Wire interface
Table 1. Comparison of Modbus ASCII and Modbus RTU
which is most commonly used. At Data link layer Table 4. MODBUS Function Codes
MODBUS Serial Line Protocol is defined[10]. CODE Description
MODBUS RTU network consists of one “master” 01 Read coil status
and up to 247 “slaves”[11]. 02 Read input status
In Table 1, we have presented the difference be- 03 Read holding registers
tween Modbus ASCII and Modbus RTU. 04 Read input registers
05 Force single coil
06 Present single registers
2.1 MODBUS Addressing
07 Read exception status
The MODBUS address space includes 256 dif- 15 Force multiple coils
ferent address. 16 Write holding/multiple registers
The Address 0 is for broadcast purpose and all 17 Report slave ID
slave must recognize the broadcast address. The
Master node has no specific address. outputs in a slave. In other words, it is used to Read
Coil Status or digital status from a Modbus Slave
Table 2. MODBUS Address data acquisition device. It read from 1 to 2000 con-
0 1 - 247 248 - 255 tiguous status of coils in a remote device. The
Broadcast Slave Address Reserved Request PDU specifies the starting address i.e. the
Address address of the first coil specified. The Query mes-
sage specifies the starting coil and quantity of coils
2.2 MODBUS Frame Structure to be read. Table 5 Represents a request to read
Modbus frame is composed of ADU(Application slave (20-27) from device 17. The coil in the re-
Data unit) which encloses a PDU(Protocol Data sponse message are generally packed as one coil
Unit). ADU composed of Address, PDU and Error per bit of data field, Table 6 shows the frame
check. PDU is composed Function code and Data. structure.
Table 5. Query Structure of Function 01
3. MODBUS OVERVIEW
Field Name RTU
3.1 MODBUS Function Codes Header None
Slave Address 11
MODBUS has 10 function codes as shown in Function 01
Table 4. In this proposed system we have used two Starting Address Hi 00
codes i.e. 04 and 16. Starting Address Lo 13
No. of Points Hi 00
A. Function 01: Read Coil Status No. of Points Lo 25
Function 01 reads ON/OFF status of discrete CRC --
Table 6. Response Frame Structure of Function 01 ponse Frame structure is represented in Table 8.
Field Name RTU
Header None C. Function 04: Read Input Registers
Slave Address 11 The Modbus RTU function 04 is used to read
Function 01 Input registers from a Modbus Slave device.
Byte Count 05 Function 04 reads the contents of the 3X registers.
Data (Coils 27-20) CD Here the address starts from 0(zero), for example,
CRC --
1-10 are addressed as 0-9. Function 04 includes
the quantity of registers to read from the Modbus
B. Function 03: Read Holding Registers Slave device. With Function 04 master used to re-
The Modbus RTU Function 03 is used to Read quest one or more holding register value from the
Holding Registers (4x register references), in a device but only one slave device can be addressed
Modbus slave device. This function reads con- in a single query. The frame structure of query and
tinuous blocks of holding registers in the remote response are shown in Table 9 and Table 10
device. Registers are addressed starting at Zero. respectively.
Query message specifies the starting registers. In Table 9, Top 2 byte in Data field is the start
Here we present an example of a request to read address register, the lower two bytes is word units.
register 40108-40110 from slave device 17. Query In Table 10, the first byte in Data field is the num-
frame structure is shown in Table 7. In the Res- ber of data to read (byte unit) and then the top data,
ponse message, the register data are packed as two sub-data and lower data.
bytes per registers with binary contents. For each
register, the first byte contains the high order bits D. Function16: Write Holding Registers
and the second contains the low order bits. Res- With Function16 Master Device write values in-
Table 7. Query Structure of Function 03
to a sequence of holding registers to specified slave.
The query message specifies the register refer-
Field Name RTU ence. It writes values which are specified in the
Header None
Slave Address 11
Function 03 Table 9. Query Structure of Function 04
Table 11. Query Structure of Function16 code, starting address and quantity of registers
Field Name RTU written.
Header None
Slave Address 11 3.2 Modbus Exception
Function 16 Modbus exception codes are error codes that are
Starting Address Hi 00 returned by slave devices when it detects any
Starting Address Lo 01 problem with a command it received. It can be gen-
Quantity of Register Hi 00
Quantity of Register Lo 02
erated for several reasons, bad data values, invalid
Byte Count 04 Modbus function codes, invalid register and much
Data Hi 00 more. These errors result in an exception response
Data Lo 02 from either the master computer software (Modbus
Data Hi 00 Communications Handler) or the PC Slave, de-
Data Lo 04 pending on the typeof error. The standardized
Error Check Lo C6 Exception codes are listed in Table 13
Error Check Hi B5 Here we also focused on counting the type of
error occur and in which situation. This helped us
Table 12. Response Frame Structure of Function 16 to understand our system much more deeply.
Field Name RTU
Header None 4. IMPLEMENTATION
Slave Address 11
Function 16 This proposed system was successfully im-
Starting Address Hi 00 plemented and tested with Arduino, RS485 and
Starting Address Lo 01 PLC by Modbus RTU protocol. Here we have dis-
Quantity of Register Hi 00 cussed the implementation briefly with the result
Quantity of Register Lo 02 table. The system must confirm that devices on
Error Check Lo 12 the same bus must have same communication
Error Check Hi 98 parameters.
request data field. Here data packets are two bytes 4.1 Wiring of the proposed system
per register. Table 11 shows the frame structure Fig. 1(a) gives an overall idea of the connection
of Query. Table 12 shows the frame structure of between Arduino and PLC with the mediator
Response, it returns the slave address, function RS-485. Here TX and RX port of Arduino is con-
Table13. Response Frame Structure of Function 16
(a)
(b)
(c)
Fig. 1. (a) System Wiring (b) Setup Location (c) PLC Setup Box.
nected to DI and RO of RS485 respectively. picts the location where we setup our system to
Whereas RE and DE port of RS485 are combined test. (c) is the PLC setup Box where *39 and 40
and connected to port13 of Arduino. Port 13 is used port is connected to RS485(A, B port shown in Fig.
as a button. 1(a))
Fig. 1(a) depicts the full system wiring. (b) de-
PLC and Arduino Interaction Based on Modbus Protocol 517
Address Function Code Data Data Data Data CRC Low CRC HI
0×01 0×04 0×00 0×00 0×00 0×01 0×18 0×F0
0×01 0×04 0×00 0×01 0×00 0×01 0×19 0×60
0×01 0×04 0×00 0×02 0×00 0×01 0×19 0×90
0×01 0×04 0×00 0×03 0×00 0×01 0×19 0×00
0×01 0×04 0×00 0×04 0×00 0×01 0×1A 0×30
4.2 Results and Arduino with less cost and time. We believe
To test our system we send data from Arduino that this system will reduce the cost of develop-
to PLC using function code 04 where slave address ment for various companies due to reusability and
is 0x01. Here starting data address from 0×00 to easy to install. This proposed system can be able
0x04 represents the different data request from to setup a communication network with a device
PLC. having PLC installed in it or PLC supportable
In Table 14, first two data column states the devices. Moreover, Arduino gives a wide range of
starting address of PLC and next two data column option to connect other third party devices either
states the amount of data is being sent. In the through a wireless module or physically connected
fourth row of this table, Data from 0×00-0×04 are to it. Here we have mainly focused on error count-
the register start address from the data need to be ing and handling. Study on error handling helped
read. us to rectify the situation of error and on what type
In the same way PLC reply to Arduino with data of data error has occurred. Due cost effective and
which was requested. We should note that the CRC complexity reduction this system will be helpful in
should be same during each communication other- the field of automatic devices.
wise there will be an error in communication. Table In future, we are going to use this system as
15 depicts the reply to Arduino request. a mediator between two or more devices which can
In Table 15, first data column lists the amount communicate each other effectively. This system
of data receives and the next two data column are will provide easy connectivity module for various
received data. Third row Data represents the num- devices.
ber of data is received. Fourth and fifth row repre-
sents the data of defined address. REFERENCE
[ 2 ] B.B. Shibarinath and N. Gaur, “MODBUS netics, IEEE International Conference , Vol.
Communication in Microcontroller Based 4, No. 6, pp. 2431-2436, 2000.
Elevator Controller,” International Confer- [ 9 ] PLCopen Technical Committee 6, “XML
ence on Control, Automation, Robotics and Formats for IEC 61131-3, Ver 1.0”, http://
Embedded Systems, pp. 16-18, 2013. www.plcopen.org/pages/tc6_xml/down-
[ 3 ] Comité Europeo de Normalización, EN loads/tc6_xml_v201_technical_doc.pdf
61131-3: Programmable Controllers. Prog- (accessed Nov., 21, 2016).
ramming languages (IEC 611131-3:2013) , [10] MODBUS over Serial Line Specification and
International Electrotechnical Comission, Implementation Guide V1.02, http://www.mod-
Brussels, France, 2013. bus. org/docs/Modbus_over_serial_line_V1_02.
[4] E. Tisserant, L. Bessard, and M. Sousa, “An pdf (accessed Nov., 21, 2016).
Open Source IEC 61131-3 Integrated Devel- [11] X. Jin, H. Yuan, Q. Jiang, and Y. Wang,
opment Environment,” Transactions on In- “Design of Silicon-based Transient Voltage
dustrial Informatics, Vol. 1, pp. 183-187. 2007. Suppressor to Meet IEC System-level ESD
[5] M. Banzi, Getting Started with Arduino, Specification for RS485 Transceiver,” Pro-
O’Relly Media, Sebastopol, Calif., 2011. ceeding of International Conference on Solid-
[6] M. Margolis, Arduino Cookbook, O’Relly State and Integrated Circuit Technology, pp.
Media, Sebastopol, Calif., 2011. 1-3, 2014.
[7] J. Ko, J. Shim, “A Comparison of the Con- [12] Z. Wu, S. Liu, M. Du, Q. Li, C. Han, and J.
struction for IoT System in Smart Clothing,” Wu, “Design of Vibrating Wire Sensor
Journal of Multimedia Information System, Singnal Acquisition Board Based on STM32,”
v.2, no.4, pp. 327-332, 2015. Proceeding of World Congress on Intelligent
[8] G. Frey and L. Litz, “Formal Methods in PLC Control and Automation, pp. 1846-1849, 2016.
Programming,” Systems, Man, and Cyber-
PLC and Arduino Interaction Based on Modbus Protocol 519