How to Use Software Serial in Arduino: Simple Guide
Use the
SoftwareSerial library to create a serial communication on any digital pins by defining RX and TX pins. Initialize it with SoftwareSerial mySerial(rxPin, txPin);, then start communication with mySerial.begin(baudRate); and use mySerial.read() or mySerial.write() to receive or send data.Syntax
The SoftwareSerial library lets you create serial communication on any two digital pins. You first include the library, then create an object with RX and TX pins. Use begin() to set the baud rate, and read(), write(), available() to interact.
#include <SoftwareSerial.h>: Includes the library.SoftwareSerial mySerial(rxPin, txPin);: Creates a serial object on chosen pins.mySerial.begin(baudRate);: Starts serial communication at the speed you want.mySerial.read(): Reads incoming data.mySerial.write(data): Sends data out.
arduino
#include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); // RX, TX void setup() { mySerial.begin(9600); // Start software serial at 9600 baud } void loop() { if (mySerial.available()) { char c = mySerial.read(); // Read incoming byte mySerial.write(c); // Echo it back } }
Example
This example shows how to set up software serial on pins 10 (RX) and 11 (TX) to echo back any data received. It demonstrates basic reading and writing with software serial.
arduino
#include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); // RX, TX void setup() { Serial.begin(9600); // Start hardware serial for monitoring mySerial.begin(9600); // Start software serial Serial.println("Software Serial Ready"); } void loop() { if (mySerial.available()) { char incoming = mySerial.read(); Serial.print("Received: "); Serial.println(incoming); mySerial.write(incoming); // Echo back } }
Output
Software Serial Ready
Received: A
Received: B
Received: C
Common Pitfalls
Common mistakes when using SoftwareSerial include:
- Using the same pins for hardware serial and software serial, causing conflicts.
- Not calling
mySerial.begin()before reading or writing. - Trying to use software serial at very high baud rates (above 115200) which may cause errors.
- Not checking
mySerial.available()before reading, which can cause wrong data reads.
Also, only one software serial port can listen at a time, so switch carefully if using multiple.
arduino
/* Wrong way: No begin() called */ #include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); void setup() { Serial.begin(9600); } void loop() { if (mySerial.available()) { char c = mySerial.read(); // This won't work properly Serial.println(c); } } /* Right way: */ #include <SoftwareSerial.h> SoftwareSerial mySerial(10, 11); void setup() { Serial.begin(9600); mySerial.begin(9600); // Must start software serial } void loop() { if (mySerial.available()) { char c = mySerial.read(); Serial.println(c); } }
Quick Reference
Here is a quick summary of key SoftwareSerial functions:
| Function | Description |
|---|---|
| SoftwareSerial(rxPin, txPin) | Create software serial on chosen pins |
| begin(baudRate) | Start serial communication at given speed |
| available() | Check if data is ready to read |
| read() | Read one byte of incoming data |
| write(data) | Send data out |
| listen() | Make this port active if multiple software serials exist |
| end() | Stop software serial communication |
Key Takeaways
Use SoftwareSerial to add extra serial ports on any digital pins.
Always call begin() before reading or writing with software serial.
Check available() before reading to avoid errors.
SoftwareSerial works best at baud rates up to 115200.
Only one software serial port can listen at a time; switch with listen() if needed.