SD.open does not create file

Hey guys,

I use an Arduino Mega 2560 R3 with a TFT LCD mega shiel V2.2 and TFT_320QVT_9341 touch screen with a SD slot on it. My SD card is 1GB and formatted to FAT32.

I used the CardInfo library to see whether my SD card is initialized. The Serial monitor showed the following text (so I guess it worked fine):

Initializing SD card…Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT32

Volume size (bytes): 965101056
Volume size (Kbytes): 942481
Volume size (Mbytes): 920

Files found on the card (name, date and size in bytes):

Then I added the following code form the “Files” and “ReadWrite” examples to test if a file is created.

#include <UTFT.h>
#include <ITDB02_Touch.h>
#include <UTFT_Buttons_ITDB.h>
#include <SPI.h>
#include <SD.h>
#include <TimeLib.h>


UTFT myGLCD(ITDB32WC, 38, 39, 40, 41);
ITDB02_Touch myTouch(6, 5, 4, 3, 2);
UTFT_Buttons myButtons(&myGLCD, &myTouch);
const int chipSelect = 53;

File myFile;


(...more stuff for the touchscreen...)


void setup() {
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myTouch.InitTouch(1);
  myTouch.setPrecision(PREC_HI);
  myButtons.setTextFont(BigFont);
  myButtons.setSymbolFont(Dingbats1_XL);
  myGLCD.setBrightness(16);          //this stuff is for my LCD touchscreen
  pinMode(50, INPUT);
  pinMode(51, OUTPUT);
  pinMode(52, OUTPUT);
  pinMode(53, OUTPUT);    //my Arduino has other outputs than normally
  SPI.begin();
  SD.begin();
  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


(...CardInfo Code...)


  // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  File myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

 

// 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("example.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 file");
  }

  // re-open the file for reading:
  myFile = SD.open("example.txt");
  if (myFile) {
    Serial.println("example.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");
  }

}

The output from the serial monitor is:

Creating example.txt…
example.txt doesn’t exist.
error opening file
error opening test.txt

So I guess the problem lies in creating the file… but what did I do wrong? I tested all of the other libraries from Arduino for SD cards but nothing worked… I also changed the name of the file but that didn’t do anything either…

Maybe the library doesn’t work with my arduino? Or there is a problem with the connection because I use other pins than in the examples? Or is there a function that I didn’t call in the right way?

Any ideas?

I tried to format the card with the SdFormatter of the SdFat library. Now I formatted it to FAT16 because I read only cards>2GB should be FAT32, but then the CardInfo tells me:

Initializing SD card...Wiring is correct and a card is present.

Card type: SD2 Could not find FAT16/FAT32 partition. Make sure you've formatted the card

...why? :D

Okay so now I'm back to the first problem. The SD card initializes correctly but there is no file created when I use anything like:

myFile.open("test.txt", FILE_WRITE);
myFile.close();

is there anything I didn't concider? I don't know if the information is transferred to the card correctly... do I need to set something like a bit rate or anything like that?