8 Replies Latest reply on Jul 7, 2016 7:18 PM by Jason_Maker

    How do I safely remove an SD card from the Arduino Breakout Board to preserve new data on it?


      I am using an Intel Edison plugged into an Arduino Breakout Board. I wrote a test Arduino sketch to write data to an SD card and read from it to verify that the writing was successful. The sketch works fine. I use a jumper acting as a switch to trigger writing to the SD card. Then I use another jumper acting as a switch to print the contents of the SD card to the serial monitor. It verifies that the data that I just attempted to write to the SD card was successfully stored on the card. However, when I unplug the card and plug it into my laptop, sometimes the new data is not there and sometimes it is there. Also, if I unplug the SD card while leaving the serial monitor open, and then plug the SD card back in to the Arduino Breakout Board, sometimes the new data is not there and sometimes it is there when I read the card with my sketch.


      I have attempted to figure out the proper procedure for unplugging the SD card by waiting 10 seconds after writing to the card, then disconnecting the Arduino Breakout Board from the computer, and only then removing the SD card, yet still the new data is sometimes not there when I check on my laptop or by using the sketch.


      What is the proper procedure for removing the SD card from the Arduino Breakout Board to ensure that you don't lose data that was just written to it?


      Below is the code for my sketch.




      #include <SD.h>
      #include <SPI.h>
      #define READ_SWITCH_PIN 11
      #define WRITE_SWITCH_PIN 4
      bool switch_acknowledged = false;
      File file;
      void setup() {
        pinMode(10, OUTPUT);
          Serial.println("SD card initialization failed");
      void readLines() {
        int linesRead = 0;
        file = SD.open("test.txt");
        while(file.available()) {
          char charIn = file.read();
          if(charIn == '\n')
        Serial.println(" lines read from test.txt\n");
      void writeLine() {
        long timeStamp = micros();
        file = SD.open("test.txt", FILE_WRITE);
        Serial.print("wrote ");
        Serial.println(" to test.txt\n");
      void loop() {
        //waiting to react to change in jumper state
        if(!switch_acknowledged) {
          if(digitalRead(READ_SWITCH_PIN)) {
            switch_acknowledged = true;
          } else if(digitalRead(WRITE_SWITCH_PIN)) {
            switch_acknowledged = true;
        //both jumpers are plugged in
        } else if(!digitalRead(READ_SWITCH_PIN) && !digitalRead(WRITE_SWITCH_PIN)) {
          switch_acknowledged = false;