As an Amazon Associate we earn from qualifying purchases.
SoftUART, also called software serial, is a way to send and receive serial data using ordinary digital pins and program code instead of a dedicated UART chip. On Arduino boards, the most common way to do this is the SoftwareSerial library. This article explains what it does, walks through the example code, and covers the limits you should know about before using it in a project.
What is SoftUART?
A UART (Universal Asynchronous Receiver/Transmitter) is the hardware block that handles serial communication. It turns bytes into a stream of bits on a TX pin and rebuilds bytes from the bits arriving on an RX pin. Most microcontrollers have only a few of these, and some boards have just one.
A SoftUART does the same job in software. The code switches a pin high and low at precise intervals to send bits, and reads another pin at precise intervals to receive them. This technique is often called bit-banging. The result is a serial port you can place on almost any pair of free digital pins.
Why use software serial?
The main reason is a shortage of hardware serial ports. On a typical Arduino Uno, the single hardware UART is shared with the USB connection and pins 0 and 1. If you connect a device to those pins, you can have trouble uploading sketches or using the Serial Monitor at the same time.
Software serial gets around this. You can keep the hardware port for your computer and use a second, software-based port for another device. Common examples include:
- GPS receivers
- Bluetooth serial modules
- GSM or other cellular modules
- RFID readers
- Serial displays and other sensors that talk over TX/RX
Basic SoftwareSerial example
The sketch below creates a software serial port on pins 10 (RX) and 11 (TX), prints a message, and then echoes back any byte it receives.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop()
{
if (mySerial.available())
mySerial.write(mySerial.read());
}
How the code works
- #include <SoftwareSerial.h> loads the library, which ships with the Arduino IDE.
- SoftwareSerial mySerial(10, 11); creates a serial object. The first number is the RX pin (where data comes in), and the second is the TX pin (where data goes out). The name
mySerialis your choice. - mySerial.begin(9600); sets the baud rate, which is the speed of the connection. Both devices must use the same rate.
- mySerial.println(…) sends text out of the TX pin, followed by a line break.
- mySerial.available() returns how many bytes are waiting to be read. When it is above zero, mySerial.read() pulls one byte from the buffer, and mySerial.write() sends it straight back out.
The result is a simple echo: whatever the connected device sends is returned to it.
Wiring it up
Serial connections are crossed. The TX pin of one device connects to the RX pin of the other, and the other TX connects back to the first RX. In the example above, Arduino pin 11 (TX) goes to the RX pin of your module, and Arduino pin 10 (RX) goes to the TX pin of your module.
- Share a ground. Both devices need a common ground connection, or the signals will not be read correctly.
- Match voltage levels. Many Arduino boards use 5 V logic, while many modules use 3.3 V. Connecting a 5 V output directly to a 3.3 V input can damage the module. A level shifter or a voltage divider on the line solves this. Check your module’s datasheet.
- Match the baud rate. If you see garbled characters, a mismatched baud rate is the most common cause.
Limitations to know about
Software serial is convenient, but it is not a full replacement for a hardware UART. Because the processor does the work, there are trade-offs.
- Lower reliability at high speeds. 9600 baud is a safe choice. Faster rates can lead to lost or corrupted bytes, especially when the board is busy with other tasks.
- One receiver at a time. If you create several SoftwareSerial ports, only one can actively receive data at any moment. You switch between them with the
listen()method. - Interrupt use. The library relies on interrupts for timing. This can interfere with other code that depends on precise timing, and with other libraries that need interrupts.
- Pin restrictions. Not every pin works as an RX pin on every board. Check the documentation for your specific board if the example does not behave as expected.
- CPU load. Time spent sending and receiving bits is time the processor cannot spend on something else.
Tips for better results
- Keep
loop()short and avoid longdelay()calls while waiting for incoming data, so bytes are read before the buffer fills up. - Start at 9600 baud, confirm things work, and only then try a higher speed if the device needs it.
- If your board has a spare hardware serial port, use that for demanding devices such as GPS or cellular modules, and keep software serial for lighter jobs.
- Print debugging messages to the hardware serial port and keep the software port for the connected device.
- On other platforms, such as ESP boards, a different software serial library may be needed, since the standard Arduino one is written for AVR-style boards.
SoftUART beyond Arduino
The idea is not limited to Arduino. Any microcontroller that can toggle a pin and measure time accurately can run a software UART. Developers use it on chips with no spare hardware serial, or when a design needs more serial ports than the chip provides. The same trade-offs apply: it costs processor time and is best at moderate speeds.
FAQ
What does SoftUART stand for?
It is short for software UART. UART stands for Universal Asynchronous Receiver/Transmitter, and a SoftUART is a version of it implemented in code rather than in dedicated hardware.
Can I use SoftwareSerial and the Serial Monitor at the same time?
Yes. The Serial Monitor uses the hardware serial port, while SoftwareSerial uses the pins you choose. This lets you view data from a connected device on your computer while the program runs.
What baud rate should I use?
Use whatever rate your connected device expects, and make sure both sides match. If you have a choice, 9600 is a dependable starting point for software serial. Higher rates may work, but they are more likely to cause errors.
Why am I getting garbled text?
The usual causes are a baud rate mismatch, TX and RX wired straight instead of crossed, a missing common ground, or a speed too high for software serial to handle reliably.