The SoftwareSerial library allows serial communication on other digital pins of an Arduino board, 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.
The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library by 'Mikal Hart'.
To use this library:
1#include <SoftwareSerial.h>
SoftwareSerial library has the following known limitations:
If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library.
SoftwareSerial()
Create an instance of a SoftwareSerial object. Multiple SoftwareSerial objects may be created, however only one can be active at a given moment.
1SoftwareSerial(rxPin, txPin, inverse_logic)
None.
1#include <SoftwareSerial.h>2
3const byte rxPin = 2;4const byte txPin = 3;5
6// Set up a new SoftwareSerial object7SoftwareSerial mySerial (rxPin, txPin);
available()
Get the number of bytes (characters) available for reading from a software serial port. This is data that has already arrived and stored in the serial receive buffer.
1mySerial.available()
None.
The number of bytes available to read.
1#include <SoftwareSerial.h>2
3#define rxPin 104#define txPin 115
6// Set up a new SoftwareSerial object7SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);8
9void setup() {10 // Define pin modes for TX and RX11 pinMode(rxPin, INPUT);12 pinMode(txPin, OUTPUT);13 14 // Set the baud rate for the SoftwareSerial object15 mySerial.begin(9600);16}17
18void loop() {19 if (mySerial.available() > 0) {20 mySerial.read();21 }22}
begin()
Sets the speed (baud rate) for the serial communication. Supported baud rates are: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200 bauds.
1mySerial.begin(speed)
None.
1#include <SoftwareSerial.h>2
3#define rxPin 104#define txPin 115
6// Set up a new SoftwareSerial object7SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);8
9void setup() {10 // Define pin modes for TX and RX11 pinMode(rxPin, INPUT);12 pinMode(txPin, OUTPUT);13
14 // Set the baud rate for the SoftwareSerial object15 mySerial.begin(9600);16}17
18void loop() {19 // ...20}
isListening()
Tests to see if requested software serial object is actively listening.
1mySerial.isListening()
None.
Boolean.
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial portOne(10, 11);5
6void setup() {7 // Set the baud rate for the Serial port8 Serial.begin(9600);9
10 // Set the baud rate for the SerialSoftware object11 portOne.begin(9600);12}13
14void loop() {15 if (portOne.isListening()) { 16 Serial.println("portOne is listening!");17 }18
19 // ...
overflow()
Tests to see if a SoftwareSerial buffer overflow has occurred. Calling this function clears the overflow flag, meaning that subsequent calls will return false unless another byte of data has been received and discarded in the meantime. The SoftwareSerial buffer can hold up to 64 bytes.
1mySerial.overflow()
None.
Boolean.
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial portOne(10, 11);5
6void setup() {7 // Set the baud rate for the Serial port8 Serial.begin(9600);9
10 // Set the baud rate for the SerialSoftware object11 portOne.begin(9600);12}13
14void loop() {15 if (portOne.overflow()) {16 Serial.println("portOne overflow!");17 }18
19 // ...
peek()
Return a character that was received on the RX pin of the software serial port. Unlike read(), however, subsequent calls to this function will return the same character. Note that only one SoftwareSerial object can receive incoming data at a time (select which one with the listen() function).
1mySerial.peek()
None.
The character read or -1 if none is available.
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial mySerial(10, 11);5
6void setup() {7 // Set the baud rate for the SerialSoftware object8 mySerial.begin(9600);9}10
11void loop() {12 char c = mySerial.peek();13}
read()
Return a character that was received on the RX pin of the SoftwareSerial objecto. Note that only one SoftwareSerial object can receive incoming data at a time (select which one with the listen() function).
1mySerial.read()
None.
The character read or -1 if none is available.
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial mySerial(10, 11);5
6void setup() {7 // Set the baud rate for the SerialSoftware object8 mySerial.begin(9600);9}10
11void loop() {12 char c = mySerial.read();13}
print()
Prints data to the transmit pin of the SoftwareSerial object. Works the same as the Serial.print() function.
1mySerial.print(val)
The number of bytes written (reading this number is optional).
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial mySerial(10, 11);5
6int analogValue;7
8void setup() {9 // Set the baud rate for the SerialSoftware object10 mySerial.begin(9600);11}12
13void loop() {14 // Read the analog value on pin A015 analogValue = analogRead(A0);16
17 // Print analogValue in the Serial Monitor in many formats:18 mySerial.print(analogValue); // Print as an ASCII-encoded decimal19 mySerial.print("\t"); // Print a tab character20 mySerial.print(analogValue, DEC); // Print as an ASCII-encoded decimal21 mySerial.print("\t"); // Print a tab character22 mySerial.print(analogValue, HEX); // Print as an ASCII-encoded hexadecimal23 mySerial.print("\t"); // Print a tab character24 mySerial.print(analogValue, OCT); // Print as an ASCII-encoded octal25 mySerial.print("\t"); // Print a tab character26 mySerial.print(analogValue, BIN); // Print as an ASCII-encoded binary27 mySerial.print("\t"); // Print a tab character28 mySerial.print(analogValue/4, BYTE); // Print as a raw byte value (divide the29 // value in 4 because analogRead() function returns numbers30 // from 0 to 1023, but a byte can only hold values up to 255)31
32 mySerial.print("\t"); // Print a tab character 33 mySerial.println(); // Print a line feed character34
35 // Pause for 10 milliseconds before the next reading36 delay(10);37}
println()
Prints data to the transmit pin of the SoftwareSerial object followed by a carriage return and line feed. Works the same as the Serial.println() function.
1mySerial.println(val)
The number of bytes written (reading this number is optional).
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial mySerial(10, 11);5
6int analogValue;7
8void setup() {9 // Set the baud rate for the SerialSoftware object10 mySerial.begin(9600);11}12
13void loop() {14 // Read the analog value on pin A015 analogValue = analogRead(A0);16
17 // Print analogValue in the Serial Monitor in many formats:18 mySerial.print(analogValue); // Print as an ASCII-encoded decimal19 mySerial.print("\t"); // Print a tab character20 mySerial.print(analogValue, DEC); // Print as an ASCII-encoded decimal21 mySerial.print("\t"); // Print a tab character22 mySerial.print(analogValue, HEX); // Print as an ASCII-encoded hexadecimal23 mySerial.print("\t"); // Print a tab character24 mySerial.print(analogValue, OCT); // Print as an ASCII-encoded octal25 mySerial.print("\t"); // Print a tab character26 mySerial.print(analogValue, BIN); // Print as an ASCII-encoded binary27 mySerial.print("\t"); // Print a tab character28 mySerial.print(analogValue/4, BYTE); // Print as a raw byte value (divide the29 // value in 4 because analogRead() function returns numbers30 // from 0 to 1023, but a byte can only hold values up to 255)31
32 mySerial.print("\t"); // Print a tab character 33 mySerial.println(); // Print a line feed character34
35 // Pause for 10 milliseconds before the next reading36 delay(10);37}
listen()
Enables the selected SoftwareSerial object to listen. Only one SoftwareSerial object can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() function (unless the given instance is already listening).
1mySerial.listen()
None.
Returns true if it replaces another.
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial portOne(10, 11);5
6// Set up a new SoftwareSerial object with RX in digital pin 8 and TX in digital pin 97SoftwareSerial portTwo(8, 9);8
9void setup() {10 // Set the baud rate for the Serial object11 Serial.begin(9600);12
13 // Set the baud rate for the SerialSoftware objects14 portOne.begin(9600);15 portTwo.begin(9600);16}17
18void loop() {19 // Enable SoftwareSerial object to listen20 portOne.listen();21 22 if (portOne.isListening()) {23 Serial.println("portOne is listening!");24 } else {25 Serial.println("portOne is not listening!");26 }27
28 if (portTwo.isListening()) {29 Serial.println("portTwo is listening!");30 } else {31 Serial.println("portTwo is not listening!");32 }33}
write()
Prints data to the transmit pin of the SoftwareSerial object as raw bytes. Works the same as the Serial.write()function.
1mySerial.write(val)
The number of bytes written (reading this number is optional).
1#include <SoftwareSerial.h>2
3// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 114SoftwareSerial mySerial(10, 11);5
6void setup() {7 // Set the baud rate for the SerialSoftware object8 mySerial.begin(9600);9}10
11void loop() {12 // Send a byte with the value 4513 mySerial.write(45);14
15 //Send the string “hello” and return the length of the string.16 int bytesSent = mySerial.write(“hello”);17}