How tell Arduino to wait for Serial to be available before continuing

Oscar de la Hera Gomez
A flower that represents Arduino with the text "Wait for Serial" beneath it.

Use the "Serial" and "yield" function together or in combination with a timer or a macro.

There are three ways to get Arduino to wait for Serial, all of which make use of the Serial and yield functions.

A | Standalone

The simplest way to tell Arduino to wait until Serial is available is to use the following line of code.

This can be inconvenient as if Serial is not found (i.e. the board is not plugged into a computer via a USB) the code will not continue.

B | With a Macro

Another way to achieve this is to define a macro that dictates if WAIT_FOR_SERIAL or DEBUG_ENABLED is enabled.

This can be inconvenient as you need to manually set the Macro to be true if you want the board to wait for Serial.

C | With a Timer

A third way to achieve this to create logic that waits for the Serial to be available for a maximum period of time before proceeding.

This is the recommended approach as it will work regardless of whether Serial can be found or not.

Looking to learn more about things you can do with Arduino ?

Search our blog to find educational content on Arduino.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details