skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino UART Serial

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Teensy
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

Using the UART (Real Serial Port)

The Teensy and Teensy++ contain a UART which is the same as the serial port on a standard Arduino. However, it is not used for programming and can be used independantly of the USB Serial object.

Hardware

Serial PortSignalTeensy
1.0
Teensy
2.0
Teensy++
1.0 & 2.0
Teensy
3.0 & 3.1
Serial1ReceivePin 2Pin 7Pin 2Pin 0
TransmitPin 3Pin 8Pin 3Pin 1
Serial2Receive---Pin 9
Transmit---Pin 10
Serial3Receive---Pin 7
Transmit---Pin 8

The serial signals on the Rx and Tx pins are "TTL level". To convert to RS-232 level a MAX232 or similar chip is needed.

TODO: XBee

TODO: Bluetooth hardware example - using "BlueSMiRF" module

TODO: Modbus RS-485 example (see transmitterEnable below)

TODO: Show 2 Teensy boards connected to each other

Example Code

This simple example shows how to use both the UART and USB Serial at the same time. Both are monitored for incoming bytes, and when either receives data, the results are printed to both.
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1

void setup() {
	Serial.begin(9600);
        HWSERIAL.begin(9600);
}

void loop() {
        int incomingByte;
        
	if (Serial.available() > 0) {
		incomingByte = Serial.read();
		Serial.print("USB received: ");
		Serial.println(incomingByte, DEC);
                HWSERIAL.print("USB received:");
                HWSERIAL.println(incomingByte, DEC);
	}
	if (HWSERIAL.available() > 0) {
		incomingByte = HWSERIAL.read();
		Serial.print("UART received: ");
		Serial.println(incomingByte, DEC);
                HWSERIAL.print("UART received:");
                HWSERIAL.println(incomingByte, DEC);
	}
}


Serial Object Names

On Teensy 2.0, "Serial1" accesses the serial port.

On Teensy 3.0, "Serial1", "Serail2", and "Serial3" access the 3 serial ports.

Standard Serial Functions

All of the standard Serial functions are supported.

Serial1.begin(baud)

Initialize the Uart object. The baud rate must be given.

Serial1.print(data) and Serial1.println(data)

Print a number or string. Serial1.print() prints only the number or string, and Serial1.println() prints it with a newline character.
 
  // Serial1.print() can print many different types
  int number = 1234;
  Serial1.println("string");     // string
  Serial1.println('a');          // single character
  Serial1.println(number);       // number (base 10 if 16 or 32 bit)
  Serial1.println(number, DEC);  // number, base 10 (default)
  Serial1.println(number, HEX);  // number, base 16/hexidecimal
  Serial1.println(number, OCT);  // number, base 8/octal
  Serial1.println(number, BIN);  // number, base 2/binary
  Serial1.println(3.14);         // number in floating point, 2 digits

Serial1.write(byte)

Transmit a byte.

Serial1.available()

Returns true if at least 1 byte is available, or false if nothing has been received.

Serial1.read()

Read 1 byte (0 to 255), if available, or -1 if nothing available. Normally Serial1.read() is used after Serial1.available(). For example:

 
  if (Serial1.available()) {
    incomingByte = Serial1.read();  // will not be -1
    // actually do something with incomingByte
  }

In 9 bit mode, the return value is 0 to 511, or -1 if nothing available.

Serial1.flush()

Wait for any transmitted data still in buffers to actually transmit. If no data is waiting in a buffer to transmit, flush() returns immediately.

Serial1.clear()

Discard any received data that has not been read.

Serial1.transmitterEnable(pin)

Use a pin to automatically enable a RS-485 transceiver chip. This pin outputs a logic HIGH when transmitting data. It goes LOW after the last stop bit, to allow reception when not transmitting.

Most RS-485 chips have 2 control signals, an active-high transmitter enable and an active-low receiver enable. Normally those 2 signals are wired together and connected to the transmitter enable output.

Serial1.begin(baud, format)

Initialize the serial object with a baud rate and data format. Supported only on Teensy 3.0 and 3.1.

Format NameData BitsParityStop BitsRX PolarityTX Polarity
SERIAL_7E17EvenNormalNormal
SERIAL_7O17Odd
SERIAL_8N18None
SERIAL_8N28None
SERIAL_8E18Even
SERIAL_8O18Odd
SERIAL_7E1_RXINV7EvenInvertedNormal
SERIAL_7O1_RXINV7Odd
SERIAL_8N1_RXINV8None
SERIAL_8N2_RXINV8None
SERIAL_8E1_RXINV8Even
SERIAL_8O1_RXINV8Odd
SERIAL_7E1_TXINV8EvenNormalInverted
SERIAL_7O1_TXINV8Odd
SERIAL_8N1_TXINV8None
SERIAL_8N2_TXINV8None
SERIAL_8E1_TXINV8Even
SERIAL_8O1_TXINV8Odd
SERIAL_7E1_RXINV_TXINV7EvenInvertedInverted
SERIAL_7O1_RXINV_TXINV7Odd
SERIAL_8N1_RXINV_TXINV8None
SERIAL_8N2_RXINV_TXINV8None
SERIAL_8E1_RXINV_TXINV8Even
SERIAL_8O1_RXINV_TXINV8Odd
The following formats are available when
HardwareSerial.h is edited with SERIAL_9BIT_SUPPORT
SERIAL_9N19NoneNormalNormal
SERIAL_9E19Even
SERIAL_9O19Odd
SERIAL_9N1_RXINV9NoneInvertedNormal
SERIAL_9E1_RXINV9Even
SERIAL_9O1_RXINV9Odd
SERIAL_9N1_TXINV9NoneNormalInverted
SERIAL_9E1_TXINV9Even
SERIAL_9O1_TXINV9Odd
SERIAL_9N1_RXINV_TXINV9NoneInvertedInverted
SERIAL_9E1_RXINV_TXINV9Even
SERIAL_9O1_RXINV_TXINV9Odd

Serial1.write9bit(word)

Transmit 9 bit data. The "word" may have the 9th bit set or cleared. Supported only on Teensy 3.0 & 3.1.

In 7 and 8 bit modes, this function is the same as write(byte).

Usable Baud Rates

Serial communcation can tolerate about 2.5% error. Because the baud rates are created using the CPU clock, which is not an exact multiple of standard baud rates, higher speed baud rates can not be used if the clock is set (in the Tools > CPU Speed menu) to slower speeds.

Intended
Baud Rate
Teensy 3.0
Error at
48 MHz
Teensy 3.0
Error at
24 MHz
Teensy 2.0
Error at
16 MHz
Teensy 2.0
Error at
8 MHz
Teensy 2.0
Error at
4 MHz
Teensy 2.0
Error at
2 MHz
Teensy 2.0
Error at
1 MHz
115200+0.04%-0.08%+2.12%-3.55%+8.51%+8.51%+8.51%
57600-0.02%+0.04%-0.79%+2.12%-3.55%+8.51%+8.51%
38400+0.00%+0.00%+0.16%+0.16%+0.16%-6.99%+8.51%
31250 (MIDI)+0.00%+0.00%+0.00%+0.00%+0.00%+0.00%+0.00%
19200+0.00%+0.00%+0.16%+0.16%+0.16%+0.16%-6.99%
9600+0.00%+0.00%+0.16%+0.16%+0.16%+0.16%+0.16%
4800+0.00%+0.00%-0.08%+0.16%+0.16%+0.16%+0.16%
2400+0.00%+0.00%+0.04%-0.08%+0.16%+0.16%+0.16%
1200+0.00%+0.00%-0.02%+0.04%-0.08%+0.16%+0.16%
300+0.00%+0.00%+0.01%+0.01%-0.02%+0.04%-0.08%

Arduino at 57600 Baud - It's Actually 58824

Arduino creates the same baud rates as Teensyduino, except at 57600, where Arduino's error is +2.12%. When communicating with an Arduino board, the combined +2.12% and -0.79% error is too much. Successful communication with Arduino requires 58824 baud.

void setup() {
  Serial1.begin(58824);  // Arduino 57600 is actually 58824
}

Alternately, if you edit the Arduino code to use "Serial.begin(57601)", Arduino will create a baud rate with only -0.79% error, which exactly matches Teensy.

The Arduino bootloader on Duemilanove and Diecimila, and the "8u2" chip on Arduino Uno when set to 57600, are always 58824 baud. These are not easily reconfigured, so setting Teensy to 58824 baud is the best solution if you must communicate with these.