Check if a SD card is connected

I’m looking for a way to check if a SD card is available in slot (via SPI) before I send data do save in it.
Till now, I’m using:

try:
    ww.write(data, sync = True)
except:
    mmc_card = False
    ww.close()

but this is not working.

The device saves data periodically in SD. I want to check if an error occurred after the file is open. Explaining better: if an error occurs in SD, the device will not try to save data anymore and send an alert to the user.
Is there a better way?

hi @Nysix
some SD card modules have Card Detect (CD) pin to signal if the card is inserted.
Does your module have that pin?

Hello @karimhamdy1

No, my module doesn’t have this pin. I can check if a card is inserted when my application starts (using a try/except with open() method). The problem is if the card fails while my application is running.
I’m logging data every 30 seconds. I need to check if the card is OK before the write operation. If it is not OK, I send a notification to the user (“hey, the SD card is dead”) and set a flag to stop the write operation.

I really don’t know if it is possible.