can arduino read current size/capacity of one SD card?

Hi all,
I would like to know can Arduino read the current size/capacity of one SD card? I know the GetInfo code for SD card, but I don't think the code can read the current size of the SD card; if let say the SD card is used for logging purpose, and every time it is logging, the program needs to know the size of SD card so that data can be stored in the SD card and when it reaches the limit, data will then be stopped/transferred to other SD card.
Does anyone know this? Hopefully you can reply. Thanks in advance~

Of course it "can", as the SD card uses SPI for all the data transmission. but, i do not konw how .. :slight_smile:

http://arduino.cc/en/Tutorial/CardInfo may help but I have no experience of using the functions.
an extract from the code

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

As an alternative how about writing to the first card until the write fails then continuing with the second card ? This would also cover the situation where the first card fails for some reason. Mind you, I do wonder what you are going to do when the second card fills up.

You really can't get the size of a card with SD.h and be able to write to the card. SD.h is a wrapper for a very old version of SdFat and CardInfo uses the underlying SdFat library directly. Direct use of the underlying SdFat functions is not compatible with use of SD.h in a single program.

SdFat allows concurrent use of these functions. You can get both the total number of blocks in the card and the number of free clusters remaining in the volume. Getting the free space can be slow since the entire File Allocation Table must be scanned.

The SdFat SdInfo example prints the free space and you can see how it is done and how long it takes.

A pointer to the volume object is available as a member function in the SdFat class.

My experience is that counting free blocks is a lot faster after you format the sd card with sd formatter 4. fat16lib pointed this out. Saved my project. Ditch the SD.h library and use sdfat.h

I never felt the need to go back to SD.h once I switched over

UKHeliBob:
http://arduino.cc/en/Tutorial/CardInfo may help but I have no experience of using the functions.
an extract from the code

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks

volumesize *= volume.clusterCount();      // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);




As an alternative how about writing to the first card until the write fails then continuing with the second card ? This would also cover the situation where the first card fails for some reason. Mind you, I do wonder what you are going to do when the second card fills up.

sorry if i ask, how to define 'fails' in the code? does that mean, i have to put a limit of let say size of memory in the SD card?
if the second card fills up, supposedly the code will re check the first SD card's size of memory and then start to store data into it.

fat16lib:
You really can't get the size of a card with SD.h and be able to write to the card. SD.h is a wrapper for a very old version of SdFat and CardInfo uses the underlying SdFat library directly. Direct use of the underlying SdFat functions is not compatible with use of SD.h in a single program.

SdFat allows concurrent use of these functions. You can get both the total number of blocks in the card and the number of free clusters remaining in the volume. Getting the free space can be slow since the entire File Allocation Table must be scanned.

The SdFat SdInfo example prints the free space and you can see how it is done and how long it takes.

A pointer to the volume object is available as a member function in the SdFat class.

oh okay. thanks. but what i face at the moment, by using the GetInfo code from the example, I could not get the left space in the SD card. and by that, how am i going to let the code know whether the first SD card is full or not so that another SD card can be taken over the logging process?

liudr:
My experience is that counting free blocks is a lot faster after you format the sd card with sd formatter 4. fat16lib pointed this out. Saved my project. Ditch the SD.h library and use sdfat.h

I never felt the need to go back to SD.h once I switched over

sorry if i ask (because i am still learning arduino), how can I count the free blocks? and do i need to format the first SD card whenever it is already full? what i planned is to transfer all data for let say after one day of logging to laptop/PC by using wireless communication and if the first SD card is full/failed (based on my calculation if it is correct, the SD card can last long about 2 year of logging data), another SD card will take over the logging process.

Counting block sizes is not your job. You call a function to do it. The function just takes it time to count. Read sdfat sample code. It's all in there. I hate to state the obvious, when you open the sample code called sdinfo.ino

Hello.
Excuse me if I could be elementary, but I am new on this...
I am using esp8266 , how can I use this sdfat.h library in my sketches with esp8266 and Arduino IDE?
THANKS A LOT
Alex