XIAO SAMD21 serial
Two ports at once: Serial on the chip’s own USB, and Serial1 on D6 and D7 with one jumper between them. The whole build, a Seeed XIAO SAMD21, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 5 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $5.40. On Mokxi they're free.
What you'll learn
- Two serial ports on one board
- Using a jumper to loop TX back into RX
- Reading bytes with available and read
- Baud rates on a hardware serial port
- Difficulty: Beginner
- Time: About 5 minutes
- Board: Seeed XIAO SAMD21
- Parts: 1
How it works
This board has two serial ports. Serial is the chip's own USB connection, the one the serial monitor listens to. Serial1 is a real hardware port that sends data out of pin D6 and listens on pin D7. It runs at 9600 baud.
Put one jumper wire between D6 and D7. Anything the board sends out of D6 comes straight back in on D7. At startup it sends one line this way, and you will see it echoed.
Type a character in the serial monitor and the sketch prints it back, then sends it out through Serial1. The same character returns on D7, and the sketch prints "D7 heard" for each one.
Parts list
1 part, plus the jumper wires. They retail for about $5.40; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Seeed XIAO SAMD21 pin D7 | Seeed XIAO SAMD21 pin D6 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Serial: the two ports this board has, and how they differ.
//
// `Serial` is the chip's own USB. It is what the serial monitor is connected
// to, and it has no baud rate of its own: the number below would be passed to
// the host, which is free to ignore it. There is no USB stack in the model, so
// `while (!Serial)` returns at once here; on a real XIAO it waits for the
// host to enumerate the device.
//
// `Serial1` is SERCOM4 on D6 (TX) and D7 (RX): real 8N1 frames on real pins.
// Wire a jumper from D6 to D7 and this sketch hears its own echo.
void setup() {
Serial.begin(115200);
Serial1.begin(9600);
Serial.println("XIAO SAMD21 ready");
Serial.println("type something and press enter");
Serial1.println("and this goes out of D6 at 9600 baud");
}
void loop() {
while (Serial.available()) {
int c = Serial.read();
Serial.print("you typed: ");
Serial.println((char)c);
Serial1.write((uint8_t)c);
}
while (Serial1.available()) {
Serial.print("D7 heard: ");
Serial.println((char)Serial1.read());
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Opens both ports. Serial starts at 115200, though USB serial may ignore that number. Serial1 starts at 9600, and that number really sets the speed on D6 and D7. Then it prints a greeting on USB and sends a test line out of D6.
void setup() {
Serial.begin(115200);
Serial1.begin(9600);
Serial.println("XIAO SAMD21 ready");
Serial.println("type something and press enter");
Serial1.println("and this goes out of D6 at 9600 baud");
}loop sketch.ino
Checks each port for waiting bytes. Anything typed on USB is printed as "you typed" and forwarded out of Serial1. Anything arriving on Serial1 is printed as "D7 heard". With the jumper in place, the two halves complete a loop. A short delay keeps it relaxed.
void loop() {
while (Serial.available()) {
int c = Serial.read();
Serial.print("you typed: ");
Serial.println((char)c);
Serial1.write((uint8_t)c);
}
while (Serial1.available()) {
Serial.print("D7 heard: ");
Serial.println((char)Serial1.read());
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- You never see any "D7 heard" lines This project
The jumper is missing or on the wrong pins. It must join D6 to D7. - Garbled text from the second port This project
Both ends of a serial link must use the same baud rate. HereSerial1.begin(9600)sets it. - A 5 V signal damages a pin. Seeed XIAO SAMD21
The XIAO SAMD21 is 3.3 V and its pins are not 5 V tolerant. Level shift 5 V sensors. The 5V pad is the USB supply, not an output pin. - The serial output is missing, or the upload fails. Seeed XIAO SAMD21
Serial is the USB port, and Serial1 is on D6 (TX) and D7 (RX). To recover the board, short RST to GND twice quickly to enter the bootloader, then upload.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.