Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteris

How to Use the SoftwareSerial library in Arduino PLC industrial controller

See an Arduino serial communication example


 
 

What is Arduino Serial Communication? and How to use it


There are different types of Serial Communications. When you use an Arduino board on a project you can choose the standard Serial pins as 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 there are some additional pins that can work as an 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 library softwareserial.h.

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 a RS232 and as a RS485. When you use RS232 the functionality is quite similar as working as Serial TTL but if you work using RS485 you can configure a network using a Master device that is able to 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 from 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)

In this post, we explain the advantage of the SoftwareSerial library to simulate a serial port through Software (virtual serial TTL) using the Arduino IDE. 

Arduino Uno board Serial TTL

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

Arduino Uno includes 1 serial TTL port from pin 0 and pin 1 it works from UART, but there are the possibility to use as a TTL 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 software serial ports.  See on technical specifications from Arduino UNO that Rx and Tx is shared with USB port, so if you need to use Rx and Tx from UART the USB port can not be used. There are other characteristics that 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 working together

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

Using and Arduino Industrial controller

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

Industrial Arduino PLC Ardbox Relay

See the example of use.

Requirements

Ethernet or 20 I/Os PLC:    Ethernet PLC          20 I/Os PLC

SoftwareSerial Library:       SoftwareSerial Library

Configuration

For the Arduino LEONARDO:

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

pin 14 of Leonardo (SO), pin 15 of Leonardo (SCK), or 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 Serial Software 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 (SI), connected to RX of the other device.

Receives from the serial hardware, sends to software serial, receives from software serial, sends to hardware serial
.

#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 software serial: 
 

#include <SoftwareSerial.h>


SoftwareSerial mySerial(14, 15);  // Rx (MISO, Tx (MOSI)

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

}

Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send