Detect microSD card connected to Arduino? - Page 1
Forum section: electronics / general

author: Peabody
author_rank: Super Contributor
timestamp: April 22, 2019, 05:10:35 pm
content: I'm looking at a possible new SD card bootloader for Arduinos, and this would typically require level shifting since Arduinos are usually 5V and SD cards are 3.3V. Here is the module widely available on Ebay:https://www.ebay.com/itm/382768089497And I believe the relevant schematic is attached below.The problem is detecting the presence of the card on power-up, if possible something other than sending commands into the ether and seeing if any response comes back. That's not a good solution because the user's circuit may have other things connected to the SPI pins.I've done such a bootloader for a couple of the TI MSP430 parts, which don't require level shifting. Every card has a nominal 50K pullup resistor on its card select pin, so I put a 1M external pulldown resistor on that line at the MSP end, so I could read the value at boot. If the card is present it reads HI, otherwise LO. If a card is present, then I switch the pin over to output mode for its chip-select function, and the pullup and pulldown resistors are of high enough value that they don't interfere.But I haven't figured out a way to make that work with level shifting, and haven't come up with any alternative. Unfortunately, the "card inserted" pin on the holder isn't brought out to the module header.If anyone can think of something, please let me know.

author: stj
author_rank: Super Contributor
timestamp: April 22, 2019, 06:48:31 pm
content: look closely,the socket contains a switch near the "J1" print.

author: Peabody
author_rank: Super Contributor
timestamp: April 22, 2019, 09:47:00 pm
content: Quote from: stj on April 22, 2019, 06:48:31 pmYes, that's the switch that connects to ground if a card is inserted, and it's connected to the first pin on the holder. But that pin isn't connected to anything. In the worst case I will use that, but it would require devoting yet another GPIO pin to this, which I would rather not do.I've come up with something that may work. See attached circuit. I jumper around the 125 gate on the Card Select line, and add the 1M resistor on the Arduino, so it looks like the MSP430 version. At the beginning the port is in Read mode, and it will read high if a card is present because the card has a pullup resistor on that pin. Otherwise it will read low because of the 1M pulldown. Then if a card is present, I get around the need for level shifting by putting the port in Input mode when I need it to go high, at which point the pullup resistor in the card brings it high, and putting the port in Output, Low mode when I need it to be low. But I never actually source 5V to that pin on the card, so level shifting isn't needed.This is just the card select line, so there's nothing fast going on there. I will test this when my modules arrive.

author: thm_w
author_rank: Super Contributor
timestamp: April 22, 2019, 09:55:00 pm
content: I know its not what you want, but here is the module with CD signal if anyone else is wondering:https://www.aliexpress.com/item/MicroSD-card-module/32530917432.htmlYour idea is great though, I've had to deal with a lot of bad card detect type switches (which stops product from working), they really are not needed as is shown here.

author: OM222O
author_rank: Frequent Contributor
timestamp: April 22, 2019, 10:25:18 pm
content: easiest solution I can think of:I'm not sure which micro you are using, but there should be some library that allows you write to files etc.Try opening a file with a random name for write and check for exceptions. if there were exception it means there is no sd card and if it passed, it means there is one. put this in a timer interrupt that happens let's say every second and you will have a pretty responsive device without using any hardware.

author: Peabody
author_rank: Super Contributor
timestamp: April 23, 2019, 02:38:40 am
content: Quote from: OM222O on April 22, 2019, 10:25:18 pmWell, I'm working on an SD card bootloader, so there wouldn't be access to libraries. The main problem with just trying to communicate to see if a card is there is that the user might have other stuff connected to the pins used for SPI, and spewing out clock and data traffic might not be appreciated by whatever is connected there. So if possible I need to detect the card's presence directly during bootup.

author: Peabody
author_rank: Super Contributor
timestamp: April 23, 2019, 02:59:45 am
content: Quote from: thm_w on April 22, 2019, 09:55:00 pmThanks for the link. I stay away from AliExpress, but was able to find that module on Ebay. Still, as you say, I hear those switches aren't all that reliable. I don't know why that is, but apparently in time they just break. And if that happens, you can't fix it by replacing the SD card.

author: OM222O
author_rank: Frequent Contributor
timestamp: April 23, 2019, 04:47:00 am
content: Quote from: Peabody on April 23, 2019, 02:38:40 amwell then don't have the chip select pins all randomly pulled down then? go through their detection sequence one by one? I've had no issues doing the software trick. it wasn't used for the boot sequence but I don't imagine it would be that much different?

author: Peabody
author_rank: Super Contributor
timestamp: April 23, 2019, 02:23:53 pm
content: I don't understand what you're saying. Where do I have all the chip select pins pulled down randomly? I just have the one pin pulled down with a 1M resistor.

author: OM222O
author_rank: Frequent Contributor
timestamp: April 23, 2019, 04:39:01 pm
content: Quote from: Peabody on April 23, 2019, 02:38:40 amyou said the other connected chips may not like the data and clock signals on the SPI bus. SPI ignores any incoming data when the chip select is high (doesn't latch any data). so if you're worried about that, just don't have them randomly pulled down. you don't need any pull down resistors (even if you say they're 10Gohm!). simple elegant software solution that adds no cost to the project and is 100% reliable (unless the SD card is broken which is another story!). This is actually what many people use in their designs as it has no draw backs compared to a hardware implementation and comes at a low low price of 0$

author: Peabody
author_rank: Super Contributor
timestamp: April 23, 2019, 10:29:55 pm
content: Quote from: OM222O on April 23, 2019, 04:39:01 pmThe SPI pins are also regular digital I/O pins, so what's connected to them could have nothing to do with SPI. And since this would be a bootloader, I don't want to be sending out SPI clock and data streams on those lines every time the Arduino boots up, not knowing what might be connected to those pins. So I still think I need to use a method of SD card detection that doesn't send out those streams. And so far the pulldown resistor is what I've come up with.

author: OM222O
author_rank: Frequent Contributor
timestamp: April 23, 2019, 11:23:38 pm
content: in which case you need to specify your issue in details when creating a postif not then the answers that you will get are only as good as you described your problem.I'm not sure what your project is that needs to be this modular? are you making your own custom MCU / CPU?anyways I can't think of any hardware implementation that does not require physical buttons which people suggested will fail in the long run.I'm still very skeptical as to what this project is, since you must read the data from the SD card to boot anyways (otherwise having it, let alone detecting it! is useless)in which case you can't have the pins as general IO pins?please describe what you are trying to achieve clearlyEdit:actually I can think of a simple solution ... a 4 x 1:2 demux chip (not sure if you can buy that exact config) to divert the signals either to the SD card, or to the other chips that are present. however I still have a lot of issues with your idea as things just don't add up