Corrupted SD card if I work with blocks from sdfat library


I’m doing a sketch with the sdfat library, for high speed data logging (GPS+accelerometer).
I’m using the RawWrite.ino from the examples (modified version). I mean I create N blocks in the contiguous file. But after serveral tests, I “break” the SD card (I’m done with 2 cards).
The SD is corrupt and I can’t format it.
In one SD, i get this error in linux when I try mounting it ->Error mounting: Mount: /dev/sdb1: Can’t read superblock.

Why I’m “breaking” the SD card? There are any problem with the creation the blocks in SD card? Is there any way to fix the card?

I use this code for the SD card (only paste the code associate with the SD card):

void setup() {

if (!sd.begin(SS, SPI_FULL_SPEED)) sd.initErrorHalt(); 
  // delete possible existing file
  // create a contiguous file (BLOCK_COUNT = 10000UL)
  if (!file.createContiguous(sd.vwd(), "TEST.CSV", 512UL*BLOCK_COUNT)) {
    error("createContiguous failed");

  // get the location of the file's blocks (uint32_t bgnBlock, endBlock;)
  if (!file.contiguousRange(&bgnBlock, &endBlock)) {
    error("contiguousRange failed");
  // clear the cache and use it as a 512 byte buffer
  pCache = (uint8_t*)sd.vol()->cacheClear();

  // fill cache with eight lines of 128 bytes each
  memset(pCache, ' ', 512);  
  for (uint16_t i = 0; i < 512; i += 128) {
    // put line number at end of line then CR/LF
    pCache[i + 126] = '\r';
    pCache[i + 127] = '\n';

  // tell card to setup for multiple block write with pre-erase
  if (!sd.card()->erase(bgnBlock, endBlock)) error("card.erase failed 1");
  if (!sd.card()->writeStart(bgnBlock, BLOCK_COUNT)) {
    error("writeStart failed 1");


void loop(){

//with a for, I save values in pCache
 for (uint8_t i = 0; i < SIZE; i++) {
    pCache[i] = any_value();

// each block (512 bytes), I write data in SD. (512 bytes is when line == 3)
if(line == 3){
  if (!sd.card()->writeData(pCache)) error("writeData failed");
  pCache = (uint8_t*)sd.vol()->cacheClear();


You must post all of your code. It's not clear how many blocks you write or how you end the write. You must be writing over the MBR.

Don't format SD cards with Linux utilities they can't format some corrupt cards and don't produce a format that complies with the SD standard. The same is true for Mac and Windows utilities.

Use the SD Association's Formatter

You can also use the SdFat example sketch SdFormatter.ino located int the SdFat/examples/SdFormatter folder.

I use SDFormatter application to format SD card. But now the SD is corrupted and i can’t format.

I define this number of blocks in the contiguous file (const uint32_t BLOCK_COUNT = 10000UL), but I don’t write all of blocks

The end the write is “brute force”… I remove the card from the reader. This may break the card? Is necessary end the write with this line “sd.card()->writeStop()”?

The end the write is "brute force"

No you can't just pull the card. You must call "sd.card()->writeStop()" and close the file with file.close().

The SdFormatter.ino program can format any SD no matter how corrupt. SdFormatter.ino erases the SD so all blocks contain zeros and then formats the SD.

Post the output from SdFormatter.ino. It should look like this:

This sketch can erase and/or format SD/SDHC cards.

Erase uses the card's fast flash erase command. Flash erase sets all data to 0X00 for most cards and 0XFF for a few vendor's cards.

Cards larger than 2 GB will be formatted FAT32 and smaller cards will be formatted FAT16.

Warning, all data on the card will be erased. Enter 'Y' to continue: Y

Options are: E - erase the card and skip formatting. F - erase and then format the card. (recommended) Q - quick format the card without erase.

Enter option: F Card Size: 7580 MB, (MB = 1,048,576 bytes)

Erasing ................................ ............................ All data set to 0x00 Erase done

Formatting Blocks/Cluster: 64 FAT32 ................ Format done

If SdFormatter.ino fails, try the SD Association's formatter which also can format any SD no matter how corrupt.

If the SD Association's formatter fails, you have a bad SD card. SD cards are 3.3 V devices and you can ruin them by applying voltages greater than 3.6 volts to power or signal lines.

If the SD Association's formatter works but the SdFat formatter fails, you have a problem with the SD module or the way it is wired to the Arduino.

Hi, is working fine now. I added it to my code the "writeStop()" and the "file.close()". And the most important... I have changed the conection of the SD card with Arduino (5V to 3.3V) :~

Thanks fat16lib