Sub topic 9
Extra Serial Ports: Hardware UARTs and SoftwareSerial
Prerequisites and the main choice
This chapter assumes you know the Arduino setup() and loop() functions, bytes, and basic TX/RX wiring. UART means Universal Asynchronous Receiver/Transmitter. It converts bytes into serial bits and converts received bits back into bytes.
Many Arduino boards provide one or more hardware UARTs. A hardware UART is a circuit inside the microcontroller that handles timing, start bits, stop bits, and shifting data. Serial is the Arduino object connected to the board's first hardware UART.
When you need another serial connection, you have two main choices:
| Option | Strength | Limitation |
|---|---|---|
| Hardware UART | Reliable timing and full-duplex communication | Available pins and UARTs depend on the board |
| `SoftwareSerial` | Can create a port on chosen digital pins | Uses the CPU and has stricter timing limits |
Full-duplex means the device can transmit and receive at the same time.
Hardware UARTs on common boards
On an Arduino Uno or Nano, Serial normally uses digital pins 0 and 1. Pin 0 is RX, or receive. Pin 1 is TX, or transmit. These pins are also connected to the USB interface used by the Serial Monitor. Connecting another device there can interfere with uploading sketches or displaying debug output.
An Arduino Mega has several hardware UARTs:
Serialuses pins 0 and 1 and the USB connection.Serial1uses pins 19 (RX1) and 18 (TX1).Serial2uses pins 17 (RX2) and 16 (TX2).Serial3uses pins 15 (RX3) and 14 (TX3).
The exact pin mapping varies by board. Check the board's pinout before wiring. Hardware UART objects usually have the same API as Serial, including begin(), available(), read(), and write().
This Mega sketch keeps the USB connection for debugging while forwarding data between the computer and a device on Serial1:
void setup() {
Serial.begin(115200); // USB serial connection
Serial1.begin(9600); // External device
Serial.println(F("Bridge ready"));
}
void loop() {
while (Serial.available() > 0) {
Serial1.write(Serial.read());
}
while (Serial1.available() > 0) {
Serial.write(Serial1.read());
}
}Connect the external device's TX to the Mega's RX1 pin, its RX to TX1, and connect their grounds. Both devices must also use compatible voltage levels and the same baud rate.
SoftwareSerial
SoftwareSerial creates a serial port by changing digital pins at carefully timed moments. The library is useful on boards with one hardware UART when that UART is already needed for USB debugging.
The constructor arguments are in RX, TX order. This order is easy to confuse because wiring crosses the signals:
#include <SoftwareSerial.h>
SoftwareSerial sensorPort(10, 11); // Arduino RX, Arduino TX
void setup() {
Serial.begin(115200);
sensorPort.begin(9600);
}
void loop() {
while (sensorPort.available() > 0) {
Serial.write(sensorPort.read());
}
while (Serial.available() > 0) {
sensorPort.write(Serial.read());
}
}For this sketch, connect the external device's TX to pin 10 and its RX to pin 11. The comment describes the Arduino's point of view. The external device sees the opposite direction.
SoftwareSerial can receive on only one port at a time. Some library versions let you call listen() to select the active receiver, but bytes arriving on another SoftwareSerial port may be missed. Transmission also occupies the processor, so timing-sensitive code, interrupts, and high data rates can cause errors.
Choosing and troubleshooting a port
Use a hardware UART whenever the board provides one and the application needs continuous traffic, higher baud rates, or dependable full-duplex operation. Use SoftwareSerial for slow devices that send occasional messages, such as simple GPS, Bluetooth, or sensor modules.
Common errors include:
- Reversing the constructor arguments.
SoftwareSerial(rx, tx)names the Arduino pins. - Connecting TX to TX or RX to RX instead of crossing the signals.
- Forgetting the shared ground.
- Selecting pins that do not support the library correctly on the chosen board.
- Using a baud rate that is too high for SoftwareSerial.
- Expecting two SoftwareSerial receivers to work simultaneously.
- Connecting incompatible voltage levels. A 5 V output can damage a 3.3 V input without suitable level conversion.
Exercise
Exercise
An Uno must read a device at 9600 baud while sending debug text to the Serial Monitor. Which approach is suitable, and what pin mapping should you use for SoftwareSerial on pins 10 and 11?
Show answer
Use SoftwareSerial so the hardware Serial port remains available for debugging. Construct it as SoftwareSerial devicePort(10, 11), where pin 10 is Arduino RX and pin 11 is Arduino TX. Connect the device TX to pin 10, the device RX to pin 11, and connect the grounds.