For now all I'm trying to do is write the number 10 to a file in the sdcard, and print the written data through serial. I'm using a seeduino v 2.2, a catalex microsd card adapter and a generic brand 4gb sd card ( works fine with the pc).
If I open the serial monitor window immediately after I connect the usb to the arduino it works and starts writing the data. But if reset the program, it shows card failed or not present, no matter how many times i reset it. If on the other hand I unplug the usb cable, plug it back and open the serial monitor right after, the code runs ok until I reset it again. If I take the vcc wire out of the sd adapter and plug it back before reseting the program it works fine too, only for that first time. Whenever I reset the program it just won't work. What in earth is going on? Here's my code:
/*
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK(SCK) - pin 13
** CS - pin 10
*/
#include <SD.h>
const int chipSelect = 10;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void loop()
{
delay(500);
String dataString = "";
dataString +=String(10);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.print(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
I have used the Catalex microSD adapter and cards can be reinitialized after a reset on Uno and other Arduino boards.
Can you try another microSD card? A few SD card can not be reinitialized without cycling power.
My Catalex adapter has the following on the back: v1.0 12/20/2013
I tested with the old CardInfo example included with SD.h and the SdInfo sketch in the new version of SdFat. SD.h is just a wrapper around a very old version of SdFat.
Thanks! I tested it with another SD Card, using both SD and SDFat libs, but no luck. Finally got the Catalex Module working fine by switching the module's VCC input with a 2n2222 transistor. All I do is digitalWrite 1 to the base at setup.
I test SdFat with the Catalex MicroSd module and I never have had a problem with reinitialization of a card. The Catalex MicroSd module is used by many people and no one has reported this problem.
I mainly use SanDisk, Transcend, and Kingston MicroSD cards. I now have tested the Catalex module with about 10 cards including ADATA and generic Amazon basic cards and all can be reinitialized without cycling power.
I used an Uno for the tests.
Could you run the SdFat SdInfo example twice without cycling power? The example will print the following when it starts:
SdFat version: 20131225
type any character to start
Enter a character and the program should print info about the card. It should prompt again with this line.
type any character to start
Enter another character. SdInfo will attempt to reinitialize the card. If it fails it will print something like this:
And it will actually work after resetting now. Unlinke my other friend CardInfo, from SD Lib, which will still need power cycling to read the info a second time. Guess it was all about the library after all, which is strange because I remember testing SDfat and getting similar results, even though it's been a while since I've tested it. Anyway, thanks once again.
I guess I remembered it right. My attempt to write data to the SD card using SdFat library still needs cycling power. I'm running this:
if (!sd.begin(10, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!myFile.open("datalog.txt", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening datalog.txt for write failed");
}
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
myFile.close();
Serial.println("done.");
And that will work the same way it was before. Running it for the first time will successfully write to the SD card.
Resetting it will show: "Invalid format, reformat SD"
Even though I've tested it with some other cards I have, I'll get a fresh SD from a good brand to make sure the problem is not it and I'll post the results.
Could you try the SdFat bench example? I tried it with a number of cards with the Catalex module and none fail.
I run the example and when it finishes I type another character and it runs the test again after reinitializing the card.
Here is typical output.
Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 1029
Type is FAT32
File size 5MB
Buffer size 100 bytes
Starting write test. Please wait up to a minute
Write 179.33 KB/sec
Maximum latency: 301060 usec, Minimum Latency: 84 usec, Avg Latency: 552 usec
Starting read test. Please wait up to a minute
Read 310.71 KB/sec
Maximum latency: 2812 usec, Minimum Latency: 84 usec, Avg Latency: 316 usec
Done
Type any character to start
Free RAM: 1029
Type is FAT32
File size 5MB
Buffer size 100 bytes
Starting write test. Please wait up to a minute
Write 179.47 KB/sec
Maximum latency: 297960 usec, Minimum Latency: 84 usec, Avg Latency: 551 usec
Starting read test. Please wait up to a minute
Read 310.71 KB/sec
Maximum latency: 2820 usec, Minimum Latency: 84 usec, Avg Latency: 316 usec
Done
Type any character to start
I can also hit the reset without power cycling the card and it reruns correctly.
I don't have a seeduino and no users have contacted me about problems with a seeduino.
There have been SD cards that will not correctly reinitialize in SPI mode. This seems unlikely since you have tried several cards.
I am using SD card adaptor from LC Studio and run Cardinfo Sketch. It worked for the first time but not for the second try.
Resetting the UNO doesn't help, I had to re-plug the USB cable.
Interestingly, I found re-insert the SD card (reject and insert the SD card again only, without un-plug USB cable) also help.
I wonder if the sketch leaves something un-finished so that SD card was waiting for something and wouldn't accept any further command until being hard-reset by un-plugging, ... just my first thought.