SdFat writes a file, but it remains empty


I'm using a Mega2560 and an SD module.
I've hooked up the cabling correctly (MOSI, MISO, SCK) and remembered to change the CS/SS pin to 53 for the Mega. Also tried both 3.3V and 5V as the module accepts both.

I'm using two SD cards, SanDisk 4 GB and SanDisk 512 MB.
Formatted both cards using SDFormatter 4.0.

I've tested with a few examples included with SdFat and I didn't change anything (except CS/SS pin).

When using the included "SdInfo" example I get the following output:

init time: 166 ms
Card type: SDHC
Manufacturer ID: 0X3
Product: SD04G
Version: 8.0
Serial number: 0X33F96D70
Manufacturing date: 4/2010
cardSize: 3965.19 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000
SD Partition Table
Volume is FAT32
blocksPerCluster: 64
clusterCount: 120752
freeClusters: 120751
freeSpace: 3956.77 MB (MB = 1,000,000 bytes)
fatStartBlock: 14496
fatCount: 2
blocksPerFat: 944
rootDirStart: 2
dataStartBlock: 16384

Using the ReadWriteSdFat example I get this output on the first run:

Writing to test.txt...done.

If I check the card on my PC the test.txt file is there but it is empty. It should contain "testing 1, 2, 3."

Resetting the Arduino with the ReadWriteSdFat example results in the following:

Writing to test.txt...done.
error: opening test.txt for read failed
SD errorCode: 0X13,0XFF

So I'm rather confused here and can't figure out what is wrong, how come I can create a file but not write anything to it?

Can someone give me a hint as to what might be wrong and what I might want to check?

Needless to say I have tried to search for information on this issue but it didn't help me much further in solving the problem, I've also read the documentation for SdFat but couldn't find anything useful related to this.



Turns out both of the GND connectors on the module has to be connected, uhm... I failed.

Thank you so much! I have been trying to find a solution for days and finally I found this! :slight_smile: