Detect Whether SD Card Has Been Unplugged Teensy 4.1

Status
Not open for further replies.

grinch

Well-known member
Hi, I am working on a project using the Teensy 4.1's built in SD card reader to read data from text files. I have pretty much everything working, just minor tweaks at this point. One thing I've been trying to figure out is preventing my program from crashing if the SD card gets unplugged mid program. I was thinking there would be a simple function to test whether an SD card was plugged in, but looking at the documentation I haven't been able to find one. Something like SD.exists() or SD.isPluggedIn() type function.

Essentially, I'm finding if the SD card gets unplugged mid runtime, my program crashes when I get to a file.read() statement. I'd like to add a conditional that avoids these statements if the SD card is unplugged so that the other parts of the program can keep running. Something like:

Code:
if(SD.isPluggedIn()){
char data = File.read()
//do something with the data
}else{
//throw unplugged warning, test for replug
}

Is there an easy way to do this?
 
Hi grinch,
Here is how I deal with it:

Code:
/***************************** GetConfigFile ***************************/
void GetConfigFile()
{
  if(MenuActive)
  {
    tft.fillScreen(HX8357_NAVY);
    tft.setTextColor(HX8357_YELLOW);  
    tft.setCursor(0, 0);
    tft.setFont(Arial_18_Bold);
    tft.println("Please wait while MMConfig.ini");
    tft.println("is read from the SD card.");
    tft.println();
  }
  
  tft.setFont(Arial_14);
//  tft.println("Initializing SD card reader...");
  if (!SD.begin(chipSelect)) 
  {
    Serial.println("SD initialization failed!");
    tft.println("SD card bad or not inserted.");
  }
  else
  {
    Serial.println("SD initialization success.");
//    tft.println("SD initialization success.");
  }

  // Open config file and list contents to serial port
  ConfigFile = SD.open("MMConfig.ini");
  if (ConfigFile) 
  {
//    tft.println("Found MMConfig.ini");
    Serial.println("MMConfig.ini");
    Serial.print("  ");
//    tft.print("  ");
    
    // read from the file until there's nothing else in it:
    TimeIt = millis();
    while (ConfigFile.available()) 
    {
      Rchar = ConfigFile.read();
      InBuf += String(Rchar);
      Serial.write(Rchar);
//      tft.print(String(Rchar));
      if((Rchar == 10 && ConfigFile.available() > 1) || ConfigFile.available() == 0)
      {
//        Serial.print("InBuf: ");Serial.println(InBuf);
        ParseInBuf();
        InBuf = "";
        Serial.print("  ");
//        tft.print("  ");
      }

      if(millis() - TimeIt > 10000)
      {
        Serial.println("SD Card read timed out");
        break;
      }
    }
    // close the file:
    ConfigFile.close();
    Serial.println();
  }
  else 
  {
    // if the file didn't open, print an error:
    Serial.println("Error opening MMConfig.ini");
    tft.println("Error opening MMConfig.ini");
  }

}
 
Thanks for the response. How does this code you posted check whether the SD card is unplugged? I see you are using SD.available() File.available() and File.read() in your reading function, but that hasn't worked for me. File.available() seems like it returns based on buffered information rather than what's on the SD card, and my code crashes as soon as I get to File.read().
 
The following snippet does not crash
Code:
  uint32_t data;
  int ndat = file.read(&data,4);
  if(ndat<0) Serial.println("SD error");

OK, a return of -1 indicated SD read failure, which may be also due to different reason
Important is that program does not crash, but continues to run. The rest is error-handling on your side.
 
Status
Not open for further replies.
Back
Top