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 Search PJRC

PJRC Store
Teensy, $16
Teensy Pins, $19
Teensy++, $24
Teensy++ Pins, $27
USB Cable, $4
Teensy
Main Page
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

SignalTeensy 1.0Teensy 2.0Teensy++ 1.0Teensy++ 2.0
Receive (Rx)Pin 2Pin 7Pin 2Pin 2
Transmit (Tx)Pin 3Pin 8Pin 3Pin 3

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: Bluetooth hardware example - using "BlueSMiRF" module

TODO: Modbus RS-485 example

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.
 
// This line defines a "Uart" object to access the serial port
HardwareSerial Uart = HardwareSerial();

void setup() {
	Serial.begin(9600);
        Uart.begin(38400);
}

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

Instantiate Uart Object

To use the Uart object, you must add line to your program which creates the Uart object, of type HardwareSerial(). Simply copy this line to the beginning of your program, before any place you use Uart.
  // This line defines a "Uart" object to access the serial port
  HardwareSerial Uart = HardwareSerial();
If you prefer a different name, you can replace "Uart" with any name, such as "Serial1", "RealSerialPort", or "WhateverYouLike".

Standard Serial Functions

All of the standard Serial functions are supported.

Uart.begin()

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

Uart.print() and Uart.println()

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

Uart.write()

Transmit a byte.

Uart.available()

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

Uart.read()

Read 1 byte (0 to 255), if available, or -1 if nothing available. Normally Uart.read() is used after Uart.available(). For example:
 
  if (Uart.available()) {
    incomingByte = Uart.read();  // will not be -1
    // actually do something with incomingByte
  }

Uart.flush()

Discard any received data that has not been read.

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
Error at
16 MHz
Error at
8 MHz
Error at
4 MHz
Error at
2 MHz
Error at
1 MHz
115200+2.12%-3.55%+8.51%+8.51%+8.51%
57600-0.79%+2.12%-3.55%+8.51%+8.51%
38400+0.16%+0.16%+0.16%-6.99%+8.51%
31250 (MIDI)+0.00%+0.00%+0.00%+0.00%+0.00%
19200+0.16%+0.16%+0.16%+0.16%-6.99%
9600+0.16%+0.16%+0.16%+0.16%+0.16%
4800-0.08%+0.16%+0.16%+0.16%+0.16%
2400+0.04%-0.08%+0.16%+0.16%+0.16%
1200-0.02%+0.04%-0.08%+0.16%+0.16%
300+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.

HardwareSerial Uart = HardwareSerial();
void setup() {
  Uart.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.