Go Down

Topic: Using CardInfo example to find memory space of sd (Read 774 times) previous topic - next topic

melis

Hello to everyone I am a beginner in Arduino,

So I started doing some practice. I connected the device with an Adafruit shield and put a micro sd inside. Then I used the example cardInfo to test if it works. Now I want to find out the memory left in the sd card ( not the overall) but free memory in sd card. The problem is I don't know what command to use to take all the file sizes into one variable.

Thanks, Melis.

melis

Code: [Select]
volumesize *= 1024;
  volumesize *= 1024;
  memory_left = volumesize- files ;
  Serial.println("Memory size remaining in SD card: ");
  Serial.print(memory_left);


the variable "files" is the problem . What command should I use to store the sum of the size of the files?

PaulS

Quote
What command should I use to store the sum of the size of the files?

There isn't one command. You need to find all the files on the card, and get the size of each one. Add the individual sizes.

melis

Yes but how do I get the individual size of each file? I think I have to use pointers but I am not sure how to do it!
Thanks by the way!

PaulS

Quote
Yes but how do I get the individual size of each file?

The size() method returns the number of bytes in the file.

Keep in mind that there is a difference between the number of bytes in the file and the number of bytes that the file makes unusable by other files on the SD card.

fat16lib

To find the free space on an SD you need to account for files, directories, and allocation tables.

SD.h uses a very old version of SdFat and does not provide a way to do this.

New versions of SdFat have a function that returns the number of free clusters which gives total free space.

Here is an example SdFat sketch that prints the number of free clusters and the amount free space.

Code: [Select]

#include <SdFat.h>
// Default SD chip select pin.
const uint8_t sdChipSelect = SS;

// File system
SdFat sd;

void setup() {
 Serial.begin(9600);
 if (!sd.begin(sdChipSelect)) {
   Serial.println("sd.begin failed");
   while(1);
 }
 // Get the count of free clusters in the volume.
 uint32_t freeClusters = sd.vol()->freeClusterCount();
 Serial.print("Free clusters: ");
 Serial.println(freeClusters);
 
 // Calculate free space in KB.
 float freeKB = 0.512*freeClusters*sd.vol()->blocksPerCluster();
 Serial.print("Free space: ");
 Serial.print(freeKB);
 Serial.println(" KB (KB = 1000 bytes)");
}

void loop() {}

Here is the output for a 8 GB card.
Quote

Free clusters: 249037
Free space: 8160445.00 KB (KB = 1000 bytes)

melis

Roger that PaulS and fat16lib I will give it a try ! If I make it work or stuck somewhere I'll post it here.

Thank you for your time guys!


Go Up