2

I have a Question regarding Arduino IDE:

Many old Arduino setup examples states that the Serial.begin() function always should begin with :

Serial.begin(9600); // (or 115200) 

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

Now I have read a lot of comments saying that this "Wait-function" is no longer necessary ??

Is this an old routine, that's no longer necessary, or is it still important ???

3 Answers 3

2

That function is not obsolete. However, there is gratuitous use of it where it's not really needed.

The usage of the function that you alude to grew from the desire to make ATMega32U4-based boards (such as the Leonardo) which have a native USB interface act similar to the ATMega328P etc boards.

When you open the serial port on an Uno, for example, the main MCU is reset and the sketch runs from the start. When you do the same on a Leonardo that doesn't happen. So by adding that code snippet to the beginning the sketch is paused until the serial port is opened. This makes it appear that the board has been reset and the sketch is running afresh, like an Uno - but it isn't.

Placing that while loop at the start of your sketch without thinking about the consequences means that your sketch may never run. If a serial connection is required for your program then that's not such a problem. However, if all you're using the serial for is debugging, then as soon as you try and use your sketch without the computer opening the serial port it will fail.

So I would recommend not using that while loop unless you know that you want the effects that it provides. That is, stalling the sketch until the serial port is opened by the computer.

1

This is for the Arduino boards that have native USB to ensure that the serial port is ready before progressing. The while loop is not needed for Arduino boards where the USB is not native to the processor (such as the Arduino Uno).

Indicates if the specified Serial port is ready.

On the boards with native USB, if (Serial) (or if(SerialUSB) on the Due) indicates whether or not the USB CDC serial connection is open. For all other boards, and the non-USB CDC ports, this will always return true.

Ref: Arduino Reference > communication > serial > if(serial)

2
  • How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
    – Rhino
    May 1, 2019 at 15:13
  • yeah, it's only "needed" for a few less-common ardunio boards. every ESP board should be fine without.
    – dandavis
    May 1, 2019 at 17:23
1

Microcontrolers with native USB interface create an USB connection. It takes a little time. The serial prints right after begin() would be lost without waiting or the connection.

For Uno, Mega, Nano, esp8266/32 boards and other boards with a MCU without native USB support, an external USB-to-TTL-Serial converter chip handles the USB connection.

Leonardo, Micro, MKR have an MCU with built-in USB port. Some boards with Zero or M0 in name have only the native USB port, some have a debugger chip which can serve as USB-to-TTL-Serial converter too.

!Serial is a C++ construct using bool() operator. Then implementation is a function which checks if the USB connection is ready.

The waiting for native USB Serial (called SerialUSB for some boards) is used in examples to see the debug prints from setup(). But if USB is not connected, it waits and the sketch doesn't continue. It can be very confusing. So I rather use a delay(500) after Serial.begin() on boards with native USB.

7
  • You could explain some more. What if a Leonardo has that code but then is used with a power supply without usb connector.
    – Jot
    May 1, 2019 at 15:06
  • I am working on it :-)
    – Juraj
    May 1, 2019 at 15:10
  • I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
    – Jot
    May 1, 2019 at 15:40
  • 1
    I'm still chuckling about your Majenko comment @Juraj. :-)
    – st2000
    May 1, 2019 at 15:53
  • 1
    @iPathツ, what is the parameter of this overloaded operator? Where in class of Serial do you see ! overloaded?
    – Juraj
    May 24, 2020 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.