# Serial print not working in setup, but works in a loop

**URL:** <https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547>\
**Category:** Programming\
**Created:** [August 9, 2022, 6:39am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547 "2022-08-09T06:39:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![xav17](https://avatars.discourse-cdn.com/v4/letter/x/f14d63/32.png) [@xav17](https://forum.arduino.cc/u/xav17)\
**Post date:** [August 9, 2022, 6:39am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/1 "2022-08-09T06:39:43Z")

</div>

Hi, I'm a real newbie to coding and I'm enquiring about the Serial.print function in Arduino IDE. The board I am using is the nodeMCU esp8266.  
I have researched about the board and some common problems. I have since changed the baudrate to 9600 and 115200. I have also changed the settings in Arduino itself so that the baudrate in the serial monitor matches the board's baudrate and the Serial.Begin(baudrate).

However, the serial monitor only displays what I am printing in a loop and not in void(setup).  
Below is an example code that I have tried where this issue persists. Thanks for any help in advanced.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/c/07cf85475dbbb5e32199495c196c1bf3a4f3e084.png)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 9, 2022, 6:53am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/2 "2022-08-09T06:53:15Z")

</div>

Welcome to the forum

The Serial interface is probably not ready by the time that you output to it in setup()

As a quick experiment add delay(100) immediately after the Serial.begin() or a while loop in the same place that waits until Serial is present

```cpp

while (!Serial); //wait for Serial

```

---

<div class="post-metadata">

**Author:** ![xav17](https://avatars.discourse-cdn.com/v4/letter/x/f14d63/32.png) [@xav17](https://forum.arduino.cc/u/xav17)\
**Post date:** [August 9, 2022, 7:06am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/3 "2022-08-09T07:06:26Z")

</div>

Hi there, thanks for your help.

I have tried the above as you said and inputted **while (!Serial);** right after **Serial.begin(115200);**.  
However, it seems as though the the serial monitor skipped past the first **Serial.printf("Hello, ESP8266");**.

I have tried another code that I have retrieved from another website and found out that Serial.print works in a while loop which was within void(setup). The codes are seen below.

.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/1/4/7/1478c4a8cb76aab18e9182a4e75f97fcf3684f6b.png)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 9, 2022, 7:12am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/4 "2022-08-09T07:12:54Z")

</div>

Please do not post screenshots of code and Serial monitor output

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'  
  
Use **code tags** (the \</\> icon above the compose window)&nbsp;to make it easier to read and copy for examination

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

Did you try a delay() after the Serial.begin() ?

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [August 9, 2022, 7:13am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/5 "2022-08-09T07:13:39Z")

</div>

Instead of `while(!Serial)`, add a short delay.

`while(!Serial)` has, to my knowledge. no effect on a nodeMcu. It only has an effect on boards with native USB.

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [August 9, 2022, 7:15am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/6 "2022-08-09T07:15:46Z")

</div>

the ch340 is slow to start. add `delay(500);` after Serial.begin

---

<div class="post-metadata">

**Author:** ![xav17](https://avatars.discourse-cdn.com/v4/letter/x/f14d63/32.png) [@xav17](https://forum.arduino.cc/u/xav17)\
**Post date:** [August 9, 2022, 7:20am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/7 "2022-08-09T07:20:06Z")

</div>

Hi there, thanks for your help.

I have been adding a delay before, however the thought that the delay was too short did not cross my mind. The serial monitor now shows my first serial print.

Thank you again 🙂

---

<div class="post-metadata">

**Author:** ![xav17](https://avatars.discourse-cdn.com/v4/letter/x/f14d63/32.png) [@xav17](https://forum.arduino.cc/u/xav17)\
**Post date:** [August 9, 2022, 7:21am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/8 "2022-08-09T07:21:41Z")

</div>

Hi there, apologies for the wrong formatting when uploading my code in the forum.

Thank you for your input about adding a delay. That has fixed my problem with the first serial print. 🙂

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [August 9, 2022, 7:23am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/9 "2022-08-09T07:23:34Z")

</div>

Is it possible that you are opening serial monitor after uploading your sketch? You probably cannot do that quickly enough to catch any text printed in setup(). Try this: with serial monitor open and your code running on the nodeMCU, click the reset button on the nodeMCU. You may see some garbage text, followed by the text printed in setup().

---

<div class="post-metadata">

**Author:** ![xav17](https://avatars.discourse-cdn.com/v4/letter/x/f14d63/32.png) [@xav17](https://forum.arduino.cc/u/xav17)\
**Post date:** [August 9, 2022, 7:27am UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/10 "2022-08-09T07:27:24Z")

</div>

Hi there, thank you for your help. I have tried opening the serial monitor before the code is uploaded to the board to catch any text that might have been immediately sent to the serial monitor.  
However, the solution is to add a delay right after `Serial.begin(9600)`.  
Thank you for your input nonetheless.

---

<div class="post-metadata">

**Author:** ![boly38](https://avatars.discourse-cdn.com/v4/letter/b/f05b48/32.png) [@boly38](https://forum.arduino.cc/u/boly38)\
**Post date:** [January 22, 2023, 7:20pm UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/11 "2023-01-22T19:20:55Z")

</div>

this works for me (CH340G), thanks !

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 21, 2023, 7:21pm UTC](https://forum.arduino.cc/t/serial-print-not-working-in-setup-but-works-in-a-loop/1020547/12 "2023-07-21T19:21:37Z")

</div>

Closed on July 21, 2023, 7:21pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
