Handling SD card malfunction

Hi,

I have been reading some files from an sd card but due to a "bumpy" environment (lots of vibrations), every once in a while the card seems to lose connection, despite still being in the socket. The file.read() function just stalls until the watchdog kicks in. I'm pretty sure it is physical disconnection since after the reset the card doesn't initialise again until I remove it and insert again.

What happens when a card gets physically disconnected during reading data from it, from code standpoint? Is it possible to detect such situation with software, so that instead of crashing I could write some code to give me the opportunity to reinsert the card?

I've tried to get the number of available bytes to read (file.available()) but it doesn't work in such situation, it also just stalls

Thanks!

Looks like any of the SD card functions returns a boolean showing TRUE if function is successful, and returns FALSE if that function failed. Try and see if it works for you.

Paul

Thanks!

How have I not thought about that yet... I was so fixated on the file class that I didn't realise that there is the SD class as well. I have tried the SD.exists(filename) and it indeed returned false when I removed the card. Many thanks again.

Avalyah:
Thanks!

How have I not thought about that yet... I was so fixated on the file class that I didn't realise that there is the SD class as well. I have tried the SD.exists(filename) and it indeed returned false when I removed the card. Many thanks again.

That is great! I have not used an SD card with Arduino, but the return code just made sense to be there for use.

Paul

Well, tbh the solution is not absolutely perfect - I can't check if card is present before reading every byte - the SD.exists(filename) function takes about 19ms.

But still, filling the buffer takes about 20-25ms and happens roughly about once a second (depending on a whole lot of other things) so if the card does get disconnected I have about 95% chance to catch it (if it happens during the 950ms before filling the next buffer I catch it. If it fails dring the 50ms it takes to check the SD card and write data to buffer, well, I'm out of luck :D). Since this didn't happen often at all I thing those odds are acceptable :smiley: