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.