How to use Serial1 with XIAO ESP32C3

I want to use Serial1.
At first I used sample code for multi serial,following below.
But, It doesn’t work.(Seral1 Tx : D6 Rx:D7)

Is there any settings or others.

void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}

// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
}

2 Likes

I found the same problem, when i flash this into XIAO ESP32, then the OS can’t recognize the COM. evenif i use another FTD32 USB-COM board and connect to D6/D7, can’t flash program.

Luckily, I was able to use USB and UART0 at the same time. Of course, USB CDC on Boot is set to Enable. So I’ll share how I did it.

ESP-ROM:esp32c3-api1-20210207 Build:Feb 7 2021
Arduino core for the ESP32 v2.05
Arduino IDE 2.0.2

Note
In case of “CPU Frequency:160MHz”, it work correct, but in case of “CPU Frequency:40MHz”, it works at half the speed of what you set. This seems to be the cause.

/*   Multiple Serial test for XIAO ESP32C3 */
HardwareSerial MySerial(0);   //Create a new HardwareSerial class.

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  MySerial.begin(115200, SERIAL_8N1, RX, TX); // at CPU Freq is 40MHz, work half speed of defined.

}

void loop() {
  // read from port 1, send to port 0:
  if (MySerial.available()) {
    String str  = MySerial.readStringUntil('\r');
    Serial.println(str);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    String str = Serial.readStringUntil('\r');
    MySerial.println(str);
  }

}

RX and TX is defined in arduino-esp32/variants/XIAO_ESP32C3/pins_arduino.h as below.

static const uint8_t TX = 21;
static const uint8_t RX = 20;
/* */
static const uint8_t D6 = 21;
static const uint8_t D7 = 20;
3 Likes

There’s actually three UART devices on this board!

The USB CDC Serial-overUSB, and two hardware UART devices. These two devices can be allocated to any available GPIO ports you want.

By default, UART0 is mapped to TX=D6, RX=D7.

To use both, the following code should work.

// Need this for the lower level access to set them up.
#include <HardwareSerial.h>

//Define two Serial devices mapped to the two internal UARTs
HardwareSerial MySerial0(0);
HardwareSerial MySerial1(1);

void setup()
{
    // For the USB, just use Serial as normal:
    Serial.begin(115200);

    // Configure MySerial0 on pins TX=6 and RX=7 (-1, -1 means use the default)
    MySerial0.begin(9600, SERIAL_8N1, -1, -1);
    MySerial0.print("MySerial0");

    // And configure MySerial1 on pins RX=D9, TX=D10
    MySerial1.begin(115200, SERIAL_8N1, 9, 10);
    MySerial1.print("MySerial1");
}

You will need to do some special handling on the MySerial0 port as it gets some diagnostic messages sent to it on boot and if it crashes etc. I’m using it for a LoraWAN modem, so its fairly immune as it won’t react to anything except AT commands. I believe you can stop this with fuse bits, but I haven’t dug that deep yet.

4 Likes

Thanks guys
I 've been able to use several serial ports on XIAO ESP32.

Thanks @tfuruha
This works and I got more clarity for using multiple serial ports