skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home Products Teensy Blog Forum
You are here: Teensy Teensyduino Libraries SoftwareSerial

PJRC Store
Teensy 3.6, $29.25
Teensy 3.5, $24.25
Teensy 3.2, $19.80
Teensy LC, $11.65
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
Teensy
Main Page
Hardware
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

SoftwareSerial Library

Download: SoftwareSerial is included with Arduino

Real UART Serial

The hardware UART Serial port should always be used, if possible, before considering SoftwareSerial. On Teensy, the hardware UART serial port completely separate from the USB port.

On Arduino boards, the main hardware serial port is used for programming and sending messages to the Arduino Serial Monitor. Many projects and website may advise you to use SoftwareSerial or NewSoftSerial to communicate with serial devices, like GPS modules or Modbus controllers. This is necessary on Arduino Uno, because there is no unused hardware UART serial port.

Teensy does have a hardware UART serial port available. Using the real hardware UART serial port provides much better performance and avoids SoftwareSerial's many issues.

Arduino 1.0 and Later

The old, buggy SoftwareSerial was replaced by Mikal Hart's NewSoftSerial in Arduino 1.0.

However, NewSoftSerial was renamed to SoftwareSerial. Please refer to the NewSoftSerial page for use with Arduino 1.0.

Arduino 0023 and Earlier

Do Not Use SoftwareSerial
on Arduino 0022 or 0023

Prior to Arduino 1.0, SoftwareSerial does not handle interrupts properly. Any interrupts, from the normal timer0 (used for millis, delay, micros) to any other libraries in use, can cause corrupted data. SoftwareSerial has poor timing, with incorrect hard-coded assumptions about the speed of digitalWrite. On Teensy, the maximum working baud rate is 19200. SoftwareSerial does not support available(), and can only receive data when read() is called, which severly limits its usefulness for many applications. If you do use SoftwareSerial, the transmit pin is not set to output mode automatically. You must use pinMode to set it.

NewSoftSerial corrects the problems present in SoftwareSerial. If you must must emulate a serial port using Arduino 0022 or 0023, use NewSoftSerial.

Example Program


/*  SoftwareSerial example (modified for Teensy++ pin numbers)
  http://arduino.cc/en/Reference/SoftwareSerialExample

  Sample of the SoftwareSerial library.  Listens for serial in on rxPin
  and sends it out again on txPin.

  by Tom Igoe
  based on examples by David Mellis and Heather Dewey-Hagborg
  written: 6 Jan 2007
*/

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 27
#define txPin 0
#define ledPin 6

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello World - SoftwareSerial");
}

void loop() {
  // listen for new serial coming in:
  char someChar = mySerial.read();
  // print out the character:
  mySerial.print(someChar);
  // toggle an LED just so you see the thing's alive.  
  // this LED will go on with every OTHER character received:
  toggle(ledPin);
}

void toggle(int pinNum) {
  // set the LED pin using the pinState variable:
  digitalWrite(pinNum, pinState); 
  // if pinState = 0, set it to 1, and vice versa:
  pinState = !pinState;
}

Details

For more details, please refer to the official SoftwareSerial page.