Serial Monitor

After writing in the setup window, when looking at the serial monitor, the first character in the solution gets displayed twice. How do I fix that?

For example, in Programming Arduino: Getting Started with Sketches - Chapter 3 : C Language Basics

// sketch 3-04
void setup ()
{
Serial.begin(9600);
int a = 2;
int b = 2;
int c = a + b;
Serial.println(c);
}
void loop ()
{}

When I select the Serial Monitor icon, the window opens up and will present "4", but sometimes after a while, it will present "4" again, so it in the Serial Monitor window it will present "44".

Why is it looping when the code is written in setup and its suppose to occur only once?

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.

Posting tips.

  • Always list the version of the IDE you are using and the board version if applicable.
  • How to insert an image into your post. ( Thanks Robin2 )
  • Add your sketch where applicable but please use CODE TAGS ( </> )
  • Add a SCHEMATIC were needed even if it is hand drawn
  • Add working links to any specific hardware as needed (NOT links to similar items)
  • Guessing is not the best way to describe an issue. Actual facts are the most important.
  • Remember that the people trying to help cannot see your problem so give as much information as you can

Sure. I hope this is better.

Arduino type: UNO
Version of IDE: Arduino 1.8.10

Code tag:

 void setup ()
{
    Serial.begin(9600);
    int a = 2;
    int b = 2;
    int c = a + b;
    Serial.println(c);
}
void loop ()
{}

I was expecting the Serial Monitor to present the characters or code in the manner that is was written in the sketch. It did that, but it is looping twice and it displays the first character of C twice.

For example, as I mentioned before, where I would expect c = 4 to be presented in the serial monitor, it will present that, but it will present it twice. "44"

In another example, where the code is written to present "1234", the Serial monitor will present "11234".

I actually just tried it again but this time it looped twice like so on the serial monitor:

1234
1234

In summation, the code presents itself differently when it is uploaded and then ran through the serial monitor. It looks like it is trying to loop or behave like a loop.

Various presentation:

Example 1:

1234
1234

Example 2:
11234

Example 3:
44

I don't know if I was clear in the description, so let me know if I need to clarify something.

Thank you.

This definitely is not the intended behavior. I have seen behavior something like this before, but I can't reproduce it with your sketch. When you open the Serial Monitor, it resets the Uno.
So my hypotheses are:

  • The data printed before the Serial Monitor is opened is being buffered.
  • The board is getting reset twice.

I don't have an explanation for what would cause either.

As a workaround, you might try adding a short delay after Serial.begin() and before the first call to Serial.print(). That has fixed some other weird serial output problems I've had with boards other than the Uno.