Softwareserial.h Library May 2026
Bidirectional Communication with Collision Avoidance Since SoftwareSerial is half-duplex, implement a simple protocol:
SoftwareSerial port1(2, 3); SoftwareSerial port2(4, 5); void setup() port1.begin(9600); port2.begin(9600); port1.listen(); // port1 is active softwareserial.h library
#include <SoftwareSerial.h> For ESP8266/ESP32, note that they have a different SoftwareSerial implementation (often renamed or not available due to better hardware serial options). Constructor SoftwareSerial mySerial(RX_pin, TX_pin); // Example: RX on pin 10, TX on pin 11 SoftwareSerial gpsSerial(10, 11); Essential Methods | Method | Description | |--------|-------------| | begin(baud) | Initializes the software serial port. | | available() | Returns number of bytes ready to read. | | read() | Reads one byte (-1 if none). | | write(data) | Sends a byte (or string via print() / println() ). | | listen() | Enables this port for reception (if multiple ports exist). | | isListening() | Checks if this port is active. | | overflow() | Returns true if data was lost due to buffer overflow (64-byte buffer). | Simple Example: GPS Module #include <SoftwareSerial.h> SoftwareSerial gps(10, 11); // RX=10, TX=11 | | read() | Reads one byte (-1 if none)
void setup() Serial.begin(9600); // Debug console gps.begin(9600); bluetooth.begin(38400); // Common HC-05 default gps.listen(); // Listen to GPS first | | isListening() | Checks if this port is active
void sendString(SoftwareSerial &ss, char *msg) ss.listen(); // Ensure we're not receiving delay(1); // Allow any pending RX to finish ss.print(msg);
// Switch to port2 briefly port2.listen(); if (port2.available()) // Process port2 data
void loop() // Check GPS if (gps.available()) char c = gps.read(); Serial.print(c); // Show on debug bluetooth.print(c); // Forward to Bluetooth