MicroSD card detection Teensy 4.1 - SOLVED

Jeff_805

New member
Such a shame. Looking up into the end of the card holder slot, pin 9 (card detection) appears to be there (in this image below it's missing - like it was intentionally removed). Again, in mine it's there but, unfortunately, it doesn't appear to be tied to any of the adjacent pinholes (pinhole 37). If PJRC is interested in user feedback, my wish is that you'd tie the card detection back to pin 37 (or any other). This would allow users who want/need card detection an option. Those who don't want/need it can "cut" the link, like you cut Vin from Vusb if you're using external power.

1706997650251.png


I'm going to poke around with my DMM to see if I can find somewhere external to pick up the signal, but it'd be a LOT easier and cleaner to have it built into the board. That's about as low hanging as fruit gets IMO. This post here is giving me hope AND proving there is a need.
https://forum.pjrc.com/index.php?th...teensy-3-6-builtin-sd-card.45090/#post-284344

Does anyone happen to know what the part # is for the cards (I'm assuming different revisions used different holders) used on these boards? The one on my Teensy 4.1 does NOT look like this one in the image.

Thank you,
 
Last edited:
Our SD class code has some code in it to help with the detection of media.

If you look at section 26 of the reference manual, you will see that the DAT3 pin can be used as a card detection pin.

The SD.begin() call if it fails to open properly it will set the SD_DAT3 to INPUT_PULLDOWN,
And there is a member call SD.mediaPresent() which in the BUILTIN_SDCARD case will use this to detect that a card was inserted.

The media present code, if previous calls to it believes that there was media present, it will ask the card for it's status and in error condition,
will return false...

For other external SD adapters, if they have a CD pin, you can pass that in to the SD.begin(pin_number) and the mediaPressent code will use this
for the detection.
 
Forgive my shaky camera hands and poor lighting but here is what I found from poking around:
1707001347932.png


What I'm calling Pin 9 is on the far right and lines up with the mechanical lever that moves when the MicroSD card is inserted.

1707001465621.png


Using my DMM, that lever (Pin 9?) has continuity with the housing (grounded) when the MicroSD card is NOT inserted. Continuity is missing when the MicroSD card IS inserted. Use the built in pull up resistor and watch for falling edge to determine if the cards removed? Look for a rising edge for insertion of the card?

Anyone have a good idea on how to solder a conductive thread between that lever and a pin. Did I mention my shaky hands...? ;-)
 
Our SD class code has some code in it to help with the detection of media.

If you look at section 26 of the reference manual, you will see that the DAT3 pin can be used as a card detection pin.

The SD.begin() call if it fails to open properly it will set the SD_DAT3 to INPUT_PULLDOWN,
And there is a member call SD.mediaPresent() which in the BUILTIN_SDCARD case will use this to detect that a card was inserted.

The media present code, if previous calls to it believes that there was media present, it will ask the card for it's status and in error condition,
will return false...

For other external SD adapters, if they have a CD pin, you can pass that in to the SD.begin(pin_number) and the mediaPressent code will use this
for the detection.

That's awesome @KurtE, thank you. But can you please help clarify. I'm wanting this because someone may remove the card while the Teensy is running. I'm coding processes that read/write to the card to stop at that point. Upon reinsertion, I think I need to code the SD card to be reinitialized?

So I'm not necessarily trying to detect if the card was there upon startup. I'm trying to detect if it's removed or reinserted after the Teensy initializes. Can I do that with what you mentioned above?

Re-reading your post, I think the determination of the SD being present is stale data based on if it was there when initialized. It does not allow for detection of removal/insertion post startup. Is that correct?
 
As a follow up, what @KurtE suggested does work perfectly for me. Remember to re-initialize the card after it's been re-inserted.

Code:
if (!SD.mediaPresent) {
    while (!SD.begin(BUILTIN_SDCARD)) {
        Serial.println("SD is not present or not initialilzed.");
        delay(1000);
    }  // end while()
}  // end if()

Something similar to that runs in the loop() of my application. So if someone removes the card while it's running, it effectively pauses the program until can read and write to it again. From reading Section 26, it sounds like it's not instantaneous (interrupt listen period on PIN3 when data is not being sent/received) but it's quick enough in my case.

Thank you @KurtE.
 
Back
Top