SD card module won't open file

Ill try to make it as simple and short as I can be while still being descriptive.

Some background info that might be helpful is im a complete noob with Arduino im used to using raspberry pis, python and java.

Second im trying to read a txt file on my Arduino but for some reason when I try to print what it sees I get just -1 which I know that means it doesn't see anything as per the documentation.

This is what it prints out when I list all files

SPOTLI~1/
	STORE-V2/
		C8A80D~1/
			PSID.DB		8192
			TM~1.LIO		0
			LIO~1.CRE		0
			TMP.CAB		0
			CA~1.CRE		0
			TM~1.STA		0
			INDEXS~1		28
			~1.IND		4096
			~~2.IND		16
			~~~3.IND		8
			~~~~4.IND		3277
			~~~~~5.IND		4096
			~~~~~~33.IND		1496
(shortened to make it easier to read)
			LIVE~382.SHA		16
			LIVE~385.IND		1024
			LIVE~~~4.DIR		65536
			LIVE~395.SHA		4096
			LIVE~398.SHA		3120
			LIVE~401.SHA		0
			LIVE~405.SHA		8
			LIVE~409.SHA		2056
			LIVE~413.SHA		65536
			LIVE3D~1.SHA		1088
			LIVE~420.SHA		4
	VOLUME~1.PLI		4454
FSEVEN~1/
	FSEVEN~1		36
	000000~1		72
	000000~2		72
POOPY123/
	_TEST~1.TXT		4096
	TEST2.TXT		24
T.TXT		24
_~1.TXT		4096

I made poopy123 as folder just to mess around with for testing so you can ignore that I've been trying to open t.txt

I already tried to do what has been listed in another forum here and I tried to open the txt file inside the folder just to make sure it wasn't an issue with my directory

/*
  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 (for MKRZero SD: SDCARD_SS_PIN)

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.println("Initializing SD card...");

  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("t.txt");

  // read from the file until there's nothing else in it:
  Serial.print(myFile.read());
  while (myFile.available()) {
    Serial.print(myFile.read());
}

Serial.println("should be done");

  myFile.close();
}

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

OUTPUT

Initializing SD card...
initialization done.
-1should be done
Initializing SD card...
initialization done.
-1should be done

Contents of t.txt (censored to comply with TOS sorry I was tried while making the file)

mother***ker better work

When you see such "strange" list of file names:
the files are as FAT32 (long names), but if your FW uses FAT16 (the old 8.3 rule for file names), you might get such "strange" print.
Check, if the SD card is FAT32 and if you use really FAT32 as File System. I guess, when you configure FatFS, there is an option (macro) to specify if "long file names" should be used.
Looks more like a config at your FatFS is wrong or missing.

After reformatting this is the output of all files, im using Mac and selected fat32 no options for macro btw sd card is 64gb

SPOTLI~1/
	STORE-V2/
		EFD6F2~1/
			PSID.DB		8192
			TM~1.LIO		0
			LIO~1.CRE		0
			TMP.CAB		0
			CA~1.CRE		0
			TM~1.STA		0
			INDEXS~1		28
			~1.IND		4096
			~~2.IND		16
			~~~3.IND		8
			~~~~4.IND		3277
			~~~~~5.IND		4096
			~~~~~~33.IND		1496
			~~~~~~36.IND		4096
			~~~~~~39.IND		1496
			~~~~~~42.IND		2056
			~~~~~~45.IND		8
			~~~~~~48.IND		3072
			~~~~~~50.IND		13
			~1.DIR		65536
			LIVE~1.IND		4096
			LIVE~~2.IND		32768
			LIVE~~~3.IND		16384
			LIVE~~~4.IND		3277
			LIVE~~~5.IND		4096
			LIVE~~71.IND		8192
			LIVE~~74.IND		4096
			LIVE~~77.IND		8192
			LIVE~~80.IND		8224
LIVE~~87.IND		65536
			LIVE~~90.IND		9239
			LIVE~1.DIR		65536
			STORE.DB		36864
			STOR~1.DB		36864
			DBSTR-~1.HEA		56
			DBSTR-~1.DAT		4096
			DBSTR-~1.OFF		4096
			DBSTR-~1.BUC		4096
			DBSTR-~2.HEA		56
			DBSTR-~2.DAT		8192
			DBSTR-~2.OFF		4096
			DBSTR-~2.BUC		4096
			DBSTR-~3.HEA		56
			DBSTR-~3.DAT		4096
			DBSTR-~3.OFF		4096
			DBSTR-~3.BUC		4096
			DBSTR-~4.HEA		56
			DBSTR-~4.DAT		4096
			DBSTR-~4.OFF		4096
			DBSTR-~4.BUC		4096
			DBSTR-~5.HEA		56
			DBSTR-~5.DAT		4096
			DBSTR-~5.OFF		4096
			DBSTR-~5.BUC		4096
			REVERS~1		65536
			TMPSPO~1.STA		4096
			STORE_~1		4
			JOURNA~1.COR/
			JOURNA~1.LIV/
			JOURNA~2.LIV/
			JOURNA~3.LIV/
			JOURNA~4.LIV/
JOURNA~1.ASS/
			JOURNA~2.ASS/
			JOURNA~1.HEA/
			JOURNA~1.MIG/
			JOURNA~2.MIG/
			JOURNA~1		0
			JOURNA~1.SCA/
			SHUTDO~1		4
			REVERS~1.SHA		3136
			~1.SHA		4096
			STOR~1.UPD		8
			JOURNA~1.1		140
			0DIREC~1.SHA		1088
			~~~~~234.SHA		2
			LIVE~1.SHA		4096
			LIVE~~2.SHA		0
			LIVE~~~3.SHA		0
			LIVE~~~4.SHA		8
			LIVE~~~5.SHA		2056
			LIVE~255.SHA		65536
			LIVE0D~1.SHA		1088
			LIVE~262.SHA		1
			REVERS~1.UPD		2
			TMPSPO~1.LOC		7900
	VOLUME~1.PLI		4454
FSEVEN~1/
	FSEVEN~1		36
	000000~1		162
	000000~2		72
T.TXT		8
done!

I reformatted It again and used the built in one in Mac (disk utility) I also just tried a different sd card with the same result

All file and directory names MUST be DOS 8.3 style. No longer filenames will work. The SD card itself must be 32gb or smaller and must be formatted FAT-32. If you get all these condtions are met and it still doesn't work, let me know.

yeah I just tried with a 8gb sd card and It didnt work. Fat-32 file name t.txt

Show us your wiring. Here's mine.
SDcard.pdf (37.1 KB)

here you go, black wire is 3v and it all goes down in order lf the module (as seen on the right)

Doesn't reall sh9ow the actual wiring. A top view would be better. Also, can you hand draw a schematic of what you planned please.

Its just directly wired up to the GPIO pins in the Arduino a schematic is not really needed :))) also im like half asleep

It is not the formatting of SD Card on a host computer: it is the FatFS as part of your MCU FW system. It looks like: your FW supports only FAT16 (with old DOS 8.3 file names).

I know for sure, that FAT32 is possible and working (I use). But you have to set your FatFS source code to do so.

If it is limited to DOS 8.3 format (FAT16) - then just use 8.3 file names (potentially also format SD card as FAT16).

The "issue" is on your embedded FW side: which format accepted/expected by your FatFS. It is not related to how you format SD card or copy files.
But a PC will assume SD card as FAT32 and copy files with long name - this ends up in these "strange" names (still compatible with FAT16 8.3 file name convention, just the fact that files names look now so strange.

Check you FatFS setting on MCU FW side.

I wouldn't use the Arduino 3v3 to power ANYTHING. Try using a better power source.

Aren't you missing something like this?

 if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }

I took that out cause it kept failing and now that I say that out loud I think I know what my issue is :sweat_smile:

I think its the code, I tried the example just now and it worked?!?! I will analyze more now that im not half asleep

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.