SDFat - pulling out the card

Hi, I am running a simple datalogger where I write a line with date, time and 6 floats to a single .csv file placed on the microsd card every 5 minutes. I want to pull out the card from time to time in order to copy the .csv file to my pc (and to insert it back of course), however, I do not want to switch off or reset the mcu as there are other processes running.
Now, what is the recommended SdFat scenario for such a requirement? Worst case one measurement might be lost.
p.

Now, what is the recommended scenario for such a requirement?

Light a LED when the file is open. Turn it off when the file is closed. It is safe, then, to remove the card whenever the LED is off.

Even better, use a 4 * 7 segment display, and show the number of seconds to go until the next log time, so you don't pull the card 1 second before the next log time.

My current understanding (from what I've read at the forum) is:

write__new_data_to_file:
{

  1. check whether the card is inserted (needs an additional pin and pullup connected to card's socket sense contact)
  2. init the sdcard (as it might be just newly inserted)
  3. open the log.csv file
  4. write in the new data line
  5. close the log.csv file
    }
    Am I missing something? (provided the probability I pull out the card while writing data to it is small)..

Am I missing something?

Not if you don't plan to remove the SD card. If you do, then some method of knowing when it is safe to remove it is needed.

Yes, it could be done by pressing a button (called "eject" which will close the file immediately), or, maybe there are sockets where you can sense the pressing the card deeper in (when you want remove the card you usally needs to press the card deeper in and then it jumps out)..

BTW, the olimex boards (all with usdcard socket) use a trick with 1meg resistor and card's chipselect - the 1meg is connected to the /CS and ground (no external pull-up used). When no card inserted it reads low. When the card is inserted it reads (most probably :slight_smile: ) a high. Would be great if that would be supported by SDfat, indeed.
p.

The pullup is not a good idea. CS is in output mode while the SD is in use.

If you are writing once every five minutes just init the card each time you write.

With the latest version of SdFat, sdfatlib20120719.zip Google Code Archive - Long-term storage for Google Code Project Hosting..

I would do something like this:

#include <SdFat.h>
SdFat sd;
SdFile myFile;
// LED to flash just after write
const uint8_t LED_PIN = 3;
const uint8_t chipSelect = 10;
void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if (!sd.begin(chipSelect)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("test.txt", O_WRITE | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // write your data
  myFile.println(millis());
  Serial.println('.');
  // close the file:
  myFile.close();
  // delay five minutes
  digitalWrite(LED_PIN, HIGH);
  delay(1000);
  digitalWrite(LED_PIN, LOW);
  delay(290000);
}

You can pull the card after the LED flash.

You can pull the card after the LED flash.
..the chasing a blink every 5minutes could become a new olympic discipline :stuck_out_tongue:
I think the pressing a button or toggling a switch to tell the logger I want the card is a must here, inserting the card back could be sensed via the olimex trick - I will check, thanks..
p.

A switch and LED is a great way to go if missing a point is OK.

Press the switch and wait for the LED. Remove the card and read it. Insert the card and press the switch. The LED goes out when init of the SD is successful.

In most cases card detect doesn't help much when you want to swap cards. I have worked with a lot of users and in the end they don't even use the CD switch.

You can't just pull the card since you may corrupt the file or file system if flush/sync is not called. This means you need to tell the app you want to remove the card. If you install a switch to remove the card, you can use it to indicate the new card has been inserted.

People often fumble with inserting microSD cards. They push them then try to feel if they are inserted which pop them back out.

A switch and LED is much more positive when the switch is also used to indicate card ready. You just need to do a super debounce by waiting a second after the remove press is released before trusting the card ready press.

If you are short of pins, a better use for CS is to light an LED. For normal operation the LED will be mostly lit with a little fast blinking during SD access.

You can indicate a fatal error by blinking with a half second period. This won't be confused with normal operation.

During SD change you can turn the LED off when all files have been closed and it is OK to remove the SD. The LED will light when the new SD has been initialized.

What about a different approach?
May or may not be suitable for you.

Guess you have the ethernet shield if you have the SD reader.
Instead of removing the card what about read/download the file via Telnet.
Your desktop/laptop can request the download periodically.

That SD socket is a very flimsy thing on the shield. I guess it won't last long if one removes the card regularly. I already have difficulties to insert the card and hardly used it. Sometimes have to eject-n-insert two-three times to get it working.

Also, generically speaking, dataloggers have a time/date stamp.
Once the Ethernet Server/Client is set up, it's a brease to sync an RTC to a TimeServer.
Code is available too.

Just my 1/2 cent ...

Most people who log data to an SD do not use the Ethernet Shield. There are now dozens of SD shields/modules.

A network is great if it is suitable.

fat16lib:
Most people who log data to an SD do not use the Ethernet Shield. There are now dozens of SD shields/modules.

This email doesn't add to the possible solutions. As is, it seems to be more of a lecture than anything else.
May I point out, the silly 'ranking system' may be missleading. Just because it says 'newbie' it doesn't mean that the person is inexperienced in electronics and/or programming.

Or research for that matter.

Most people who log data to an SD do not use the Ethernet Shield.

Wondering what your research method was: group size, demographic, distribution, duration, correlation ...
You may want to direct me to the peer-reviewed paper you have written on the subject.

May I suggest to refrain from such lecturing-emails?
Thank you.

The idea of my previous email was to show another way. It may or may not help this particular person. However, during the next few years, anybody who has the same issue may read it and some may re-think the aproach.

Sorry you are so upset. I thought it was strange that you assumed an Ethernet shield was used.

Guess you have the ethernet shield if you have the SD reader.

At least a thousand people have contacted me about data logging and only a small percentage use the Ethernet shield for data logging.

Sorry, I can't refer you to a peer-reviewed paper.

Wondering what your research method was: group size, demographic, distribution, duration, correlation ...
You may want to direct me to the peer-reviewed paper you have written on the subject.

Shields like this one from Adafruit are very popular Adafruit Data logging shield for Arduino [v1.0] : ID 243 : Adafruit Industries, Unique & fun DIY electronics and kits.

Many people use the SparkFun shield SparkFun microSD Shield - DEV-12761 - SparkFun Electronics.

People like these shields for data logging since they have a prototyping area.

Networking is a good solution and a number of people have used Ethernet to upload data from an SD.

The WiFi Shield can be used http://arduino.cc/en/Main/ArduinoWiFiShield.

Another Idea people are using is an Eye Fi card Internet of Things Camera -.

Sorry you are so upset.

No problem. Lets put it behind us.

The WiFi Shield can be used http://arduino.cc/en/Main/ArduinoWiFiShield.

Another Idea people are using is an Eye Fi card Internet of Things Camera -.

Yes. Some sort of wireless communication is an elegant solution.
Also, if hard-wiring is not a problem hard-/software serial, serial to USB is available as a choice.

One really basic approach could be giving an instruction to the board via the Arduino IDE serial connection to print the file to the screen. Then just copy/paste it. (Assuming one is using the Arduino IDE for programming, uploading via the 'standard USB')

Now we talk! Lots of alternatives, people can chose what they fancy!

Cheers!
Cheers!