Text write to SD Card Twice

I have never posted before I am an 11 year old learning coder.
I have been trying to write to a text file on a SD card. I am achieving this but the code I am using which is from an example online always writes both of my experimental text TWICE. I cant figure out why.


Hello World
I am Bob
Hello World
I am Bob

I would be very grateful for some help as its probably a very basic , noobie error. Here is the code I am using

#include <SD.h>

File myFile;

void setup() {
// Open serial communications and wait for port to open:
while (!Serial) {
// wait for serial port to connect. Needed for Leonardo only

Serial.print("Initializing SD card...");

pinMode(10, OUTPUT);

if (!SD.begin(4)) {
Serial.println("initialization failed!");
Serial.println("initialization done.");

myFile = SD.open("test.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("Hello world");
myFile.println("Im Bob");
// close the file:
//} else {
// if the file didn't open, print an error:
//Serial.println("error opening test.txt");

void loop()
// nothing happens after setup

"FILE_WRITE: open the file for reading and writing, starting at the end of the file."

The sketch runs once when you finish uploading it and again when you start Serial Monitor. The first one creates the file and puts the text in. The second one re-opens the file and adds text to the end.

 SD.remove("test.txt");   // Add this line to get rid of the file before creating it fresh
 myFile = SD.open("test.txt", FILE_WRITE);
  1. Your code doesn't compile so it can't be writing to the SD card.
  2. Read the sticky message at the top of the forum, particularly the one that explains how to post code properly so that it is easier to copy and paste your code for testing.

I do have one suggestion. When you open the SD card, do it like this:

  while (!SD.begin(4)) {
    Serial.println("ERROR: Can't open the SD card - retrying in 10 seconds");

That way if, like me, you've had the SD card in a card reader on the PC and forgotten to put it bacl on the Arduino, you can just put the card in and then within 10 seconds the code will proceed.


@johnwasser: Doesn't the "while (!Serial)" statement hang the code until the Serial monitor starts and therefore the code can only run once? It does when I test this on a teensy.
It only writes the two lines once per execution of the code. But if you look at the content of the file after running the code a second time and you hadn't looked at it after the first run, it will have two copies which might lead you to believe that it was writing those lines twice in one run.


"while (!Serial)" blocks the code until the Serial are ready (started), it don't care if the Serial Monitor is open or not, and it's mostly used for Leonardo boards, since there is something about their Serial comunications taking longer to start.

Thanks johnwasser
You helped me with my project.
That was the exact problem.

Now i can continue data logging in peace.