ArduinoCore-renesas icon indicating copy to clipboard operation
ArduinoCore-renesas copied to clipboard

[Uno R4 WiFi] `while (!Serial);` not working on Uno R4 WiFi

Open swanjeng opened this issue 5 months ago • 3 comments

I don't know whether it should be put here or other Github repos about Arduino Uno R4 WiFi.

Summary:

On Arduino Uno R4 WiFi, while (!Serial); does not block the program until the Serial Monitor is connected, making typical setup() serial prints disappear unless the user manually resets the board. This behavior differs from most other Arduino boards (including Uno R3 and Uno R4 Minima), breaking a large amount of existing example code and tutorials.

Steps to Reproduce:

  1. Upload this sketch to an Arduino Uno R4 WiFi:
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Hello World");
}
void loop() {

}
  1. Open Serial Monitor immediately after upload finishes.

  2. Observe that no output appears unless the reset button is pressed.

Expected Behavior:

while (!Serial); should block the program until the USB–Serial connection is established, ensuring prints in setup() are visible right after upload, without requiring a manual reset.

Actual Behavior:

while (!Serial) returns immediately. Output sent before Serial Monitor connection is lost and manual reset is required to see the message.

Impact:

Breaks compatibility with most Arduino examples and learning materials.

Confuses new users, who expect immediate output after upload.

Makes debugging output in setup() unreliable without additional delay hacks.

The issue has been reported in this Arduino Forum thread: https://forum.arduino.cc/t/while-serial-not-sufficient-on-upload-or-usb-plugin/1168429

swanjeng avatar Aug 15 '25 08:08 swanjeng

Here is a video showing the issue.

https://github.com/user-attachments/assets/69ca5798-a70c-4385-a2a4-5bedc84b63dd

code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) delay(1);
  Serial.println("Hello~");
  for (int i = 1 ; i <= 200 ; i ++) Serial.println("i = " + String(i));
  pinMode(13, OUTPUT);
  Serial.println("Starts blinking LED");
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

swanjeng avatar Aug 17 '25 02:08 swanjeng

Hi @swanjeng , the while (!Serial) pattern was introduced to allow in fact USB native ports to enumerate before trying to print (on nowhere). On native UART-to-USB like UNO R4 WiFi and UNO R3 the !Serial pattern is essentially a no-op. The reason why UNO R3 works is that it gets reset (by design) every time you open the serial port/change the baud rate. The fix should be applied to the serial-monitor project to make it faster at reopening the port after an upload @cmaglie

facchinm avatar Aug 21 '25 09:08 facchinm

So what should I do next? Post this to another repository which is more suitable or just wait?

swanjeng avatar Oct 22 '25 07:10 swanjeng