SoftwareSerial- How to add additional TX and RX pins on your Arduino board

In work with Arduino, you often want to use more than one module. On Arduino UNO board, you get only one pair of TX and RX pins which are connected to module respectively, but what do you do when you want to use more than one module? That is where SoftwareSerial library comes in handy.



SoftwareSerial library

The Arduino hardware has built-in support for serial communication on pins 0 and 1 (which also goes to the computer via the USB connection). The native serial support happens via a piece of hardware (built into the chip) called a UART. This hardware allows the Atmega chip to receive serial communication even while working on other tasks, as long as there room in the 64 byte serial buffer.

The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name “SoftwareSerial”). It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol.

Set TX and RX

Next example shows how to set TX and RX on pins 10 and 11

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
    // Open serial communications and wait for port to open:
    Serial.begin(57600);
    while (!Serial) {
     ; // wait for serial port to connect. Needed for native USB port only
    }

    Serial.println("Goodnight moon!");

    // set the data rate for the SoftwareSerial port
    mySerial.begin(4800);
    mySerial.println("Hello, world?");

}

void loop() { // run over and over
    if (mySerial.available()) {
        Serial.write(mySerial.read());
    }
    if (Serial.available()) {
        mySerial.write(Serial.read());
    }
}

And that is it. You treat mySerial the same way as built in Serial.
Cheers

Share with your friends