SD card with Arduino

Hi my frien I have a problem, I'm using this shield:


I have connected:

  • MOSI – pin 11
  • MISO – pin 12
  • SCK (CLK) – pin 13
  • CS – pin 10

And I use this code, that is an example:

/*
#include <SD.h>

File myFile;

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


  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(10)) {
    Serial.println("initialization failed!");
    return;
  }
  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 = SD.open("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:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
	// nothing happens after setup
}

My problem is, when I run the program it creates the file but don't write in, and it can't read. Why?

Thanks

So, you put the card into your PC and see an empty file named "test.txt"?

Yes, when I put the SD in my Pc the file is empty.

The code you posted omits the line

#include <SPI.h>

This surely has a bearing on matters. I don't think you should be getting anything and the empty files could be the result of some previous stuffup.

The convention at the test is

if (!SD.begin(4)) {

and I believe that should apply also to a module like yours as well - with the CS line assigned thereto.

Okok, but I read that I can use the pin 10 and the pin 4 and it is the same thing.
Howevere, I can try to change the Pin and I will write:

#include <SPI.h>

But I can't use this library in my code. Right?

Several users have trouble with that SD board. It does not have a logic level converter. So if you are not using an Arduino like a Due (3.3v I/O), you are putting 5 volts into a 3.3 volt input on the SD card. That rarely turns out well.

Yes.
Get the one from adafruit.com, it has the 3V-5V level shifter and all.

rossotony88:
Okok, but I read that I can use the pin 10 and the pin 4 and it is the same thing.

This may be true but it is better to deviate from convention after the problem has been solved

I will write:

#include <SPI.h>

But I can't use this library in my code. Right?

I don't know why you conclude that. Everybody else uses it, and you haven't provided any alternative code. The module is made for the SPI bus, is clearly marked for same, and you have wired it in the proper manner, so using the proper software sounds a pretty good idea.