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, $18
Teensy Pins, $21
Teensy++, $24
Teensy++ Pins, $27
USB Cable, $5
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.

TODO: photo of breakout board with serial port and both serial and usb cable attached

Teensy 1.0: Pin 2 is the receive signal and Pin 3 is the Transmit signal.
Teensy 2.0: Pin 7 is the receive signal and Pin 8 is the Transmit signal.
Teensy++ 1.0: Pin 2 is the receive signal and Pin 3 is the Transmit signal.
Teensy++ 2.0: Pin 2 is the receive signal and Pin 3 is the Transmit signal.

TODO: diagram showing pins 2 and 3

TODO: schematic with basic MAX232 wiring

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.