Detection of SD card removal

yeahtuna

Well-known member
I'm working on a routine for detecting insertion and removal of the T4.1 SD card. It works really well to detect insertion and removal of the card, but once the card is connected and I'm checking to see if it's still connected using (SD.mediaPresent()), it can cause errors reading data back from the card, which in my case leads to horrible square waves coming through my audio playback (spent 2 days to figure that out). Does anyone have a more robust solution?

Code:
elapsedMillis checkForSD;

void loop() {
        if (checkForSD > 500) {
            checkForSD = 0;
            if (SD.mediaPresent()) {
                if (!root) {
                    checkForSD = 0;

                    root = SD.open("/");

                    if (root) {
                        if (notify) notify->handleNotify(this, kSD_Card_Inserted);
                    }
                }
            }

            else {
   
                if (testInstrument) {
                    instL.disconnect();
                    instR.disconnect();
                    testInstrument->stopAudio();
                    if (notify) notify->handleNotify(this, kUnloadingInstrument);
                    delete testInstrument;
                    testInstrument = 0;
                }

                if (root) {
                    root.close();
                    if (notify) notify->handleNotify(this, kSD_Card_Removed);
                }
            }
        }
}
0
 
Last edited:
Perhaps if you store the working path in a global variable, then close the file before checking if media is present, then reopening it and navigating to the active path might help? But not sure how much time this would consume in the loop - or do you need the file open as long as your app is running and the car is inserted?

Another option is using an SD card slot with a card detect switch - you can extend the SD card port on the T4.1 using an extension cable, and then wire the detect pin the a digital input and read it's state
 
@KurtE - is this SD.mediaPresent() an item you worked out?
Is the p#1 issue possible from what it took to determine card presence? Expected that files closed before calling this?
 
@KurtE - is this SD.mediaPresent() an item you worked out?
Is the p#1 issue possible from what it took to determine card presence? Expected that files closed before calling this?
I have worked on some versions of it. I believe it SD library version if it thinks you previously had media, it then asks the card

does it by asking the card for information (card->status())... Code is in SD.cpp...
If it did not previously have media, it use SD_DAT3 to detect if a card is inserted or not...
 
I have found a solution: file.read() returns -1 after the SD card has been removed.

I didn't notice this previously because I was saving the return value from file.read() to a uint16_t.

So if I get -1 back from the read method, I only then call SD.mediaPresent().
 
Sounds like the SD file reading is being done in an interrupt handler.
No, the reading was being done on the main loop() not long after the call to SD.mediaPresent(), so it's not safe to call that method under normal circumstances. Again the solution is to only call SD.mediaPresent() when the card was previously initialized and some other file reading error leads you to believe that it may have been removed.
 
Again the solution is to only call SD.mediaPresent() when the card was previously initialized and some other file reading error leads you to believe that it may have been removed.
That is one usage pattern.

Another is that if it has previously detected that there is no media. The code then uses DAT3 pin to detect if a card is inserted.
As mentioned in a few places in the reference manual, such as section 26.4
1726843450690.png
 
Back
Top