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

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.

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


while (!Serial);  //wait for Serial

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.

.

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) to make it easier to read and copy for examination

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

1 Like

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.

1 Like

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

2 Likes

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 :slight_smile:

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. :slight_smile:

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().

1 Like

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.

this works for me (CH340G), thanks !

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