Arduino-IDE “while (!Serial)”ESP8266 & websocketsArduino IDE get stuck while uploading sketchESP8266 Upload hex file Arduino IDEArduino IDE - Start Serial Monitor once upload is completeRTL8170 Web Server - client unable to connectSend serial commands through IDE while RX pin is in useESP32 in Arduino-IDE with FS.h and SPIFFSDuplicate libraries while compiling for ESP32 in the Arduino IDEAre lambdas a good way to improve readability in this instance?Serial Port not recognized by Arduino IDE
How do I get past a 3-year ban from overstay with VWP?
Control variables and other independent variables
Understanding basic photoresistor circuit
"Right on the tip of my tongue" meaning?
How can this pool heater gas line be disconnected?
Why was this sacrifice sufficient?
Was there ever any real use for a 6800-based Apple I?
Why does the Earth follow an elliptical trajectory rather than a parabolic one?
How do I tell my supervisor that he is choosing poor replacements for me while I am on maternity leave?
Early arrival in Australia, early hotel check in not available
What does this quote in Small Gods refer to?
Will change of address affect direct deposit?
How to align underlines in a cases environment
Can 'sudo apt-get remove [write]' destroy my Ubuntu?
Is it a bad idea to replace pull-up resistors with hard pull-ups?
How to make the table in the figure in LaTeX?
The lexical root of the perfect tense forms differs from the lexical root of the infinitive form
find not returning expected files
Ex-manager wants to stay in touch, I don't want to
How are Core iX names like Core i5, i7 related to Haswell, Ivy Bridge?
Two researchers want to work on the same extension to my paper. Who to help?
What does i386 mean on macOS Mojave?
What is the airplane type in this formation seen above Eugene, Oregon?
Exception propagation: When to catch exceptions?
Arduino-IDE “while (!Serial)”
ESP8266 & websocketsArduino IDE get stuck while uploading sketchESP8266 Upload hex file Arduino IDEArduino IDE - Start Serial Monitor once upload is completeRTL8170 Web Server - client unable to connectSend serial commands through IDE while RX pin is in useESP32 in Arduino-IDE with FS.h and SPIFFSDuplicate libraries while compiling for ESP32 in the Arduino IDEAre lambdas a good way to improve readability in this instance?Serial Port not recognized by Arduino IDE
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 ???
esp8266 arduino-ide
add a comment |
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 ???
esp8266 arduino-ide
add a comment |
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 ???
esp8266 arduino-ide
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 ???
esp8266 arduino-ide
esp8266 arduino-ide
edited May 1 at 14:57
Rhino
asked May 1 at 14:49
RhinoRhino
112
112
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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)
How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
– Rhino
May 1 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 at 17:23
add a comment |
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.
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 at 15:06
I am working on it :-)
– Juraj
May 1 at 15:10
I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
– Jot
May 1 at 15:40
1
I'm still chuckling about your Majenko comment @Juraj. :-)
– st2000
May 1 at 15:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "540"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65017%2farduino-ide-while-serial%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered May 1 at 16:04
Majenko♦Majenko
70.7k43379
70.7k43379
add a comment |
add a comment |
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)
How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
– Rhino
May 1 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 at 17:23
add a comment |
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)
How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
– Rhino
May 1 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 at 17:23
add a comment |
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)
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)
answered May 1 at 15:01
sa_leinadsa_leinad
2,06111336
2,06111336
How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
– Rhino
May 1 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 at 17:23
add a comment |
How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
– Rhino
May 1 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 at 17:23
How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
– Rhino
May 1 at 15:13
How about Wemos D1 Mini and Wemos D32 Pro ?? Is it the same as for Arduino Uno ??
– Rhino
May 1 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 at 17:23
yeah, it's only "needed" for a few less-common ardunio boards. every ESP board should be fine without.
– dandavis
May 1 at 17:23
add a comment |
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.
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 at 15:06
I am working on it :-)
– Juraj
May 1 at 15:10
I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
– Jot
May 1 at 15:40
1
I'm still chuckling about your Majenko comment @Juraj. :-)
– st2000
May 1 at 15:53
add a comment |
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.
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 at 15:06
I am working on it :-)
– Juraj
May 1 at 15:10
I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
– Jot
May 1 at 15:40
1
I'm still chuckling about your Majenko comment @Juraj. :-)
– st2000
May 1 at 15:53
add a comment |
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.
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.
edited May 1 at 16:14
answered May 1 at 15:00
JurajJuraj
8,61321128
8,61321128
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 at 15:06
I am working on it :-)
– Juraj
May 1 at 15:10
I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
– Jot
May 1 at 15:40
1
I'm still chuckling about your Majenko comment @Juraj. :-)
– st2000
May 1 at 15:53
add a comment |
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 at 15:06
I am working on it :-)
– Juraj
May 1 at 15:10
I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
– Jot
May 1 at 15:40
1
I'm still chuckling about your Majenko comment @Juraj. :-)
– st2000
May 1 at 15:53
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 at 15:06
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 at 15:06
I am working on it :-)
– Juraj
May 1 at 15:10
I am working on it :-)
– Juraj
May 1 at 15:10
I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
– Jot
May 1 at 15:40
I have a timeout of one minute in my Leonardo and then the code continues, Serial or not.
– Jot
May 1 at 15:40
1
1
I'm still chuckling about your Majenko comment @Juraj. :-)
– st2000
May 1 at 15:53
I'm still chuckling about your Majenko comment @Juraj. :-)
– st2000
May 1 at 15:53
add a comment |
Thanks for contributing an answer to Arduino Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65017%2farduino-ide-while-serial%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown