Serial.print() in setup bug?

Hello everyone! So after some youtube suggestions from electronic gurus I have decided to try platformio for my ESP32S3 from seed xiao. Everything went smooth before I stumbled across Serial Monitor. It looks like platformio’s Serial Monitor cannot print anything inside setup part which is a bummer. I have tried all possible solutions related to this problem discussed online: some of them were code with delay(1000 or more) instead of while (!Serial) and without delay. Changed bunch of things in platform.ini like added flags disabled/enabled dtr, rts, changed monitor speed, played with build flags etc, etc. I am not sure if I am looking at the right spot, but my intuition tells me it is a bug. Can anyone confirm or deny it? Here is a simple code I was playing with:

#include <Arduino.h>

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

  while (!Serial) {
  ; // Wait for serial port to connect
  }

  Serial.println();
  Serial.print("Print in void setup(): ");
}

void loop() {

  Serial.println("--l--o--o--p--");

  delay(2000);
}

The output looks like that:
— Terminal on COM4 | 115200 8-N-1
— Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
— More details at Redirecting...
— Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
–l–o–o–p–
–l–o–o–p–
–l–o–o–p–
–l–o–o–p–
–l–o–o–p–

Note the same code works fine in arduinoIDE
Appreciate any help.

Set

build_flags =
  -DARDUINO_USB_CDC_ON_BOOT=1

See ESP32-S3 native USB interface and Serial Monitor missing first messages - #10 by sivar2311

This board is using the natvie USB port of the ESP32-S3. The native USB gets disconnected and reconnects when the ESP32-S3 reboots.

Did not help. Unfortunately still getting the same output. Here is my platform.ini
[env:seeed_xiao_esp32s3]

platform = espressif32

board = seeed_xiao_esp32s3

framework = arduino

monitor_speed = 115200

build_flags =

-DARDUINO_USB_CDC_ON_BOOT=1

Again: You are using the native built-in USB port of the ESP32-S3.
This USB port gets disconnected and will reconnect when the ESP32-S3 resets. This takes some time!

Using a sketch like this:

#include <Arduino.h>

void setup() {
    Serial.begin(115200);
    Serial.println("Hello World");
}

void loop() {
    Serial.print("alive...");
    Serial.println(millis());
    delay(1000);
}

Results in:

Hard resetting via RTS pin...
--- Terminal on COM10 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
alive...1030
alive...2030
alive...3030
alive...4030
alive...5030
alive...6030

As you can see, you need a delay of at least 1030 ms (in this case).

Adding a delay(2000)' after Serial.begin(…` to be on the safe side:

#include <Arduino.h>

void setup() {
    Serial.begin(115200);
    delay(2000);
    Serial.println("Hello World");
}

void loop() {
    Serial.print("alive...");
    Serial.println(millis());
    delay(1000);
}

Results in

Hard resetting via RTS pin...
--- Terminal on COM10 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
Hello World
alive...2030
alive...3030
alive...4030

The “Hello World” now gets printed.

So there is no bug.

Ok, that finally helped! It looks like the delay in my case is significant and should be set at least delay(5000) in setup, then “Hello World” posts. Now it makes sense. Thank you @sivar2311 for helping troubleshoot the case.

1 Like