SAMD21 Mini/Dev Breakout Hookup Guide

Pages
Contributors: Jimb0
Favorited Favorite 3

Example Sketch: 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)
SerialHardware Serial Port 03130
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.)

Also, the Serial USB port isn’t broken out on the Mini Breakout. If you want to use the pins 0/1 serial port, you’ll need to replace Serial calls with Serial1.

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 FTDI Basic or any other serial device to the SAMD21’s pins 0 (RX) and 1 (TX). Data sent from the FTDI should end up in your Serial Monitor, and data sent to your Serial Monitor will route over to the FTDI.

GPS and serial LCD connected to the SAMD21's hardware UART

To take it even further, try a GPS module to test RX, and a Serial LCD to test TX.