RedBoard Turbo Hookup Guide

Pages
Contributors: Alex the Giant
Favorited Favorite 2

Example: Serial Ports

One of the SAMD21's most exciting features is SERCOM -- its multiple, configurable serial ports. The Arduino IDE equips the SAMD21 with two hardware serial ports, by default, plus a third "USB serial port" for communicating between the serial monitor.

Each of these serial ports has a unique Serial object which you'll refer to in code:

Serial ObjectSerial PortRX PinTX Pin
SerialUSBUSB Serial (Serial Monitor)
Serial1Hardware Serial Port 101

There are a couple critical things to notice here. First of all, if you're trying to use the Serial Monitor to debug, you'll need to use SerialUSB.begin(<baud>) and SerialUSB.print(). (Thankfully find/replace exists for adjusting example code.)

Here's a quick example demonstrating the differences between Serial Monitor and Serial1. It is designed to route data from Serial1 to the Serial Monitor, and vice-versa.

language:c
void setup()
{
  SerialUSB.begin(9600); // Initialize Serial Monitor USB
  Serial1.begin(9600); // Initialize hardware serial port, pins 0/1

  while (!SerialUSB) ; // Wait for Serial monitor to open

  // Send a welcome message to the serial monitor:
  SerialUSB.println("Send character(s) to relay it over Serial1");
}

void loop()
{
  if (SerialUSB.available()) // If data is sent to the monitor
  {
    String toSend = ""; // Create a new string
    while (SerialUSB.available()) // While data is available
    {
      // Read from SerialUSB and add to the string:
      toSend += (char)SerialUSB.read();
    }
    // Print a message stating what we're sending:
    SerialUSB.println("Sending " + toSend + " to Serial1");

    // Send the assembled string out over the hardware
    // Serial1 port (TX pin 1).
    Serial1.print(toSend);
  }

  if (Serial1.available()) // If data is sent from device
  {
    String toSend = ""; // Create a new string
    while (Serial1.available()) // While data is available
    {
      // Read from hardware port and add to the string:
      toSend += (char)Serial1.read();
    }
    // Print a message stating what we've received:
    SerialUSB.println("Received " + toSend + " from Serial1");
  }
}

Then try typing something into the serial monitor. Even with nothing connected to the hardware serial port, you should see what you typed echoed back at you.

Example serial monitor

You can further test this sketch out by connecting an 3.3V FTDI Basic or any other serial device to the SAMD21's pins 0 (RX) and 1 (TX). By opening up a serial terminal, any data sent from the FTDI should end up in your Arduino Serial Monitor, and data sent to your Arduino Serial Monitor will route over to the FTDI. Here's a table that shows what pins to connect together.

RedBoard Turbo Pins 3.3V FTDI (or any USB-to-Serial Converter) Pins
1/TX RXI
0/RX TXO
GND GND