Read and write from SD card along with calculating space and free space?

Hello!! everyone i am using SDfat library from this link

My requirement is to calculate free space, total space and perform logging operations like reading and writing from and to memory card.

Here is my code free space and user space calculation.

#include <SdFat.h>

const uint8_t ChipSelect=4;

Sd2Card Card;
SdVolume Volume;
SdFile myFile;
SdFat intSd;
uint32_t cardSize;
uint32_t cardErase;

void setup()
{
  Serial.begin(115200);
  Serial.println("Initializing Memory Card");
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  if(!Card.init(SPI_HALF_SPEED,ChipSelect))
  {
    Serial.println("Card Init Failed");
    return;
  }
  Serial.println("Card Initialized");
  
  cardSize = Card.cardSize();               // This will return the total number of blocks in the card.  
  if(cardSize == 0)
  {
    Serial.println("Card Size Failed");
  }
  float cardSizeKB = cardSize*512/1024.0;  // 1 Block = 512 bytes
  Serial.print(cardSizeKB);
  Serial.println(" KB");
  
  float cardSizeMB = cardSizeKB/1024.0;  // 1 MB = 1024KB
  Serial.print(cardSizeMB);
  Serial.println(" MB");
  
  float cardSizeGB = cardSizeMB/1024.0;  // 1 GB = 1024MB
  Serial.print(cardSizeGB);
  Serial.println(" GB");
  
  Serial.print("Card Type = ");
  switch(Card.type())
  {
    case SD_CARD_TYPE_SD1:
    Serial.println("SD V1");
    break;
    case SD_CARD_TYPE_SD2:
    Serial.println("SD V2");
    break;
    case SD_CARD_TYPE_SDHC:
    Serial.println("SD High Capacity Card");
    break;
  }
  Volume.init(&Card);
  uint32_t clusterSize = Volume.freeClusterCount();
  Serial.print("Free Cluster: ");
  Serial.println(clusterSize,DEC);
  
  float freeSpace = clusterSize*512*Volume.blocksPerCluster();
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpace);
  Serial.println("bytes");
  
  float freeSpaceKB = freeSpace/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceKB);
  Serial.println("KB");
  
  float freeSpaceMB = freeSpaceKB/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceMB);
  Serial.println("MB");
  
  float freeSpaceGB = freeSpaceMB/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceGB);
  Serial.println("GB");
}

void loop()
{
}

This code is working fine.

Now the code to read and write to SD Card is as follow:-

#include <SdFat.h>

const uint8_t ChipSelect=4;

Sd2Card Card;
SdVolume Volume;
SdFile myFile;
SdFat intSd;
uint32_t cardSize;
uint32_t cardErase;

void setup()
{
  Serial.begin(115200);
  Serial.println("Initializing Memory Card");
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

// Writing to Files
  if (!intSd.begin(ChipSelect,SPI_HALF_SPEED))
  {
    Serial.println("SD Card Not Initialized");
    return;
  }
  Serial.println("SD Card Initialized");
  // open the file for write at end like the Native SD library
  if (!myFile.open("testing.txt", O_RDWR | O_CREAT | O_AT_END))
  {
   Serial.println("opening test.txt for write failed");
   return;
  }
  Serial.println("File Opened Successfully and now writing to file");
  myFile.println("Hello!! SD Card Testing");
  myFile.close();  // Close the file
  
  Serial.println("Writing done and now opening file in Reading mode");
  if (!myFile.open("testing.txt", O_READ))
  {
   Serial.println("opening test.txt for reading failed");
   return;
  }
  Serial.println("File Opened Successfully and now reading from file");
  int data;
  while ((data = myFile.read()) >= 0) 
  Serial.write(data);
  // close the file:
  myFile.close();
}

void loop()
{
}

How to merge these both codes?
Sd2Card class doesn’t works with FAT read write operation.
while SdFat class doesn’t work with freeSpace calculation and total space opeartions.

while SdFat class doesn’t work with freeSpace calculation and total space opeartions.

You can get the free cluster count in SdFat.

You probably want the total cluster count, not SD size. Operating systems like Windows give you those numbers for total space, free space, and used space.

This program:

#include <SdFat.h>

SdFat sd;

void setup() {
  Serial.begin(9600);
  if (!sd.begin(10)) {
    Serial.println(F("sd.begin failed"));
    return;
  }
  Serial.print(F("Card Size MB:   "));
  Serial.println(0.000512*sd.card()->cardSize());  
  float clusterSize = 0.000512*sd.vol()->blocksPerCluster();
  Serial.print(F("Total space MB: "));
  Serial.println(clusterSize*sd.vol()->clusterCount());
  Serial.print(F("Free Space MB:  "));
  Serial.println(clusterSize*sd.vol()->freeClusterCount());
}
void loop() {}

Prints this for a 32 GB card:

Card Size MB: 31914.98
Total space MB: 31902.40
Free Space MB: 31897.35

I use 1,000,000 bytes for MB, the same as disk and SD manufactures and the ISO Standard definition of MB.

Thanks for you help, that was extremely helpful.

I wrote this code and it works perfectly fine.

#include <SdFat.h>

const uint8_t ChipSelect=4;

char buffer[] = "This is xpress embedo, i am testing SD Card Library with Arduino Mega Board\r\n";
char filename[20] = {0};
  
SdFat intSD;

uint32_t cardSize;
uint32_t cardErase;

void setup()
{
  Serial.begin(9600);
  Serial.println("Initializing Memory Card");
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  if(!intSD.begin(ChipSelect,SPI_HALF_SPEED))
  {
    Serial.println("Card Init Failed");
    return;
  }
  Serial.println("Card Initialized");
  
  cardSize = intSD.card()->cardSize();               // This will return the total number of blocks in the card.  
  if(cardSize == 0)
  {
    Serial.println("Card Size Failed");
  }
  float cardSizeKB = cardSize*512/1024.0;  // 1 Block = 512 bytes
  Serial.print(cardSizeKB);
  Serial.println(" KB");
  
  float cardSizeMB = cardSizeKB/1024.0;  // 1 MB = 1024KB
  Serial.print(cardSizeMB);
  Serial.println(" MB");
  
  float cardSizeGB = cardSizeMB/1024.0;  // 1 GB = 1024MB
  Serial.print(cardSizeGB);
  Serial.println(" GB");
  
  Serial.print("Card Type = ");
  switch(intSD.card()->type())
  {
    case SD_CARD_TYPE_SD1:
    Serial.println("SD V1");
    break;
    case SD_CARD_TYPE_SD2:
    Serial.println("SD V2");
    break;
    case SD_CARD_TYPE_SDHC:
    Serial.println("SD High Capacity Card");
    break;
  }
    
  uint32_t clusterSize = intSD.vol()->freeClusterCount();
  Serial.print("Free Cluster: ");
  Serial.println(clusterSize,DEC);
  
  float freeSpace = clusterSize*512*intSD.vol()->blocksPerCluster();
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpace);
  Serial.println("bytes");
  
  float freeSpaceKB = freeSpace/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceKB);
  Serial.println("KB");
  
  float freeSpaceMB = freeSpaceKB/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceMB);
  Serial.println("MB");
  
  float freeSpaceGB = freeSpaceMB/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceGB);
  Serial.println("GB");
  
  char date = 0x18;
  char month = 0x01;
  char year = 0x15;
  char hour = 0x20;
  char minute = 0x30;
  char second = 0x00;
  
  filename[0] = ((date>>4)&0x0F)|0x30;
  filename[1] = ((date)&0x0F)|0x30;
  filename[2] = '-';
  filename[3] = ((month>>4)&0x0F)|0x30;
  filename[4] = ((month)&0x0F)|0x30;
  filename[5] = '-';
  filename[6] = ((year>>4)&0x0F)|0x30;
  filename[7] = ((year)&0x0F)|0x30;
  filename[8] = '.';
  filename[9] = 't';
  filename[10] = 'x';
  filename[11] = 't';
  filename[12] = 0;
  Serial.println(filename);
}

void loop()
{
  if(Serial.available())
  {
    char ch = Serial.read();
    if(ch == 'w')
    {
      // Write to file
      SD_Write(filename,buffer);
    }
    else if(ch == 'r')
    {
      //Read from file
      SD_Read(filename);
    }
  }
}

void SD_Read(char *filename)
{
  SdFile myFile;
  if (!myFile.open(filename, O_READ))
  {
    Serial.println("opening test.txt for reading failed");
    return;
  }
  Serial.println("File Opened Successfully and now reading from file");
  int data;
  while ((data = myFile.read()) >= 0) 
  Serial.write(data);
  // close the file:
  myFile.close();
}

void SD_Write(char *filename,char*buffer)
{
  SdFile myFile;
  // open the file for write at end like the Native SD library
  if (!myFile.open(filename, O_RDWR | O_CREAT | O_AT_END))
  {
   Serial.println("File Opening Failed");
   return;
  }
  while(*buffer != '\n')
  {
    myFile.print(*buffer);
    buffer++;
  }
  myFile.print('\n');
  myFile.close();  // Close the file
  
}

But when change my filename like this,
filename[0] = ((date>>4)&0x0F)|0x30;
filename[1] = ((date)&0x0F)|0x30;
filename[2] = ‘-’;
filename[3] = ((month>>4)&0x0F)|0x30;
filename[4] = ((month)&0x0F)|0x30;
filename[5] = ‘-’;
filename[6] = ((year>>4)&0x0F)|0x30;
filename[7] = ((year)&0x0F)|0x30;
filename[8] = ‘’;
filename[6] = ((hour>>4)&0x0F)|0x30;
filename[7] = ((hour)&0x0F)|0x30;
filename[8] = '
’;
filename[9] = ((minute>>4)&0x0F)|0x30;
filename[10] = ((minute)&0x0F)|0x30;
filename[11] = ‘_’;
filename[12] = ((second>>4)&0x0F)|0x30;
filename[13] = ((second)&0x0F)|0x30;
filename[14] = ‘.’;
filename[15] = ‘t’;
filename[16] = ‘x’;
filename[17] = ‘t’;
filename[18] = 0;

It stops working i am continuously getting error on serial port that file not created.
How can i solve this problem.