A library for emulating UART functionality in software on any available digital IO pins. Use this tag for questions regarding SoftwareSerial.

Introduction

SoftwareSerial uses interrupts and carefully timed sequences to provide aynsc serial communications on ports other than those supported by the hardware serial (HardwareSerial class).

You should not use SoftwareSerial if HardwareSerial is available on the same pins. Hardware serial is more robust, and can send and receive in the background. SoftwareSerial cannot.


Supported pins

On the Atmega328P-based Arduinos (eg. Uno, Pro, Nano, Mini) you can use SoftwareSerial on any pins.

On the Mega2560 you can only use it for receiving on pins 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

On the Leonardo and Micro you can only use it for receiving on pins 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

This is because SoftwareSerial requires pin-change interrupts to detect receiving a new byte, and pin-change interrupts are only supported on the pins mentioned above. You do not need pin-change interrupts to transmit, so you can choose any pin for transmitting.


Limitations

  • For timing reasons, SoftwareSerial disables interrupts once a byte has started to be received (until it has been completely received). It also disables interrupts whilst sending.

    Thus you cannot both send, and receive, at the same time, using SoftwareSerial.

  • SoftwareSerial only deals with 8-bit bytes. HardwareSerial can read and write from 5 to 9 bits.

  • SoftwareSerial only reads/writes with no parity and one stop bit. HardwareSerial can write with none/even/odd parity, and use one or two stop bits.

  • Because interrupts are turned off during sending and receiving you cannot do as much background processing whilst sending/receiving.


Enhancements

  • SoftwareSerial has an "inverted logic" mode on the constructor, which lets you read/write with the normal logic inverted. That is, +5V is a 0-bit, and 0V is a 1-bit.

Usefulness

It is handy to use SoftwareSerial where you need to access multiple serial devices, such as a GPS and a printer at the same time.


Alternatives

  • On the Mega2560 you have four hardware serial ports, so you can use those instead of SoftwareSerial.

  • There is another library: AltSoftSerial Library. This removes some of the above limitations, and adds some of its own.


Reference

history | excerpt history