ESP32C2 - understanding the CDC port

MrBean
Posts: 11
Joined: Wed Aug 17, 2022 1:55 pm

ESP32C2 - understanding the CDC port

Postby MrBean » Sat Jun 14, 2025 12:03 pm

I am trying to understand the workings of the CDC port on an ESP32. I am working on a project where the ESP is connected via USB directly to the PC. There is no UART.

The port is set up in the program as follows:

Code: Select all

	mySerial =(Stream*) &(UsbCdcSerial);
	UsbCdcSerial.begin();
	USB.begin();
In PlatformIO/pioarduino, the following has been added to the board profile:

-D ARDUINO_USB_CDC_ON_BOOT=1

The port works, sort of in the Vscode Serial Monirot, but not in anything else.

What happens is that when initially connected to Serial Monitor, it does not respond. Then when RESET is pressed, Serial Monitor shows the connection being lost and re-established again. On re-connection, it responds and transmits data.

The problem is that in any other terminal application (also tried a Python script), the terminal quits as soon as RESET is pressed.

Is there anything else that needs to be added to force the serial connection to open? I have tried this with both Arduino core 2 and Arduino core 3 with the same result.

I am not sure whether this is a PlatfromIO/pioarduino issue, but when I use standard Arduino methods (Serial.begin(115200)) and compile in the Arduino IDE, the serial port works as expected, so I am not sure its a hardware issue. However, as can bee seen in the above snippet, evidently serial is dealt with differently when using CDC methods in the Espressif core.

MrBean
Posts: 11
Joined: Wed Aug 17, 2022 1:55 pm

Re: ESP32C2 - understanding the CDC port

Postby MrBean » Sat Jun 14, 2025 2:54 pm

I have been able to replicate the problem with a simple sketch in the Arduino IDE:

This, using the well known Arduino method works (some of the time):

Code: Select all

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println("Hello");
  delay(1000);
}
This, using the USB CDC library doesn't give any output and replicates the problem:

Code: Select all

#include <USB.h>

USBCDC USBSerial;

void setup() {
  // put your setup code here, to run once:
  USBSerial.begin();
  USB.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  USBSerial.println("Hello :-)");
}
Regardless, neither option provides a stable and consistent serial port connection.

Who is online

Users browsing this forum: ChatGPT-User, meta-externalagent, Qwantbot and 2 guests