#include <SPI.h> // includ SPI library jeader
#include <SD.h> // include the SD library header
#include <LiquidCrystal_I2C.h> // I2C LCD
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int chipSelect = 10;
void setup() {
// initialize the LCD
lcd.init();
// Turn ON the Backlight
lcd.backlight();
// Clear the display buffer
lcd.clear();
// Print a message to the LCD
lcd.setCursor(0, 0);
lcd.print("Initializing");
lcd.setCursor(0, 1);
lcd.print("SD Card...");
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect
}
Serial.print("\nInitializing SD card...");
delay(1000);
// 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?");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Initialization");
lcd.setCursor(0, 1);
lcd.print("Failed");
while (1)
;
} else {
Serial.println("Wiring is correct and a card is present.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Initialization");
lcd.setCursor(0, 1);
lcd.print("Successfull");
delay(2000);
}
// print the type of card
Serial.println();
Serial.print("Card type: ");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Card Type:");
lcd.setCursor(0, 1);
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
lcd.print("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
lcd.print("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
lcd.print("SDHC");
break;
default:
Serial.println("Unknown");
lcd.print("Unknown");
}
delay(2000);
// 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());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Clusters:");
lcd.setCursor(0, 1);
lcd.print(volume.clusterCount());
delay(2000);
Serial.print("Blocks per Cluster: ");
Serial.println(volume.blocksPerCluster());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Block per Clster");
lcd.setCursor(0, 1);
lcd.print(volume.blocksPerCluster());
delay(2000);
Serial.print("Total Blocks: ");
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Total Blocks:");
lcd.setCursor(0, 1);
lcd.print(volume.blocksPerCluster() * volume.clusterCount());
delay(2000);
// 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);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Volume type is:");
lcd.setCursor(0, 1);
lcd.print("FAT");
lcd.print(volume.fatType(), DEC);
delay(2000);
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);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Volume size (KB):");
lcd.setCursor(0, 1);
lcd.print(volumesize);
delay(2000);
Serial.print("Volume size (MB): ");
volumesize /= 1024;
Serial.println(volumesize);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Volume size (MB):");
lcd.setCursor(0, 1);
lcd.print(volumesize);
delay(2000);
Serial.print("Volume size (GB): ");
Serial.println((float)volumesize / 1024.0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Volume size (GB):");
lcd.setCursor(0, 1);
lcd.print((float)volumesize / 1024.0);
delay(2000);
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);
}
void loop(void) {
}