Go Down

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


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.


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

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


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.


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!


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.


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() {
 if (!sd.begin(sdChipSelect)) {
   Serial.println("sd.begin failed");
 // Get the count of free clusters in the volume.
 uint32_t freeClusters = sd.vol()->freeClusterCount();
 Serial.print("Free clusters: ");
 // Calculate free space in KB.
 float freeKB = 0.512*freeClusters*sd.vol()->blocksPerCluster();
 Serial.print("Free space: ");
 Serial.println(" KB (KB = 1000 bytes)");

void loop() {}

Here is the output for a 8 GB card.

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


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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131