I am interfacing a 32 GB Micro SD card with a PIC32MX795F512L. I am following the examples provided in Lucio de Jasio book. In one of their example, they are using while( !getCD()); to check if the card is present or not, but this doesn't seem to work in my case, because when I remove the card, it doesn't give any error and moves forward. Below is its code:

// SD card connector presence detection switch
int getCD( void)
// returns TRUE card present
// FALSE card not present
 return !SDCD;

In the hardware, SD-CD pin is connected to RA15. So what is the right way of detecting the card's presence?

enter image description here

Above is the schematic for SD card connections. SD-CD# pin is the pin for card detect. I am checking the voltage change on R34, but there is no change. When the card is removed, it shows 3.5 V and when the card is inserted, it again shows 3.5 V. Am I doing something wrong or does the hardware have some problem?

  • \$\begingroup\$ by reading the state of that pin? \$\endgroup\$
    – PlasmaHH
    Dec 22, 2015 at 11:16
  • \$\begingroup\$ you mean if its high, so card is present \$\endgroup\$
    – user007
    Dec 22, 2015 at 11:17
  • \$\begingroup\$ @PlasmaHH if the card is not getting detected, so that means it will not further initialized. Is card detection really important? \$\endgroup\$
    – user007
    Dec 22, 2015 at 11:35
  • \$\begingroup\$ Card detection can be important. It really depends upon several things and how the software to access the card interface is written. If access is started to the interface without a card present the software could hang waiting for a response. Similarly if a card is being accessed for writing and a removal is not properly handled the card could end up being corrupted. \$\endgroup\$ Dec 22, 2015 at 12:47
  • \$\begingroup\$ SanDisk Datasheet states that card detection can be done by reading the SD-internal 50K pull-up resistor on the SD-CS# pin. In your circuit this is killed by your own pull-up. I would try removing R28, then add a 500K/1M pull-down resistor to ensure a 0 read on that pin when no card is the slot. Not really an issue, but note that although SD Specs clearly states the presence of this pull-up resistor, this is not the case for the old MMC standard. \$\endgroup\$
    – mgruber4
    Apr 11, 2020 at 8:07

1 Answer 1


SD Card detection is typically done by a mechanical switch that is built right into the SD Card socket. It opens and closes when the card is inserted and removed. You will have to investigate how this switch is hooked up in your hardware and test that this works correctly up to the PIC32 pin before you even begin to think about software issues.

Testing the hardware should be as simple as looking at the signal going to the PIC32 pin using a scope probe or multi-meter in volts measure mode. With the PIC32 board powered monitor if there are changes of state in the signal as a card is inserted or removed. If you do not see this working then the switch in the socket is not properly connected. It is typical that one side of these switches is connected to GND. The other side has a pullup resistor to +3.3V and also connects to the PIC32 pin. If working correctly you should see it changing between 3.3V and GND.

Once you have verified the hardware connection it is time to investigate the PIC32 software aspect. The particular pin connection needs to be a general purpose input port pin on the microprocessor. Then that port pin needs to be properly configured to be acting as a digital input pin. If not properly configured then there will be no joy in reading in the SD Card status signal.

  • \$\begingroup\$ Thanks for the useful information. I have edited my question and uploaded the schematic. Please have a look at it. \$\endgroup\$
    – user007
    Dec 22, 2015 at 12:33
  • \$\begingroup\$ @user007 - Well at least you now know that there may be something wrong with the hardware. The SD Card socket could be broken or the socket does not have a switch.... \$\endgroup\$ Dec 22, 2015 at 12:43
  • \$\begingroup\$ I think there is some problem in card connector. Maybe its switch is broken. Thank you very much.! \$\endgroup\$
    – user007
    Dec 22, 2015 at 12:55
  • \$\begingroup\$ I have tested multiple uSD card connectors and found huge quality variation between them. I bought like 8 or 10 different ones to aid in selection of a socket for a project I was working on. I can also say that some of the sockets with the push-push type insert and eject mode of operation can be seriously damaged if the card is forcibly pulled out when it is in the retained position. Thus if you select a push-push type socket you want to look for one, that when used in conjunction with the product case, does not allow normal access to yank the card out without the push eject action. \$\endgroup\$ Dec 22, 2015 at 13:20
  • 1
    \$\begingroup\$ Whether the SD or uSD card socket supports a card detect switch is a different question from the type of card retention mechanism the socket supports. Most sockets will have the card detect switch contact built into the socket. However that said I am sure that there are a few types without the switch to save a few pennies of cost. Note that these switches are pretty delicate, especially for uSD card sockets and can be easily damages. You should look to see of any of the SMT feet of your socket failed to solder properly. Secondly try to find the socket mfg and look up the part data sheet. \$\endgroup\$ Dec 23, 2015 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.