Using SoftwareSerial in Arduino for Serial Communication

The Arduino hardware has built-in support for serial communication on pins 0 and 1. 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. So, Let’s learn about using SoftwareSerial in Arduino.

The SoftwareSerial library allows serial communication on other digital I/O pins. It replicates the serial communication functionality using the software. 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.

Different Functions of SoftwareSerial Library

Constructor

It is used to create an instance of SoftwareSerial Object. You can create Multiple SoftwareSerial objects, however only one can be active at a given moment. You need to call SoftwareSerial.begin() to enable the communication.

Syntax: SoftwareSerial(rxPin, txPin, inverse_logic)

Parameters:

rxPin: the pin on which to receive serial data
txPin: the pin on which to transmit serial data
inverse_logic: It is used to invert the sense of incoming bits. If set, SoftwareSerial treats a LOW (0 volts on the pin, normally) on the Rx pin as a 1-bit (the idle state) and a HIGH (5 volts on the pin, normally) as a 0-bit. It also affects the way that it writes to the Tx pin. The default value is false.

Warning: You should not connect devices that output serial data outside the range that the Arduino can handle, normally 0V to 5V, for a board running at 5V, and 0V to 3.3V for a board running at 3.3V.


Begin

It begins the SoftwareSerial on the given baud rate.

Syntax: mySerial.begin(9600);

Parameters: speed: the baud rate (long)

Read

Return a character that was received on the RX pin of the software serial port. Note that only one SoftwareSerialinstance can receive incoming data at a time. Returns -1 if no data is available.

Syntax: mySerial.read();

Write

Prints data to the transmit pin of the software serial port as raw bytes. Works the same as the Serial.write() function

Syntax: mySerial.write(data);


Print

Prints data to the transmit pin of the software serial port. Works the same as the Serial.print() function. Same way println() function also works.

Syntax: mySerial.print(data);

Available

Get the number of bytes (characters) available for reading from a software serial port. This is data that’s which arrived and stored in the serial receive buffer.

Syntax: mySerial.available();

Example

This is an example for use of SoftwareSerial Library in Arduino and other compatible boards.




In code, integrated <SoftwareSerial.h> Library. mySerial is constructed on pin numbers 10 and 11. In setup, Begin at 9600 baud rate. We are writing ‘123’ on mySerial in the loop function.

1 thought on “Using SoftwareSerial in Arduino for Serial Communication”

Leave a Reply

Your email address will not be published. Required fields are marked *