double output on setup observations

Google redirects top hits on this search to various necro posts here that have many suggestions as to why you may be seeing double output when a sketch is uploaded to Arduino. Usually with a Serial.println() involved in the setup() code block of a sketch. What I never saw was any testing of behavior that led to an understanding of why this might be happening. There were lots of hardware based suggestions such as adding a capacitor or disabling brown out detection/reset functionality but what I discovered was something much more simple. Here's hoping that Google eventual re-indexes and links this suggestion as well.

CODE SNIPPET:

void setup() {
Serial.begin(9600);
Serial.println("Serial Monitor initialized...");
}

void loop() {
.
. your main program code here
.
}

SERIAL MONITOR OUTPUT (TIMESTAMPS ON FOR CLARITY):
13:11:01.816 -> Serial Monitor initialized...
13:11:01.816 -> <first output of your main loop()>
13:11:01.816 -> <possible continuation of first output of your main loop()>
13:11:03.437 -> Serial Monitor initialized...
13:11:03.470 -> <first output of your main loop() identical to 13:11:01.816>
13:11:03.503 -> <possible continuation of first output of your main loop() identical to 13:11:01.816>
13:11:04.462 -> <new output, expected, as normal for the code you wrote in your main loop()>]
.
.
.

OBSERVED BEHAVIOR:

Serial Monitor Open + Arduino IDE Upload Sketch button clicked >> double print/output in serial monitor as described above.

Serial Monitor Closed on above running Arduino sketch and re-opened >> single print/output in serial monitor not as described above but as expected as normal.

On upload the Arduino board is reset. On opening the Serial Monitor the Arduino is reset. When the Serial Monitor is open when the sketch is uploaded to the Arduino always see a double print as described above. Both appear to be sending reset to Arduino board resulting in double reset, double print of initial Serial output.

This behavior is 100% repeatable on my setup no matter what combination I try. Could be a missing check within the Arduino IDE to handle the state of having Serial Monitor open and a new upload of sketch to Arduino board resulting in two resets being sent to the Arduino board resulting in peoples puzzling over double prints over years of same question asked again and again.

It's not a common question here. Is it creating some problem for you?

Possibly a power kind of issue causing a reboot when things are powered up.

Maybe fire up your hardware, wait a bit for things to settle out then firs up the serial monitor stuff?

-jim lee

Does this help? Double print on upload - Suggestions for the Arduino Project - Arduino Forum

Add a 200-millisecond delay after Serial.begin(). That will give Serial Monitor time to connect and cause the second reset before your sketch does any output.

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