Skip to main content

Softwareserial.h Library [better] May 2026

// Periodically check Bluetooth (non-blocking) if (bluetooth.available()) char cmd = bluetooth.read(); if (cmd == 'G') gps.listen(); // Switch back to GPS

SoftwareSerial port1(2, 3); SoftwareSerial port2(4, 5); void setup() port1.begin(9600); port2.begin(9600); port1.listen(); // port1 is active softwareserial.h library

void checkForData(SoftwareSerial &ss) ss.listen(); if (ss.available()) // Process // Periodically check Bluetooth (non-blocking) if (bluetooth

// Switch to port2 briefly port2.listen(); if (port2.available()) // Process port2 data void setup() Serial

The classic Arduino Uno, Nano, and Mega 2560 (for its first few ports) have dedicated hardware UARTs (Universal Asynchronous Receiver-Transmitters). However, hardware UARTs are limited in number. Once you connect a GPS module, a Bluetooth module, and a debug console simultaneously, you run out of ports.

void setup() Serial.begin(9600); // Debug console gps.begin(9600); bluetooth.begin(38400); // Common HC-05 default gps.listen(); // Listen to GPS first

You can create multiple instances, but only one can receive at a time . The active receiver is set by the last call to listen() .