How to Use the Software Serial library in Arduino PLC industrial controller

Arduino Software Serial example
February 18, 2019 by
How to Use the Software Serial library in Arduino PLC industrial controller
Boot & Work Corp. S.L., Support Team


 
 

Index

What is Serial communication with Arduino? How is the Library used in the Arduino IDE?


There are different types of Serial Communications. When you use an Arduino board on a project you can choose the standard Serial pins as Arduino software serial Rx Tx, from the UART inside the Arduino board, so it is called Serial TTL. In that case, you will use the Hardwareserial.h Library, but some additional pins can work as a Rx or Tx. For example, the SPI communication pins can work as a MISO, MOSI and Select (SC), but they are also pins that can work as a digital input or digital output, or if you need, you could use those pins as Rx, Tx using the softwareserial.h Library. 

Serial communications allow you to connect two different devices sending and receiving data between them.  

The Serial TTL port can be transformed as required on Industry as an RS232 and as an RS485. When you use RS232 the functionality is quite similar to working as Serial TTL but if you work using RS485 you can configure a network using a Master device that can connect with Slave devices. So the number of devices has been increased from 2 to 32 devices (nodes). And the max distance between them can be up to 1220m if the wiring is well done and in compliance with EMC and the electrical noise is avoided.

To sum up, if you are using the UART serial port from the Arduino or the Arduino-based PLC for Industrial projects, the use of other pins working as a Serial TTL can help you with the success of the development of your project.  So, that additional serial port must be programmed using softwareserial.h library.

If the HardwareSerial Library can not be used, because you need to use a communication protocol that needs the use of a physical UART instead of a virtual serial port. Then, you could convert the standard RS232 or RS485 from the device to a Serial TTL. (This example will be shown in another post)

This post explains the advantage of the SoftwareSerial Library to simulate a serial port through Software (virtual serial TTL) using the Arduino IDE. 

Serial ports on Arduino and Arduino Industrial PLCs using the Industrial Shields

Arduino Uno includes 1 serial TTL port from pin 0 and pin 1. It works from UART, but there is the possibility to use it as a TTL with other pins. For example, all digital pins can work as a Tx, but the SPI communication pins can work as an Rx, so you could be able to add at least 3 additional SoftwareSerial ports.  See on technical specifications from Arduino UNO that Rx and Tx are shared with USB port, so if you need to use Rx and Tx from UART the USB port can not be used. Other characteristics affect the use of Arduino UNO so we recommend the use of Arduino Leonardo and Arduino Mega if you need to use both ports, USB and Serial TTL.

If you see Arduino Mega, it includes 4 UART serial TTL ports and you can also use other pins as Rx. Using Arduino Leonard the UART is not shared from the USB so you could use USB and Serial TTL to work together

Anyway, we don't recommend the use of the USB port to send and receive data because it is designed just to program the Arduino board. and Sometimes the configuration of the UART chip can be misconfigured if the volume and/or the velocity of the communication is quite high. So, for safety reasons on Industrial projects, we recommend using RS232 instead of USB.

Using an Arduino Industrial controller

Using an Arduino Industrial Shield, the standard configuration has already been done. So you can connect directly to RS232 and RS485 communication ports but only one of them can run under hardware serial, so, the other must be configurated as SoftwareSerial. You need to configure the internal jumpers to define which port should work as hardware serial and which one should work as SoftwareSerial because the PLC allows you to configure which one can run as a hardware serial. 

See the example of the use

                Requirements

                Configuration

                For the Arduino LEONARDO

                All pins can be used for Tx on the Arduino LEONARDO, while for Rx the following can be used:

                • Pin 14 of Leonardo (SO)

                • Pin 15 of Leonardo (SCK)

                • Pin 16 of Leonardo(SI)

                We recommend using one of the Rx-enabled pins for Tx.

                For the Arduino MEGA

                On the Arduino MEGA, all pins can be used for Tx, while for the Rx the following can be used: 

                • Pin 50 of the Arduino Mega 2560 (SO)

                • Pin 51 of the Arduino Mega (SI)

                • Pin 52 of the Arduino Mega (SCK)

                We recommend using one of the Rx-enabled pins for Tx. Using an Arduino board (this example has been done using Arduino Leonardo, Arduino Uno and Arduino Mega. If you need to use other Arduino boards, you can check the technical specification from that board).


                Software

                This sketch is very simple and shows how the library works. First, the Tx and Rx pins of the equipment for the SoftwareSerial must be defined (we are trying it on an Ardbox Family model). See the SoftwareSerial source code:
                SoftwareSerial mySerial(14, 15); // RX, TX

                Rx is digital pin 14 (SO), connected to TX of the other device.

                Tx is digital pin 15 (SCK), connected to RX of the other device.


                Receives from the HardwareSerial, send to SoftwareSerial. Receives from SoftwareSerial, send to HardwareSerial.

                #include <SoftwareSerial.h>
                
                SoftwareSerial mySerial(14, 15); // RX (MISO), TX (SCK)
                
                void setup()  
                {
                  // Open serial communications and wait for port to open:
                  Serial.begin(9600);
                  while (!Serial) {
                    ; // wait for serial port to connect. Needed for Leonardo only
                  }
                
                
                  Serial.println("Goodnight moon!");
                
                  // set the data rate for the SoftwareSerial port
                mySerial.begin(9600); mySerial.println("Hello, world?"); } void loop() // run over and over
                { if (mySerial.available())
                Serial.write(mySerial.read());
                if (Serial.available())
                mySerial.write(Serial.read()); }

                This other sketch sends instructions through the SoftwareSerial: 
                 
                #include <SoftwareSerial.h>
                SoftwareSerial mySerial(14, 15);  // Rx (MISO), Tx (SCK)
                void setup() {
                  // put your setup code here, to run once:
                mySerial.begin(9600);
                Serial.begin(9600);
                }
                void loop() {
                  // put your main code here, to run repeatedly:
                delay(500);
                mySerial.println("Instruction 1"); 
                Serial.println("1st instruction sended");
                delay(500); 
                mySerial.println("Instruction 2"); 
                Serial.println("2nd instruction sended");
                }

                ​Search in our Blog

                How to Use the Software Serial library in Arduino PLC industrial controller
                Boot & Work Corp. S.L., Support Team February 18, 2019

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