Hi,
I' ve been trying to get an SD card reader working with the SD library. After lots of reading of forum articles I can't find anything that solves the problem I have. Having struggled with my own code I went back to run the examples and to see which of them work.
The CardInfo example works and the Files example seems to work however the ReadWrite example fails. The library will not write successfully to an SD card. I have tried two different readers and four different SD Cards - they all give the same problem. The SD card ReadWrite example code is (Note this is not my code but the code that comes with 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 (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);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
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
}
After an attempt to write to the SD card there some weird looking file names. Running the CardInfo example after a readwrite gives the following:
Initializing SD card...Wiring is correct and a card is present.
Card type: SD1
Clusters: 490848
Blocks x Cluster: 8
Total Blocks: 3926784
Volume type is: FAT32
Volume size (Kb): 1963392
Volume size (Mb): 1917
Volume size (Gb): 1.87
Files found on the card (name, date and size in bytes):
B⸮@)0 .t⸮ 2016-00-23 12:02:48 1082158849
=⸮@`qp.⸮/ 2022-04-16 12:02:04
⸮AQ ⸮".⸮⸮ 1982-00-24 10:01:00 1099173953
PA.⸮A/ 1981-00-05 00:32:48
A⸮AP 1988-02-01 00:16:10 68702240
A⸮AP. 2012-12-04 02:02:02 33883160
A⸮AP.A/ 2020-01-00 08:12:08
⸮AP.A⸮ 1982-00-24 10:01:00 1099173953
PA.⸮A/ 1981-00-05 00:32:48
A⸮AP 1988-02-01 00:16:10 1511936032
⸮⸮P⸮Qd.J⸮R 1989-01-01 08:04:00 2147483906
@⸮$
I'm sure others have had this problem given some of the stuff I have read in the forum - however I cannot find a solution.
Any help will be gratefully received.
Thanks.