Re-connect to an SD card using SDFat

I have been using SDFat for writing data and logs to an SD card - it works great! However, I’ve noticed that if someone takes the SD card out while the device is running, I don’t have a way to re-connect. I’ve tried re-initializing the sd class if it fails too many times, but that actually seems to freeze up the firmware completely!

Any tips or ideas?

@hagandh, here is a response by the libraries author to a similar question some time ago:


I might add to @peekay123 's post that if you remove the power supply to the SD card connector (and thus to the SD card) this is equivalent to removing the SD card from the connector socket. I remove or switch off the power supply when sleeping (with a Photon but Electron should be the same). Just after waking I call sd.begin() and this restarts the controller IC on the SD card every time. You do have to be careful as the snippet explains about closing files because if the SD card is just removed the likelihood is that the index files will get corrupted. So best not to leave the log files open after each transaction.

1 Like