Basics about RS-485 of an industrial PLC

March 6, 2020 by
Basics about RS-485 of an industrial PLC
Andrei Postolache

            RS-485 's Introduction

            RS-485, also known as EIA-485, is defined as a differential multi-point bus system. The RS-485 standard is perfect to transmit data with high speed to ranges until 12m. One of its most important characteristics is that the twisted pair of cables reduce the noise-induced in the transmission line. Multiple receivers may be connected to such a network in a linear, multi-drop bus. These characteristics make RS-485 useful in industrial control systems and similar applications.

            Our PLCs for industrial automation have inside the integrated circuit MAX485. This one is a low-power transceiver used for the communication RS-485 that works with a single power supply of 5V and the average current is 300μA. Using Half Duplex communication to convert the TTL level to RS-485 level, it can reach the maximum speed of transmission of 2.5Mbps. Internally we can find a Half Duplex transceiver MAX485 and also a MAX485, in the way that, if we are in Full Duplex, it will use the Half Duplex MAX485 to receive data and the transmitter MAX485 will send it.

            Previous reading

            We recommend you read the following posts in order to understand the program of this blog. We used the following blog posts to do this example:
            How to program our industrial Arduino PLC with Arduino IDE:


            Requirements for working with RS-485

            In order to work with RS-485 protocol, you will need any of our industrial Arduino PLC controllers for industrial automation:
            - Industrial Shields controllers: 

            - Configuring the switches and jumpers
             

            RS-485 in M-Duino

            On the M-Duino Family you will always have activated the RS-485 by default, so you will not need to activate it using any switch. The only thing is that you need to select between Full Duplex and Half Duplex, using the last switch (FD/HD).

             

            RS485 in M-Duino - Configuring the switches and jumpers


            RS-485 in Ardbox

            For the Ardbox family we have two subfamilies. In this one, we have to choose between RS-485 and RS-232 using the switches and jumpers as we show in the following image:

            IMPORTANT: THE CONFIGURATION OF THE COMMUNICATION IS ALREADY DONE, YOU JUST NEED TO CHOOSE IT WHEN YOU BUY THE DEVICE.





            Ardbox Relay HF+



            Hardware

            The first thing we need to do is to be sure that the industrial Arduino PLC is supplied with 12-24Vdc.

            M-Duino - Pins PLC for RS-485 communication

            The pins of the PLC that we have to use for RS-485 communication. 

            Ardbox Analog HF+ - Pins PLC for RS-485 communication

            Ardbox Relay HF+ - Pins PLC for RS-485 communication

            The first step is downloading the Arduino based PLC boards for Arduino IDE.
            After making the hardware configuration, we need to continue with the software configuration and also how we use it. To begin this process, it is necessary to include the library RS485.h included with our Boards. After this, in the function setup, please take care to do the right implementation of your communication.

            For all families:

            Software configuration

            #include <RS485.h>

            To check the RS-485 activation you only have to use the serial monitor from the Arduino IDE using the right sentence inside the setup() function.

            Serial.begin(9600); 

            It is also important to implement the initialization on the setup() function.

            RS485.begin(38400);

            IMPORTANT❗: CHECK THE SPEED OF TRANSMISSION OF PLC-LAPTOP AND PLC-DEVICES.

            A basic example of writing in the RS-485:

            // Include Industrial Shields libraries
            #include <RS485.h>
            
            //// IMPORTANT: check switches configuration
            
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            void setup() {
              // Begin serial port
              Serial.begin(9600);
            
              // Begin RS485 port
              RS485.begin(38400);
            }
            
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            void loop() {
              // Wait bytes in the serial port
              if (Serial.available()) {
                byte tx = Serial.read();
            
                // Echo the byte to the serial port again
                Serial.write(tx);
            
            
                // And send it to the RS-485 port
              RS485.write(tx);
              }
            }


            A basic example of reading of the RS-485: 

            // Include Industrial Shields libraries
            #include <RS485.h>
            
            //// IMPORTANT: check switches configuration
            
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            void setup() {
              // Begin serial port
              Serial.begin(9600);
            
              // Begin RS485 port
              RS485.begin(38400);
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            void loop() {
              // Print received byte when available
              if (RS485.available()) {
                byte rx = RS485.read();
            
                // Hexadecimal representation
                Serial.print("HEX: ");
                Serial.print(rx, HEX);
            
                // Decimal representation
                Serial.print(", DEC: ");
                Serial.println(rx, DEC);
              }
            }


            Basic Full Duplex example of RS-485:

            Before starting the test, connect A, B (receivers) to the Y, Z (transmitters).

            // Include Industrial Shields libraries
            #include <RS485.h>
            //// IMPORTANT: check switches configuration
            //// IMPORTANT: Full duplex mode is only available when device supports it
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            byte tx = 5;
            void setup() {
              // Begin serial port
              Serial.begin(9600);
              // Begin RS485 port
              RS485.begin(38400, FULLDUPLEX);
              Serial.println("setup");
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            void loop() {
              // Wait bytes from the RS-485
              RS485.write(tx);
              delay(3000);
              if (RS485.available()) {   
                byte rx = RS485.read();
                // In full-duplex mode it is possible to send and receive data
                // at the same time in a secure way
                RS485.write(rx);
                // Echo the byte to the serial port
                Serial.println(rx);
              }
            } 


            ​Search in our Blog

            Basics about RS-485 of an industrial PLC
            Andrei Postolache March 6, 2020
            Share this post

            Looking for your ideal Programmable Logic Controller?

            Take a look at this product comparison with other industrial controllers Arduino-based. 

            We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


            Industrial PLC comparison >>>