Go Down

Topic: SD lib:SD.exists() returns TRUE if SD is removed (Read 13 times) previous topic - next topic


Thanks for the answer.
My CardSocket does not have this Card Detect because I had to make it myself using a mSD-to-SD adapter.
But I guess this is my problem, not a general one.
How ever, the original reason to post this Thread was the behavior of exists() ( and begin() in some way )

Let's assume I have a socket with the CardDetect pin, I pull the card out and insert it/another one again. I still need a way to detect if a File exists after that, and that is not possible if exists() reads the available files from a buffer.
And calling begin() again seems to fail. It returns FALSE because it thinks it is already initialized I guess (it would return TRUE if initialisation was successful). But there is no end() or something to destroy the object.


Jan 10, 2011, 03:27 pm Last Edit: Jan 10, 2011, 04:43 pm by fat16lib Reason: 1
All sockets that I have seen have a card detect switch and write lock switch.  There are 11 pins on the socket.  nine go to the SD and the outside two on the side with narrow spacing are the switches.

There is no simple reliable SPI command to detect the SD card.

Card removal must be handled by the app as I stated above.  Card present is just digitalRead(CD_PIN) is low.  

Shields have no uniform connection to the switches so it is easier for the app to handle them since it is so simple.

Why have an extra begin() parameter for the CD_PIN, allocate RAM to remember the pin number so the library can return digitalRead(CD_PIN)?

MicroSD sockets also have a CD switch.  I have tested CD on the Gravitech and Adafruit micoSD breakouts.

I have not been able to make CD work on the new Arduino Enet shield.  CD and WP are connected to analog 0 and 1 with pullups.  My shield may be defective.


Thanks for your suggestion.
But the SD socket I use does not have a Pin to detect that the card is present.
How ever, I think such a pin/switch is optional so shouldn't the Library be able to detect the availability of the SD itself?

But even if you think that it is unnecessary, how can I now detect that the card is present?
I am not familiar with SPI, but my idea would be to send a "test"-signal to the SD.  Googling for Arduino+SD+SPI+returned many results but until now I did not find a sample.
Any Ideas?

Thanks a lot in advance.


If card detect is connected to a digital pin you enable the pull up and do a digitalRead.  You get low if the card is in the socket.

Making your app work is not so easy.  The app must know the card is going to be removed and close any files open for write.

The app then tells the user it is ok to remove the card.

If a new card is inserted, it must be initialized by calling begin().

This is no different than using removable storage with a PC or Mac.  You can't just pull a card from the socket while an app is accessing it.


The only sure way to detect removal of the SD is to use the SD detect switch on the SD socket.

Sample code?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up