[sdfat] Any way to check if SD has been remove during loop()

Hello all and happy new year.

My program write log into a sd card.
Mst it important, it need to write GPS position when GSM is not available.

Is there a way to check if the carshas been remove (accidenty or not)?

If it return tue (= card removed), it will loop until the card is re inserted.

do
{
   Serial.println(F("Crad removed"));
}while(isInserted() == false);

Should I do with:

sd.begin(CHIPSET)

in the do loop, instead of isInserted?

Cheers

Most card readers have a card detect switch. You can read this switch to determine that a card has been removed.

SD cards must be reinitialized when they have lost power, so when the card is reinserted, you will need to call sd.begin(). The calling of sd.begin() after the first initialization is straightforward with SdFat but SD.h requires some additional code. See http://forum.arduino.cc/index.php?topic=239157.msg1718831#msg1718831

You can use a button to trigger the call to sd.begin() when the card has been reinserted. If you try to use the card detect switch it will require good debounce and an understanding of the timing of when the card trips the switch and when the contacts are actually made.

Most card readers have a card detect switch.

That switch is probably NOT connected to the Arduino, so you'll need to add a jumper wire to get access to the switch.

I have tried using this code and it's working

if (!SD.begin(CS_pin)
    {
      Serial.println("SD Card Failed!");
while(1)
}

I don't know if it's reliable, because everytime when it comes into loop the CS pin gets high for a second.

See what experts says.

Using SD.begin() is not a good way. It is not reliable.

You can’t use it if a file is open since a begin() call will clear all cached data.

Use a socket wit a card detect switch!