Can´t write into file

I am testing a New SD shield using my ARDUINO UNO and the SD.h library that comes wit IDE 022, using the example sketches. Im accessing and reading text files from a 512MB SD card withou problem. Trouble appears when trying to write. It will create files, but no content gets added. Files keep empty. When I try to create a new directory, it also fails, it creates just another file. =( The contents of the SD card I´m monitoring with a Windows Vista PC. I´ve formatted the card completely, and its using 16KB sectors.

Help will be very much appreciated.

How about posting your code? Then, help will be easier to provide.

Sorry, I didn’t post it because is just one of the exaple files of the library.

  SD card read/write
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 This example code is in the public domain.
#include <SD.h>

File myFile;

void setup()
  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.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile ="test.txt", FILE_WRITE);
  // 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");
  // re-open the file for reading:
  myFile ="test.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) {

pgmartin: ... exa[m]ple files of the library...

Except for a couple of missing '}' braces and the missing empty loop() function, that is the Arduino SD library ReadWrite example sketch, right?

That example sketch works for me on an "official" Arduino Duemilanove with an "official" Arduino Ethernet Shield Version 5 (the one with the microSD socket), using a 4 Gigabyte micro SDHC card formatted (as it came from the factory) as "FAT32"

I can read and write the card using various card readers on my Windows XP and Linux workstations.

Originally there was no file named "test.txt" but one was created the first time I ran the sketch. The file contained the single line "testing 1, 2, 3."

Each time I run the sketch it appends another "testing 1, 2, 3." line to the file.


If you are, indeed, using the ReadWrite example sketch from the Arduino SD library, the question is: What is your hardware setup? What Arduino? What Shield? Is there anything else plugged into/onto your Arduino?

What sketch(es) did you run to verify that your setup can read from the microSD card?



Yes, I've been testing the hardware using the example files. I use an Arduino UNO clone with an SD shield (labeld only, like this, both of chinese manufacture. The SD shield is pretty uncomplicated, it has a couple of resistors and the conectors to the SD card. I've done the following tests:

  • After running the script, the file was created, with no content
  • If I connect the SD card into my computer using windows vista, create a file, with content, and the I read it with this same code (just comenting the writing part) the conthent of the file is shown on the serial monitor without a problem

I've also tested the example code that make a Dump of the entire file, and it also works. The problem is allway when trying to write content. Is there any way to debug it, or maybe search with an hex editor if the info is in the wrong place in the sd card?

Use a lower resistor to power it !

arduimat: Use a lower resistor to power it !

I'm powering the shield with 3.3V. Are you saying that I need voltage dividers in the data pins?

I have LCSOFT SD modules, link:

They take either 3.3V or 5V. I have only run them on 5V. The power level circuits are built in and raised some eyebrows but they work. Hardest part for me was getting up nerve to try but I got a good price on 3 at eBay.

Also make sure the SD card is formatted FAT-16, the most compatible. And IMO the SD-Fat library is better.