Detect SD card inserted / removed?

What is the best way to handle an SD card that a user can insert or remove?
begin() is working just fine and will report a card error if the card is not there but I cant work out how to detect that it has been removed subsequently.

I can see that sd.begin is a wrapper for [card / volume / route] which refers to a directory structure, the root, on a volume, on a card.
It also seems reasonable to assume that if card, volume and route are constructed when sd.begin is called then doing that again would be a bad idea unless it had been undone somehow first.

Speciffics ...
If an initialised card is removed and then replaced should it still work and is there any risk to the card or its content?
Is there a way to test if a card is still ok sometime after it was first initialised?
Assuming an absent card can be detected, what needs to be done before a new card can be initialised.

I am just going to leave it in place for now but my final project cant work that way !!

I have tried looking at sd2card and sdVolume but cant make head nor tail of them ... Much to learn I feer

Thanks
Al

Depending on the hardware, some sockets have a card detect (CD) signal, which usually just makes connection to ground when a card is inserted.

I just came across this topic and was wondering how to work with the CD signal. How exactly do I need to wire it to my arduino (pro mini)? On the adafruit-page it says "You should connect a pull up resistor (10K or so) and wire this to another pin if you want to detect when a card is inserted" but i don't really understand this sentence.
The other question is how i need to implement this into my code. do i need to analogRead this pin or work with another SD.begin or something similar?

It will work with digitalRead(), but it needs a pullup resistor. Might as well use the internal pullup, so just connect it to an unused pin and set the pin up with

pinMode(n, INPUT_PULLUP);

More info here: http://arduino.cc/en/Tutorial/DigitalPins

Thanks for that fast respond Jack Christensen!

i still can't fix my problem and still have no clue why it won't work properly. probably you can help me again?

i'm getting three value from analog.read and store them on my adafruit sd breakout board. i've included a reset function which should reset the entire board if the sd is not inserted when i start the program (which works fine except that it won't print my serial.println anymore when i connect my resetPin with the RST) but once the program has started it doesnt stop even when i remove the sd. with the card detect option i wanted to try another version of checking if there is an sd but it should actually already post "error opening datalog.txt" without the cd!? and also since i've implemented that reset-function ( digitalWrite(resetPin, HIGH); ) serial.println("test"); doesnt show anymore on my monitor....

here's my code:

#include <SD.h>

const int chipSelect = 10;
const int akkuA = 9;
const int akkuB = 8;
const int resetPin = 7;
const int cdPin = 6;
String dataString;
int loopA;
int loopB;
int loopC;

// LED's 
int redPin = 2;
int grnPin = 3;
int bluPin = 4;



//---------------------------
// setup
//---------------------------
void setup() {
  digitalWrite(resetPin, HIGH);
  pinMode(resetPin,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("reset");
  
  analogReference(INTERNAL);
//  analogRead(0);
//  pinMode(13,OUTPUT);  //evtl löschen???
  
//---SD-----
  Serial.print("Initializing SD card...");
  pinMode(10,OUTPUT);
  if(!SD.begin(chipSelect))  {
    Serial.println("Card failed, or not present");
    delay(1000);
    digitalWrite(resetPin, LOW);
  }
  Serial.println("card initialized");


  pinMode(akkuA,OUTPUT);
  pinMode(akkuB,OUTPUT);
  
  pinMode(redPin,OUTPUT);
  pinMode(grnPin,OUTPUT);
  pinMode(bluPin,OUTPUT);
  
  pinMode(cdPin,INPUT);
}

//---------------------------
// loop
//---------------------------
void loop() {
  
  
//----Messungen----

  messung();
  float voltA = loopA  * (5.0 / 20460.0);
  float diff = (loopB-loopC) * (5.0 / 20460.0);
  
  Serial.println("TEST");
  Serial.println(digitalRead(cdPin));

//----Umwandlung + Zusammenfügen----
  char spanA[10];
  dtostrf(voltA,1,3,spanA);
  
  char spanB[10];
  dtostrf(diff,1,3,spanB);
  
  
//----dataString zurücksetzen + Werte speichern----
  dataString=""; //nötig?
  dataString = String(spanA) + "," + String(spanB);

  
//----Speicherung-----
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  } 
  else {
    Serial.println("error opening datalog.txt");
//      Serial.println("resetting");
//      delay(10);
      digitalWrite(resetPin, LOW);
  }
  
Serial.println("TEST");

//----Laden?---- // 3.3V=675.18 / 3.7V=757.02
  if (voltA < 3.3) {
  digitalWrite(akkuA,HIGH);
  }
  else {
  digitalWrite(akkuA,LOW);
  }
  

  if (voltA > 4.1) {
  digitalWrite(akkuB,HIGH);
  }
  else {
  digitalWrite(akkuB,LOW);
  }


//_____RGB______ Grün>3.7 / Gelb 3.5-3.7 / Rot<3.5 / voltA< 3.3 --> rot blinken / mehr als 4.1 - blau dauerhaft blau
  if (voltA > 3.3) { 
    analogWrite(redPin,130);
    analogWrite(grnPin,0);
    analogWrite(bluPin,0);
  }
  
  if (voltA > 3.5) { 
    analogWrite(redPin,130);
    analogWrite(grnPin,30);
    analogWrite(bluPin,0); 
  } 

  if (voltA > 3.7) { 
    analogWrite(redPin,0);
    analogWrite(grnPin,130);
    analogWrite(bluPin,0);
  }
  
  if (voltA > 4.1) { 
    analogWrite(redPin,0);
    analogWrite(grnPin,0);
    analogWrite(bluPin,130); 
  }


//----Delay zwischen Messungen
  Serial.println();
  delay(1000);
  
  
}

//----Internal Readconfig----
long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

void messung() {
  loopA=0;
  loopB=0;
  loopC=0;
  
  for (int looper = 0; looper < 20; looper++) {
    readVcc();
    int sensA = analogRead(A0); 
    loopA = loopA+ sensA;
    
    readVcc();
    int sensB = analogRead(A1); 
    loopB = loopB + sensB;
    
    readVcc();
    int sensC = analogRead(A2); 
    loopC = loopC + sensC;
  }
  


}

can you find any errors?

For starters, unless there is an external pullup resistor on the CD signal, you should use

  pinMode(cdPin,INPUT_PULLUP);