[SOLVED]SD card is not working at first time !!!

Hi,first of all i am not good at code part of arduino. I am writing a code to save datas to a .txt file. I use arduino uno and think my connection part is true.I am using Catalex MıcroSD Card Adaptor. Here is my simple code;

#include <SD.h>
#include <SPI.h> 

File myFile;
void setup()
{
 
Serial.begin(9600);
     
    while (!Serial) {
        ;
    }

    Serial.println("\nInitializing SD card...");

    pinMode(4, OUTPUT);
    digitalWrite(4, HIGH);
   


 
while (!SD.begin(4)) {
Serial.println("Insert SD card");

delay(3000);

 
}

Serial.println("OK");

delay(3000);

myFile = SD.open("test.txt", FILE_WRITE);
 
if (myFile) {
Serial.println("writing... ");
 

myFile.println("something ");
 
myFile.close();
Serial.println("done");
} else {
 
Serial.println("fail");
}
 
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt");
while (myFile.available())
 
{
Serial.write(myFile.read());
 
}
 
myFile.close();
} else {
 
Serial.println("fail");
}
}
 
void loop()
{
 
}

After opening the serial port, my code does not working correctly.I see the serial port like that;

Initializing SD card...
Insert SD card
Insert SD card
Insert SD card

I remove the SD Card and insert again in a one second while program is running.Then it works.I see the serial port like that;

OK
writing... 
done
something
something 
something 
something 
something 
something 
something

My question is that why does not my code work at first time ? Why do i need to remove and insert the SD card ? I hope, i have explained my problem correctly.
Thanks in advance

When I run your code it works fine. I don't need to remove and insert the SD card.

I am using an Uno with a CATALEX SD Shield.

Initializing SD card...
OK
writing...
done
test.txt
something
something

Thanks for reply !!

It is the SD Card shield that i use. There are 5 pins as CS, SCK, MOSI, MISO, VCC and GND.

Are there any differences between yours and mine ?

I am changing my question. Instead of removing card, i remove and reconnect 5V or GND connection of SD modul and it works again.

Can i perform it with codes ?

I am using the Catalex mini shield identical to yours. Your code runs for me without the removal and reinsertion of the card. Are you sure the card is fully inserted when the sketch fails?
Edit following your latest post.
Is there anything else connected to the Arduino other than the SD module?
How are you powering the Arduino?

Yes I am sure that the card is fully inserted.

In actually it is not about inserting or removing SD Card. Because,while program is running, i remove and reconnect 5V or GND connection instead of removing card, it works again. ::slight_smile:

Thans for your reply.
I have changed my SD card and problem was solved. There is no any mistake in code and connection part. When i run my old card in card information part of arduino, arduino could not define its FAT type (FAT16 or FAT32) and i have ignored this situation. It was a big mistake >:(