Program to store data on SD card not working

Hi

I have an Arduino UNO board, SD card reader module and SDHC card of 8 GB. I followed the SD card example provided by Arduino. I want to store data collected by the sensors in a text file on the SD card.
But at the moment I am not collecting any data I am just creating text file and writing random values in it. This is done inside the SD card.
Following is my program:

#include <SPI.h>
#include<SD.h> // SD library
Sd2Card card; // Seting up of variables using the SD utility library functions:
SdVolume volume;
SdFile root;
const int chip_sel = 10; 

void setup()
{
  Serial.begin(9600); // open serial communications and wait for port to open
  Serial.print("\n Initializing SD card...");
  pinMode(10, OUTPUT); //SS pin is 10 on most Arduino boards but not in Mega
 
  /*Check to test the if the SD card is working*/
  digitalWrite (chip_sel, HIGH);
  if (!card.init(SPI_HALF_SPEED, chip_sel)) 
  {
    Serial.println("Initialization failed. Check the following:");
    Serial.println("* Have you inserted the Card?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* Have you changed the chip_sel pin to match your module?");
    return;
  }
  else
  {
    Serial.println("Wiring is correct and SD card detected.");
  }
  /*Printing the type of SD 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");
  }
  
  /*Trying to open the 'volume'/'partition' - it should be FAT16 or FAT32.*/
  if (!volume.init(card))
  {
    Serial.println("Could not find FAT16 or FAT32 partition.\nMake sure the sd card is formatted");
    return;
  }
  
  /*Printing the type and size of the first FAT-type volume(vol)*/
  uint32_t vol_size;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  vol_size = volume.blocksPerCluster();  // Clusters are collections of blocks
  vol_size *= volume.clusterCount();    // There will be lot of clusters 
  vol_size *= 512;                     // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(vol_size);
  Serial.print("Volume size (Kbytes); ");
  vol_size /= 1024;
  Serial.println(vol_size);
  Serial.print("Volume size (Mbytes): ");
  vol_size /= 1024;
  Serial.println(vol_size);
  
  Serial.println("\nFiles found on the SD card (Name, Date and Size in bytes): ");
  root.ls(LS_R | LS_DATE | LS_SIZE); //List all files in the card with date and size

/*Reading and writing in the text file*/
File myFile;
  Serial.print("Initializing SD card...");
   pinMode(10, OUTPUT);
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  // 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.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    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.txt");
  }
  
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // 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.txt");
  }
}

void loop(void)
{
  //Nothing happens after setup
}

My SD card reader module pins are connected to the Arduino in a following manner:

  • GND connected to GND of Arduino
  • 5V connected to 5V of Arduino
  • CS connected to pin 10
  • MOSI connected to pin 11
  • SCK connected to pin 13
  • MISO connected to pin 12

When I upload this program I get the following message on the Serial Monitor:

Initializing SD card...Initialization failed. Check the following:
* Have you inserted the Card?
* Is your wiring correct?
* Have you changed the chip_sel pin to match your module?

In short my whole program is not executed. I don't understand where I have gone wrong.

I'm pretty sure the SS for the SD is pin 4. Pin 10 is the E'net chip.

I tried your code and initialization failed too, until I changed this:

  if (!card.init(SPI_HALF_SPEED, 10))  //<<<<<<<<<<<<<<<    pin 10

to this:

 if (!card.init(SPI_HALF_SPEED, 4))  // <<<<<<<<<<<<<<<<< pin 4

There are two places to make that change.

Then it passed and gave me all the info of my card, as well as doing that test:

Initializing SD card...Wiring is correct and SD card detected.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 2002518016
Volume size (Kbytes); 1955584
Volume size (Mbytes): 1909

Files found on the SD card (Name, Date and Size in bytes):
Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.

Edit: At least, for SD slots on the E'net and Wireless shields it's CS = 4.... there may be other variants?

Hi again,

I made the change like you said but I am still getting the same message. Also I am not using Ethernet shield I am using the sd card reader module.

sd card module.jpg

CS is active low, so to me it seems as if this code actually switches the SD card off before it initialises it, by going high:

  /*Check to test the if the SD card is working*/
  digitalWrite (chip_sel, HIGH);   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< try with this line out?
  if (!card.init(SPI_HALF_SPEED, chip_sel)) 
  {

I am using the SD card reader module.

You are going to have lots of problems with that card.
IMO I would throw that card away and buy this one (it has proper level shifting):

I did that too but it did not work. I chose that module because I saw it working on someone else's Arduino.
If there is no solution to this then I will buy what is mentioned.
Also is there a way where you could change the Chip Select pin, like for Ethernet shield it is pin 4, say if I want to use some other pin of Arduino?