SD card write problem


I'm trying to connect SD card shield to Arduino. Using this SD module:

Also using standard SD ReadWrite example.

Reads work well.

Problem is: when executing

 myFile ="example.txt", FILE_WRITE);

SD card becomes fully corrupted - many broken files appears...

Please help. Thanks.

There is too little information to help now, so

What library are you using? Which Arduino? How is the board connected? What does the rest of your code looks like?

The following may be helpful...

Even though it is couched in terms of a specific SD adapter, many general principles are addressed, and anyway- you can probably adapt the code to suit your adapter.

Different SD cards use somewhat different FAT filesystems - perhaps your SD filesystem code has incomplete support? It shouldn't be writing anything if it doesn't recognise the actual filesystem present on the card. And the fact it reads suggests it does recognise it.


Thanks for replies.

Lets me explain more detailed.

First of all Arduino - Duemilanove, 328 I'm using SD.h for communicated with SD card.

Wiring is: CS - 4 MOSI - 11 CSK - 13 MISO - 12

SD card is formatted in vfat (msdos fat) Any other format doesn't allow to initialize card.

Results is:


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");


  myFile ="test.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    // close the file:
  } else {
   // if the file didn't open, print an error:
    Serial.println("error opening test.txt");

works fine. I could save a file to SD card on my linux laptop and read it in Serial console.

  myFile ="test.txt", O_CREAT | O_WRITE | O_APPEND);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");

Doesn't work. I need reformat card after this execution.

If you have any suggestions - please help. I'm new in Arduino world.

Hello, Did you solve your problem? I have the same with the same reader. Kind regards, Grego