Go Down

Topic: SD card: cannot read or write files, but CAN read SD card info (CardInfo) (Read 692 times) previous topic - next topic

pizzle

Hello all,

I'm trying to run the example SD card stuff on a 328 5V Nano clone with a Chinese SD card module with 3.3V regulator and level shifter. The weird thing is that I can run the CardInfo example just fine, but reading, writing or creating a file is a no-go.

I've tried 2 cards, one 2GB Sandisk and an 8GB Kingston. Both cards have been formatted with SD Card Formatter under Windows 10.

Thanks for all the help in advance!

Below is the output of the CardInfo example plus code from the Files example.

Code: [Select]

Initializing SD card...Wiring is correct and a card is present.

Card type:         SDHC
Clusters:          244960
Blocks x Cluster:  64
Total Blocks:      15677440

Volume type is:    FAT32
Volume size (Kb):  7838720
Volume size (Mb):  7655
Volume size (Gb):  7.48

Files found on the card (name, date and size in bytes):
SYSTEM~1/     2018-01-27 20:57:36
  INDEXE~1      2018-01-27 20:57:36 76
  WPSETT~1.DAT  2018-01-27 20:57:52 12
TEST          2018-01-27 20:58:12 7
TEST doesn't exist.
error opening TEST
error opening TEST


Code: [Select]

#include <SPI.h>
#include <SD.h>

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

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = A3;

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


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

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.println();
  Serial.print("Card 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");
    while (1);
  }

  Serial.print("Clusters:          ");
  Serial.println(volume.clusterCount());
  Serial.print("Blocks x Cluster:  ");
  Serial.println(volume.blocksPerCluster());

  Serial.print("Total Blocks:      ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  Serial.println();

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1KB)
  Serial.print("Volume size (Kb):  ");
  Serial.println(volumesize);
  Serial.print("Volume size (Mb):  ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Gb):  ");
  Serial.println((float)volumesize / 1024.0);

  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);

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

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("TEST", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to TEST...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening TEST");
  }

  // re-open the file for reading:
  myFile = SD.open("TEST");
  if (myFile) {
    Serial.println("TEST:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening TEST");
  }

}

void loop(void) {
}

knut_ny

I have experienced similar.
I formatted the cards with an arduino formatter.  Worked OK with arduino, but I could not open those files on my PC..
When formatted on PC; worked OK on the PC, but NOT with arduino.

I got 10 , 2GB SD (noname) card that behaves this way..
Ny

Go Up