Example Code for Arduino-USB Serial Ports

Last revision 2026/01/14

This article offers an example code for initializing and using multiple serial ports on the Arduino M0, including USB and hardware ports, with instructions for hardware and software setup across different platforms.

Hardware Preparation

Software Preparation

Other Preparation Work

Just like Arduino Leonardo, M0 has USB Serial Port and Hardware Serial Port.

Serial Port Type Serial Object
USB Serial Port Serial
Hardware Serial Port 1 Serial1
Hardware Serial Port 2 Serial2

Tested Platform:

  • Windows: win7 32bit/64bit, win8 32bit/64bit, win10 32bit
  • Linux: ubuntu 12.04 32bit
  • Mac

Sample Code

Init 3 serial ports(115200bps), print specific text cyclically per second.

void setup() {  // put your setup code here, to run once:
    Serial.begin(115200);
    while(!Serial);
    Serial1.begin(115200);
    Serial2.begin(115200);
}
void loop() {   // put your main code here, to run repeatedly:
    Serial.println("I am USB CDC Serial");
    Serial1.println("I am Serial 1");
    Serial2.println("I am Serial 2");
    delay(1000);
}

Result

Three serial ports print respective texts cyclically every 1 second.

Was this article helpful?

TOP