Serial Monitor prints empty space after uploading

I'm using an Arduino Mega2560 connected to my PC via USB, and I'm seeing some odd behavior when I upload the following code using the Arduino IDE on Windows 11

void setup() {
  Serial.begin(9600);  
}

void loop() {
  Serial.println("ABCDEFGHIJK");
  while(true);
}

As you can see, empty spaces are printed before "ABCDEFGHIJK".

However, when I press the device's reset button and print again, it prints normally without the empty spaces.

I've tried several things, but they didn't work:

  • Using a string instead of a constant: Didn't work.
  • Setting the baud rate to 115200: In this case, "ABCDEFGHIJK" was printed twice, as shown in the image below.

For reference, on a Mac, it printed "AJK" followed by a new line and then "ABCDEFGHIJK".

If I add a delay of about 1100ms before printing, the problem doesn't occur. However, I'm curious why this is happening.

That is normal. The Serial process needs a tiny amount of time to get ready. You will see many sample sketches use various techniques to deal with that. The most obvious is to add delay(1500); after the Serial.begin statement.

1 Like

it is not normal. you must check what is connected to the PC/Mac and what program is started. not forget to check if it appear only in IDE, bc i seen such behavior never.

void setup() {
  Serial.begin(9600);  
  Serial.println("\nABCDEFGHIJK");//will be always at new line
}

void loop() {}
1 Like

It is normal AND it's not normal. There are plenty topics about the issue, do a search if you feel like it.

I've seen it plenty times that there is some rubbish (not spaces) printed but if I now try to reproduce it, I can't :frowning:

I'm using Windows and Linux, do not know about the Mac.

If it wasn't normal why did the developers of the system make a function available to test for 'readiness'. Some boards just use delay, others use !Serial. Totally normal.

not normal to put something in not ready line.

Maybe we have a language problem?

Serial.begin() is not supposed to send any bytes in the line. if it does - something is not normal.

Go read the tutorial. At power on, NOT reset electrical noise causes the phenomena.

yes, we have language problem.

1 Like

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