Softwareserial Library: Limitations of This Library

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation

A R D U I N O .C C STORE Search documentation SIGN IN

H A R D WA R E S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN

SoftwareSerial Library
Learn SoftwareSerial Library

Limitations of This Library

Examples SoftwareSerial Library


Methods
The SoftwareSerial library allows serial communication on other digital pins of an Arduino board.
SoftwareSerial()

Syntax AUTHOR: Arduino LAST REVISION: 11/22/2022, 10:18 PM

Parameters

Returns 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
Example
possible to have multiple software serial ports with speeds up to 115200 bps. A parameter
See also enables inverted signaling for devices which require that protocol.

available()
The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library by
Syntax 'Mikal Hart'.

Parameters
To use this library:
Returns
COPY
Example 1 #include <SoftwareSerial.h>

See also

begin() Limitations of This Library


Syntax
SoftwareSerial library has the following known limitations:
Parameters
It cannot transmit and receive data at the same time.
Returns
If using multiple software serial ports, only one can receive data at a time.
Example
Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the
See also
following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64),
A11 (65), A12 (66), A13 (67), A14 (68), A15 (69). Not all pins on the Leonardo and Micro
isListening() boards support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14
(MISO), 15 (SCK), 16 (MOSI).
Syntax
On Arduino or Genuino 101 boards the current maximum RX speed is 57600bps.
Parameters
On Arduino or Genuino 101 boards RX doesn't work on digital pin 13.

If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library.

Examples
SoftwareSerial example: sometimes one serial port just isn't enough!

Two port receive: Work with multiple software serial ports.

Methods

We use cookies 🍪 SoftwareSerial()


Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy. Create an instance of a SoftwareSerial object. Multiple SoftwareSerial objects may be created,
however only one can be active at a given moment.

ONLY REQUIRED
Syntax ACCEPT ALL
Help

COPY
https://docs.arduino.cc/learn/built-in-libraries/software-serial 1/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation
COPY
A R D U I N O .C C STORE 1 SoftwareSerial(rxPin, txPin, inverse_logic)
Search documentation SIGN IN

H A R D WA R E S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN


Parameters

rxPin: the pin on which to receive serial data.


txPin: the pin on which to transmit serial data.
inverse_logic: used to invert the sense of incoming bits (the default is normal logic). If set,
SoftwareSerial treats a LOW (0v on the pin, normally) on the RX pin as a 1-bit (the idle
state) and a HIGH (5V on the pin, normally) as a 0-bit. It also affects the way that it writes
to the TX pin. Default value is false.

Returns

None.

Example

COPY
1 #include <SoftwareSerial.h>
2
3 const byte rxPin = 2;
4 const byte txPin = 3;
5
6 // Set up a new SoftwareSerial object
7 SoftwareSerial mySerial (rxPin, txPin);

See also

available()

begin()

isListening()

overflow()

peek()

read()

print()

println()

listen()

write()

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.

Syntax

COPY
1 mySerial.available()

Parameters

None.
We use cookies 🍪
Returns
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy.
The number of bytes available to read.

Example
ONLY REQUIRED ACCEPT ALL
Help
COPY
https://docs.arduino.cc/learn/built-in-libraries/software-serial 2/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation

1 #include <SoftwareSerial.h>
A R D U I N O .C C STORE Search documentation SIGN IN
2
3 #define rxPin 10
H A R D WA R E S O F4T WA
#define
RE txPin
C L O U D11 PROGRAMMING TUTORIALS LEARN
5
6 // Set up a new SoftwareSerial object
7 SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
8
9 void setup() {
10 // Define pin modes for TX and RX
11 pinMode(rxPin, INPUT);
12 pinMode(txPin, OUTPUT);
13
14 // Set the baud rate for the SoftwareSerial object
15 mySerial.begin(9600);
16 }
17
18 void loop() {
19 if (mySerial.available() > 0) {
20 mySerial.read();
21 }
22 }

See also

SoftwareSerial()

begin()

isListening()

overflow()

peek()

read()

print()

println()

listen()

write()

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.

Syntax

COPY
1 mySerial.begin(speed)

Parameters

speed: the desired baud rate (long). Supported baud rates are: 300, 600, 1200, 2400, 4800,
9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200 bauds.

Returns

None.

Example

We use cookies 🍪 COPY


1 #include <SoftwareSerial.h>
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
2
reading our cookie policy.
3 #define rxPin 10
4 #define txPin 11
5
6 // Set up a new SoftwareSerial object
ONLY REQUIRED 7 SoftwareSerial mySerial = SoftwareSerial(rxPin, ACCEPT txPin);
ALL
Help
8
9 void setup() {
https://docs.arduino.cc/learn/built-in-libraries/software-serial 3/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation
10 // Define pin modes for TX and RX
A R D U I N O .C C STORE
11 pinMode(rxPin, INPUT); Search documentation SIGN IN

12 pinMode(txPin, OUTPUT);
H A R D WA R E
13
S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN
14 // Set the baud rate for the SoftwareSerial object
15 mySerial.begin(9600);
16 }
17
18 void loop() {
19 // ...
20 }

See also

SoftwareSerial()

available()

isListening()

overflow()

peek()

read()

print()

println()

listen()

write()

isListening()

Tests to see if requested software serial object is actively listening.

Syntax

COPY
1 mySerial.isListening()

Parameters

None.

Returns

Boolean.

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in dig
4 SoftwareSerial portOne(10, 11);
5
6 void setup() {
7 // Set the baud rate for the Serial port
8 Serial.begin(9600);
9
10 // Set the baud rate for the SerialSoftware object
11 portOne.begin(9600);
12 }
We use cookies 🍪 13
14 void loop() {
15 if (portOne.isListening()) {
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy.
16 Serial.println("portOne is listening!");
17 }
18
19 // ...
ONLY REQUIRED ACCEPT ALL
Help

https://docs.arduino.cc/learn/built-in-libraries/software-serial 4/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation

A R D U I N O .C C STORE
See also Search documentation SIGN IN

H A R D WA R E
SoftwareSerial()
S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN

available()

begin()

overflow()

peek()

read()

print()

println()

listen()

write()

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.

Syntax

COPY
1 mySerial.overflow()

Parameters

None.

Returns

Boolean.

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in dig
4 SoftwareSerial portOne(10, 11);
5
6 void setup() {
7 // Set the baud rate for the Serial port
8 Serial.begin(9600);
9
10 // Set the baud rate for the SerialSoftware object
11 portOne.begin(9600);
12 }
13
14 void loop() {
15 if (portOne.overflow()) {
16 Serial.println("portOne overflow!");
17 }
18
19 // ...

We use cookies 🍪
See also
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy.
SoftwareSerial()

available()
ONLY REQUIRED ACCEPT ALL
begin() Help

https://docs.arduino.cc/learn/built-in-libraries/software-serial 5/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation

A R D U I N O .C C STORE
isListening() Search documentation SIGN IN

peek()
H A R D WA R E S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN
read()

print()

println()

listen()

write()

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

Syntax

COPY
1 mySerial.peek()

Parameters

None.

Returns

The character read or -1 if none is available.

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in dig
4 SoftwareSerial mySerial(10, 11);
5
6 void setup() {
7 // Set the baud rate for the SerialSoftware object
8 mySerial.begin(9600);
9 }
10
11 void loop() {
12 char c = mySerial.peek();
13 }

See also

SoftwareSerial()

available()

begin()

isListening()

overflow()

read()
We use cookies 🍪
print()
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy. println()

listen()

ONLY REQUIRED write() ACCEPT ALL


Help

https://docs.arduino.cc/learn/built-in-libraries/software-serial 6/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation

A R D U I N O .C C STORE read() Search documentation SIGN IN

H A R D WA R E SReturn
O F T WAa
R Echaracter
CLOthat
UD was received
P R O G R A on
M Mthe
I N G RX pin Tof
U Tthe
O R ISoftwareSerial
ALS L E A R N objecto. Note that

only one SoftwareSerial object can receive incoming data at a time (select which one with the
listen() function).

Syntax

COPY
1 mySerial.read()

Parameters

None.

Returns

The character read or -1 if none is available.

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in dig
4 SoftwareSerial mySerial(10, 11);
5
6 void setup() {
7 // Set the baud rate for the SerialSoftware object
8 mySerial.begin(9600);
9 }
10
11 void loop() {
12 char c = mySerial.read();
13 }

See also

SoftwareSerial()

available()

begin()

isListening()

overflow()

peek()

print()

println()

listen()

write()

print()

Prints data to the transmit pin of the SoftwareSerial object. Works the same as the
Serial.print() function.
We use cookies 🍪
Syntax
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy.

COPY
1 mySerial.print(val)

ONLY REQUIRED ACCEPT ALL


Help
Parameters

https://docs.arduino.cc/learn/built-in-libraries/software-serial 7/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation
val: the value to print.
A R D U I N O .C C STORE Search documentation SIGN IN

Returns
H A R D WA R E S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN

The number of bytes written (reading this number is optional).

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in d
4 SoftwareSerial mySerial(10, 11);
5
6 int analogValue;
7
8 void setup() {
9 // Set the baud rate for the SerialSoftware object
10 mySerial.begin(9600);
11 }
12
13 void loop() {
14 // Read the analog value on pin A0
15 analogValue = analogRead(A0);
16
17 // Print analogValue in the Serial Monitor in many formats:
18 mySerial.print(analogValue); // Print as an ASCII-encoded decim
19 mySerial.print("\t"); // Print a tab character
20 mySerial.print(analogValue, DEC); // Print as an ASCII-encoded decim
21 mySerial.print("\t"); // Print a tab character
22 mySerial.print(analogValue, HEX); // Print as an ASCII-encoded hexad
23 mySerial.print("\t"); // Print a tab character
24 mySerial.print(analogValue, OCT); // Print as an ASCII-encoded octal
25 mySerial.print("\t"); // Print a tab character
26 mySerial.print(analogValue, BIN); // Print as an ASCII-encoded binar
27 mySerial.print("\t"); // Print a tab character
28 mySerial.print(analogValue/4, BYTE); // Print as a raw byte value (divi
29 // value in 4 because analogRead()
30 // from 0 to 1023, but a byte can
31
32 mySerial.print("\t"); // Print a tab character
33 mySerial println(); // Print a line feed character

See also

SoftwareSerial()

available()

begin()

isListening()

overflow()

peek()

read()

print()

println()

listen()

write()

println()

Prints data to the transmit pin of the SoftwareSerial object followed by a carriage return and
We use cookies 🍪 line feed. Works the same as the Serial.println() function.

Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy. Syntax

COPY

ONLY REQUIRED
1 mySerial.println(val) ACCEPT ALL
Help

https://docs.arduino.cc/learn/built-in-libraries/software-serial 8/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation

Parameters
A R D U I N O .C C STORE Search documentation SIGN IN

val: the value to print.


H A R D WA R E S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN

Returns

The number of bytes written (reading this number is optional).

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in d
4 SoftwareSerial mySerial(10, 11);
5
6 int analogValue;
7
8 void setup() {
9 // Set the baud rate for the SerialSoftware object
10 mySerial.begin(9600);
11 }
12
13 void loop() {
14 // Read the analog value on pin A0
15 analogValue = analogRead(A0);
16
17 // Print analogValue in the Serial Monitor in many formats:
18 mySerial.print(analogValue); // Print as an ASCII-encoded decim
19 mySerial.print("\t"); // Print a tab character
20 mySerial.print(analogValue, DEC); // Print as an ASCII-encoded decim
21 mySerial.print("\t"); // Print a tab character
22 mySerial.print(analogValue, HEX); // Print as an ASCII-encoded hexad
23 mySerial.print("\t"); // Print a tab character
24 mySerial.print(analogValue, OCT); // Print as an ASCII-encoded octal
25 mySerial.print("\t"); // Print a tab character
26 mySerial.print(analogValue, BIN); // Print as an ASCII-encoded binar
27 mySerial.print("\t"); // Print a tab character
28 mySerial.print(analogValue/4, BYTE); // Print as a raw byte value (divi
29 // value in 4 because analogRead()
30 // from 0 to 1023, but a byte can
31
32 mySerial.print("\t"); // Print a tab character
33 mySerial println(); // Print a line feed character

See also

SoftwareSerial()

available()

begin()

isListening()

overflow()

peek()

read()

print()

listen()

write()

listen()

We use cookies 🍪 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
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
discarded during the call to listen() function (unless the given instance is already listening).
reading our cookie policy.

Syntax

ONLY REQUIRED ACCEPT ALL


Help
COPY
1 mySerial.listen()
https://docs.arduino.cc/learn/built-in-libraries/software-serial 9/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation

A R D U I N O .C C STORE Search documentation SIGN IN

Parameters
H A R D WA R E S O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN
None.

Returns

Returns true if it replaces another.

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in d
4 SoftwareSerial portOne(10, 11);
5
6 // Set up a new SoftwareSerial object with RX in digital pin 8 and TX in di
7 SoftwareSerial portTwo(8, 9);
8
9 void setup() {
10 // Set the baud rate for the Serial object
11 Serial.begin(9600);
12
13 // Set the baud rate for the SerialSoftware objects
14 portOne.begin(9600);
15 portTwo.begin(9600);
16 }
17
18 void loop() {
19 // Enable SoftwareSerial object to listen
20 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 }

See also

SoftwareSerial()

available()

begin()

isListening()

overflow()

peek()

read()

print()

println()

write()

write()
We use cookies 🍪
Prints data to the transmit pin of the SoftwareSerial object as raw bytes. Works the same as
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy. the Serial.write()function.

Syntax

ONLY REQUIRED ACCEPT ALL


Help
COPY

https://docs.arduino.cc/learn/built-in-libraries/software-serial 10/11
11/23/22, 11:28 AM SoftwareSerial Library | Arduino Documentation | Arduino Documentation
1 mySerial.write(val)
A R D U I N O .C C STORE Search documentation SIGN IN

H A R D WA R E SParameters
O F T WA R E CLOUD PROGRAMMING TUTORIALS LEARN

val: the binary value to print.

Returns

The number of bytes written (reading this number is optional).

Example

COPY
1 #include <SoftwareSerial.h>
2
3 // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in dig
4 SoftwareSerial mySerial(10, 11);
5
6 void setup() {
7 // Set the baud rate for the SerialSoftware object
8 mySerial.begin(9600);
9 }
10
11 void loop() {
12 // Send a byte with the value 45
13 mySerial.write(45);
14
15 //Send the string “hello” and return the length of the string.
16 int bytesSent = mySerial.write(“hello”);
17 }

See also

SoftwareSerial()

available()

begin()

isListening()

overflow()

peek()

read()

print()

println()

listen()

Back to top

Trademark Help Center NEWSLETTER FOLLOW US

Contact Us
Enter your email to sign up SUBSCRIBE
Distributors

Careers

© 2022 Arduino Terms Of Service Privacy Policy Security Cookie Settings

We use cookies 🍪
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised advertisement. You can adjust this in Cookie Settings or learn more by
reading our cookie policy.

ONLY REQUIRED ACCEPT ALL


Help

https://docs.arduino.cc/learn/built-in-libraries/software-serial 11/11

You might also like