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?

    Jason_Maker

      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.

       

      -Jason

       

      #include <SD.h>
      #include <SPI.h>
      
      #define READ_SWITCH_PIN 11
      #define WRITE_SWITCH_PIN 4
      
      bool switch_acknowledged = false;
      File file;
      
      void setup() {
        Serial.begin(115200);
        pinMode(READ_SWITCH_PIN, INPUT_PULLUP);
        pinMode(WRITE_SWITCH_PIN, INPUT_PULLUP);
        pinMode(10, OUTPUT);
      
        if(!SD.begin(4))
          Serial.println("SD card initialization failed");
      }
      
      void readLines() {
        int linesRead = 0;
        file = SD.open("test.txt");
        while(file.available()) {
          char charIn = file.read();
          Serial.write(charIn);
          if(charIn == '\n')
            linesRead++;
        }
        file.close();
        Serial.print(linesRead);
        Serial.println(" lines read from test.txt\n");
      }
      
      void writeLine() {
        long timeStamp = micros();
        file = SD.open("test.txt", FILE_WRITE);
        file.println(timeStamp);
        file.close();
        Serial.print("wrote ");
        Serial.print(micros());
        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;
            readLines();
          
          } else if(digitalRead(WRITE_SWITCH_PIN)) {
            switch_acknowledged = true;
            writeLine();
          }
        
        //both jumpers are plugged in
        } else if(!digitalRead(READ_SWITCH_PIN) && !digitalRead(WRITE_SWITCH_PIN)) {
          switch_acknowledged = false;
        }
      }