SoftwareSerial Library
Download: SoftwareSerial is included with Teensyduino. Source Code on Github.Use Hardware Serial If Possible
Hardware Serial ports give the most reliable serial communication while using the least CPU resources. If possible, use hardware serial and avoid SoftwareSerial.On Teensy 4.x the FlexIO_t4 library can be used to create additional hardware serial ports.
On Teensy 3.x and Teensy 2.0, AltSoftSerial can create an additional serial port using timers. While not as good as hardware serial, the timers perform better than SoftwareSerial.
SoftwareSerial should be used only as a last resort, because it has many limitations:
- Transmitting can interfere with other libraries which require interrupts. Use of slow baud rates may cause more interference.
- Transmitting can block or interfere with SoftwareSerial reception.
- Use of interrupts by other libraries can interfere with SoftwareSerial reception, especially if using higher baud rates.
- If multiple SoftwareSerial ports are created, only 1 can work at a time.
- Only certain pins can receive on Teensy LC, Teensy 2.0 and Teensy 1.0.
- Teensyduino 1.59 and earlier: Transmitting does not work on Teensy 4.x. This will be fixed in version 1.60.
- Teensyduino 1.59 and earlier: Receiving does not work on Teensy 3.x or 4.x. This will be fixed in version 1.60.
- The inverse_logic option is not supported on Teensy 3.x or 4.x.
Board | Receive Compatible Pins |
---|---|
Teensy 4.x | Any Digital Pin |
Teensy 3.x | Any Digital Pin |
Teensy LC | 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23 |
Teensy++ 2.0 | 20, 21, 22, 23, 24, 25, 26, 27 |
Teensy 2.0 | 0, 1, 2, 3, 4, 13, 14, 15 |
Teensy++ 1.0 | 20, 21, 22, 23, 24, 25, 26, 27 |
Teensy 1.0 | 5, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20 |
|
Automatic Use Of Hardware Serial
On Teensy 4.x and Teensy 3.x, SoftwareSerial will automatically use hardware serial if the 2 pins are the RX and TX pins of a hardware serial port. This feature lets you use programs and libraries hard-coded to require SoftwareSerial, but with the efficient and reliable communication of hardware serial.
#include <SoftwareSerial.h> /* Best for Teensy LC, 3.0, 3.1, 3.2 */ SoftwareSerial mySerial1(0, 1); SoftwareSerial mySerial2(7, 8); SoftwareSerial mySerial3(9, 10); /* Best for Teensy 3.5 & 3.6 */ SoftwareSerial mySerial1(0, 1); SoftwareSerial mySerial2(7, 8); SoftwareSerial mySerial3(9, 10); SoftwareSerial mySerial4(31, 32); SoftwareSerial mySerial5(34, 33); SoftwareSerial mySerial6(47, 48); /* Best for Teensy 4.0 & 4.1 & MICROMOD */ SoftwareSerial mySerial1(0, 1); SoftwareSerial mySerial2(7, 8); SoftwareSerial mySerial3(15, 14); SoftwareSerial mySerial4(16, 17); SoftwareSerial mySerial5(21, 20); SoftwareSerial mySerial6(25, 24); SoftwareSerial mySerial7(28, 29); SoftwareSerial mySerial8(34, 35); /* Teensy 4.1 only */
Basic Usage
SoftwareSerial mySerial(ReceivePin, TransmitPin);Create an instance of SoftwareSerial, using a pair of pins to receive and transmit. The receive pin must be one that supports pin change interrupts. You can create multiple SoftwareSerial ports, each on their own 2 pins, but due to the CPU usage requirements, it's only practical to use one port at a time.
mySerial.begin(baud);Initialize the port to communicate at a specific baud rate.
mySerial.print(anything);Print a number or text. This works the same as Serial.print().
mySerial.available();Returns the number of bytes received, which can be read.
mySerial.read();Reads the next byte from the port. If nothing has been received, -1 is returned.
mySerial.write(byte);Transmit a single byte.
Original SoftwareSerial
For more details, please refer to the official SoftwareSerial page.Early Arduino IDE shipped with a very buggy SoftwareSerial library derived from the Wiring software. In 2011, it was replaced by NewSoftSerial by Mikal Hart, with release of Arduino IDE 1.0. SoftwareSerial for Teensy 1.0 and 2.0 still uses the NewSoftSerial code. The implementation for Teensy 3.x and 4.x is a complete redesign by PJRC.