SD card logger shield, can read card ok - but I can't delete or write file ?

I am using Deek Robot Data logging shield v1.0

I am confused because I can read the directory from the SD card without any problems, but I can not write or delete a file which for sure exists on the SD card.

Below is image of the actual card

I attach my sketch code below.

Many Thanks
Les

#include <SD.h>


// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

File myFile;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("\nInitializing SD card...");

  if (!card.init(SPI_HALF_SPEED, 10, 11, 12, 13)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present."); 
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  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);
  
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
 
  
 // to this point everything works fine and a directory listing is displayed 
  
  
 //   Not sure why but this code below fails to write file example.txt 
 
  myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  }
  else {
    Serial.println("example.txt doesn't exist.");  
  }


 //   Not sure why but this code below fails to delete the file which exists on SD card
  Serial.println("Removing P1010085.JPG...");
  SD.remove("P1010085.JPG");

  if (SD.exists("P1010085.JPG")){
    Serial.println("P1010085.JPG exists.");
  }
  else {
    Serial.println("P1010085.JPG doesn't exist.");  
  }
}


void loop(void) {
}

Capture.JPG

Is the card in read-only mode?

Have tried the SD card in my camera and it is not set to read only mode as files can still be stored onto it.

I attach Serial monitor output.

I have also just tried to re-format card using SDFormatter but that has made no difference.

 //   Not sure why but this code below fails to delete the file which exists on SD card
  Serial.println("Removing P1010085.JPG...");
  SD.remove("P1010085.JPG");

  if (SD.exists("P1010085.JPG")){
    Serial.println("P1010085.JPG exists.");
  }
  else {
    Serial.println("P1010085.JPG doesn't exist.");  
  }

So is the problem that the file P1010085.JPG is still on the card after the code has run?

The software would appear to be consistent - you may not be able to remove() the file if it cannot recognise the name, which would appear to be the case if the file cannot be found. Otherwise the message is correct (you delete the file and then it cannot be found).

Not sure where to go from here as I don't use SD cards that much and they have always worked for me.

If you take a look at the attached serial monitor screen grab.
You will see that the file named in the sketch to be deleted does exist, but it is not deleted for some reason.
It is shown in the directory listing before that segment of code executes

Similarly creating a new file fails.

Unexplainable reading the directory works fine yet write and delete do not.

Hi, Not sure if the image of the data logger is yours or one from the internet.
If it is yours and you are still having problems note the battery is in the wrong way around.
Normally it should not affect the card but, a lack of voltage on one of its pins maybe the cause of you not being able to write to the card.

Hope it helps. D

You can't mix the code that uses card, volume and root

Sd2Card card;
SdVolume volume;
SdFile root;

With code that uses SD

SD.remove("P1010085.JPG");

  if (SD.exists("P1010085.JPG")){
    Serial.println("P1010085.JPG exists.");
  }

SD has it's own instance of card, volume, and root. This means there would be two cache buffers if you had initialized SD with an SD.begin() call.

Fortunately you didn't call SD.begin() because even more mysterious things would have happened.

Can you show me how to wiring on this?

leswilk:
I am using Deek Robot Data logging shield v1.0

I am confused because I can read the directory from the SD card without any problems, but I can not write or delete a file which for sure exists on the SD card.

Below is image of the actual card

I attach my sketch code below.

Many Thanks
Les

#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

File myFile;

void setup()
{
// Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

Serial.print("\nInitializing SD card...");

if (!card.init(SPI_HALF_SPEED, 10, 11, 12, 13)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
  Serial.println("Wiring is correct and a card is present.");
  }

// print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }

// print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
 
  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);
 
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
 
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);

// to this point everything works fine and a directory listing is displayed
 
 
//  Not sure why but this code below fails to write file example.txt

myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

// Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  }
  else {
    Serial.println("example.txt doesn't exist."); 
  }

//  Not sure why but this code below fails to delete the file which exists on SD card
  Serial.println("Removing P1010085.JPG...");
  SD.remove("P1010085.JPG");

if (SD.exists("P1010085.JPG")){
    Serial.println("P1010085.JPG exists.");
  }
  else {
    Serial.println("P1010085.JPG doesn't exist."); 
  }
}

void loop(void) {
}

You need a library different from SD.h

The specific library is DS1307RTC and is avalilable in internet: Play-Zone.CH DK Data Logging Shield V1.0

I hope this information is useful.

The ZIP archive contains a folder in which you can find two examples for the use of the library. One about the storage card an the other about the time of readings.

did anyone ever figure this issue out? I got the exact same issue, in searching for answers, found this thread... I ran his program, and it does exactly as he originally described... with 7700 hits on this thread, figured better to ask here than start another- looks like many others might have been looking for same issue with the cheap datalogging shields.

I had already updated libraries, got the RTC working fine, and the SD part initializes, reads the card, lists directory, etc...just cant seem to open,write,delete.

the libraries noted a couple posts up must have been updated in the past couple years, no longer any examples in that link for SD card write, just RTC read, and time setting on the RTC