# How to detect SD Card Insertion Without Leakage Current

I'm using a TI CC1111 SoC to read/write a microSDHC SD card, with an Amphenol card socket. The card detect (CD) pin of the socket is connected to ground when no card is present. When a card is inserted, the connection is broken. (This seems to contradict the datasheet, but it's what my multimeter tells me).

One way to detect card insertion would be to connect a GPIO pin to the CD pin. By default the CC1111's pins are configured with an internal pull-up resistor. If a card were present, I'd read logic high, and otherwise the pin would be pulled to ground. But I worried this might draw excessive current, and sure enough, the CC1111 datasheet warns (pg. 90):

Unused I/O pins should have a defined level and not be left floating. One way to do
this is to leave the pin unconnected and configure the pin as a general purpose I/O
input with pull-up resistor. This is the default state of all pins after reset except
for P1_0 and P1_1 ... Alternatively the pin can be configured as a general
purpose I/O output. In both cases the pin should not be connected directly to VDD or
GND in order to avoid excessive power consumption.


Question 1: Where is the power consumed? Is it just in the internal pull-up resistor? The datasheet says the internal pull-ups are 20k, so with 3.3 V that's about 0.17 mA. Is that what they're talking about? Or perhaps the "excessive power consumption" warning only applies when the pin is configured as an output?

Qustion 2: What is the right way to detect card insertion/removal without wasting power?

And no, I haven't measured the current draw yet, but I will.

-
The current draw from doing this carefully (ie, not accidentally driving the pin as an output) should be of little problem -except when a battery-powered device needs to enter micropower sleep mode. –  Chris Stratton Oct 12 '12 at 11:58
This is a battery-powered device that needs to enter sleep mode, as it turns out. In such cases it will have an SD card inserted, however, so the pin won't be connected to ground. But if the card were removed, it sounds like the battery would slowly be drained through the pull-up resistor. –  David Oct 12 '12 at 14:07
what I would do in that case is use another output to drive an external pullup resistor, and configure a wakeup interrupt on the input. If the card is removed, wake up and turn off the pullup resistor, then go back to sleep. However, this only lets you detect contact "make" - you have to be awake or in power-consuming mode to detect contact "break". Depending on your needs, it might be simpler to just not drive the resistor except when you are awake. –  Chris Stratton Oct 12 '12 at 14:34

I'm pretty sure the datasheet warning is there to cover the case where a pin is tied directly to Vcc or Gnd, but then the firmware configures that pin as an output and tries to drive it to the opposite state (either because of a bug or a subsequent firmware change).

If you really want to play it safe, put a relatively low-value (say, 1K) resistor between the card socket contact and the GPIO pin. When the GPIO pin is used as an input, this resistor forms a voltage divider in conjunction with the internal pullup that still allows the pin to be read as "0". However, if the GPIO pin should inadvertently be configured as an output and driven high, it serves to limit the current flow to a few mA.

-

The warning that you quoted from the datasheet would indeed be avoided by using the internal or an external pullup.

Question 1: Where is the power consumed? Is it just in the internal pull-up resistor? The datasheet says the internal pull-ups are 20k, so with 3.3 V that's about 0.17 mA. Is that what they're talking about? Or perhaps the "excessive power consumption" warning only applies when the pin is configured as an output?

The power is consumed from a floating input on the CMOS inverter. See below for detailed explanation of the power that they are talking about. This is different then the power consumed through the pull up resistor.

Qustion 2: What is the right way to detect card insertion/removal without wasting power?

You have the right idea. Assuming the SD card CD pin is floating when no card is present, and pulled to ground when a card is inserted, then a pullup ~10k-50k Ohm to VDD is the correct way. This removes the floating input concerns that you have.

CMOS Inverter Input Stage

Excess power is consumed if the pin microcontroller input pin is left floating. This is because it is a CMOS inverter configuration similar to shown below:

Non-Floating Inputs

Under normal operation the digital input should only be at a value of VCC or GND. This means that only one of the transistors will be on at a time and thus no current (or very little) will be traveling from VCC to GND.

The diagram below shows the two states that the inverter will be in:

(courtesy http://www.egr.msu.edu/classes/ece410/demlow/ lecture notes)

Floating Inputs

Now if the input signal is somewhere between VCC and GND, both of the FETs will enter their linear operating region and will both conduct. Now current is flowing from VCC to GND through the FETs and consuming power. The below graph is the I-V characteristics of the CMOS inverter:

The current consumption peaks somewhere between GND and VCC depending on the FETs.