Community
cancel
Showing results for 
Search instead for 
Did you mean: 
idata
Community Manager
1,444 Views

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

Jump to solution

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

# include

# 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;

}

}

1 Solution
RRobe17
New Contributor I
312 Views

If I am not mistaken this card is mounted and is visible to the Intel Edison.

Normally when an SD card is seen, it is mounted so the OS can see it.

I am not sure the name of the device that was created for your SD card. However this is the general command to umount.

umount /media/sdcard

umount makes sure that all data is written (flushed) to the SD card before you can remove it.

View solution in original post

8 Replies
idata
Community Manager
312 Views

Hi Jason,

I suggest you to use a LED in order to be able to see when the SD is open or close; if you close the SD interface with close you shouldn't have problems while removing the SD card. With this you also be able to power down the board when the sketch is not using the data from the SD card, this will prevent the missing data.

 

https://www.arduino.cc/en/Reference/SD https://www.arduino.cc/en/Reference/SD

 

I hope this approach helps.

Regards,

 

Charlie
idata
Community Manager
312 Views

Did you look at my sketch? It is written in such a way that I can be confident that the SD card file object is closed (with file.close()) before I remove the card from the board. It is also written in such a way that I can verify that there is new data written to the card. Yet, even though I am absolutely confident that I wrote new data to the card and that I closed the file object with file.close(), the data that was just written to the card is sometimes missing after I remove the SD card.

-Jason

RRobe17
New Contributor I
312 Views

I do not think this is a code issue. I think this is an issue with data not being flushed to the SD card before you remove it.

Have you tried to unmount the card before removing it?

idata
Community Manager
312 Views

No, how do I do that?

-Jason

RRobe17
New Contributor I
313 Views

If I am not mistaken this card is mounted and is visible to the Intel Edison.

Normally when an SD card is seen, it is mounted so the OS can see it.

I am not sure the name of the device that was created for your SD card. However this is the general command to umount.

umount /media/sdcard

umount makes sure that all data is written (flushed) to the SD card before you can remove it.

View solution in original post

idata
Community Manager
312 Views

Hi Jason,

 

 

Did you try with umount as Romonaga suggested?

 

 

Regards,

 

Charlie

 

idata
Community Manager
312 Views

Yes, I just replied to their message.

-Jason

idata
Community Manager
312 Views

That worked for me. Thanks.

-Jason

Reply