Interfacing SD card with LCD using Arduino Mega

Hello Friends,

Can anyone guide me how to display files and directory in SD card on LCD using Arduino Mega... I tried the default program example in Arduino IDE and tried to change root.ls to display on LCD but i could not, its getting printed on Serial Monitor. Is there any way to implement this please suggest me ?

Simply send to the LCD display what your current code sends to the Serial Monitor.

The code examples for both devices will give hints on the code to use for reading and writing.

Thank you for replaying !!!

In the below code the root.ls(LS_R | LS_DATE | LS_SIZE); prints directly on serial monitor and we can force it to print on LCD.

// include the SD library:
#include <SPI.h>
#include <SD.h>

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

// 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
const int chipSelect = 6;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
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?");
return;
} else {
Serial.println(“Wiring is correct and a card is present.”);
}

// print the type of 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”);
}

// 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”);
return;
}

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

volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we’ll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);

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

}

Add an lcd object and initialize it in setup (). Next replace serial.println by lcd.print.

It might require a bit more but that is the basics. As DrAzzy says, look at the examples.

PS Please use code tags when posting code.

Hi Sterretje, Thank you for valuable comment... I know that it can be displayed by using lcd.print(); or lcd.write(); but my question how to print the root.ls(LS_R | LS_DATE | LS_SIZE) content on LCD. Because when we call root.ls(LS_R) the content are directly displayed on Serial Monitor. because in class of Sdfile ..they have programmed to display on serial monitor directly.

Sorry, I missed the root.ls(); I also don’t know what it does but you can probably use (or modify) the standard example that iterates through the directories.

It looks like the codes for the examples are currently non-existing but I think that this is one of them that lists the files (source: http://stackoverflow.com/questions/16971084/how-to-recheck-file-in-arduino-sd-card).

#include <SD.h>

File root;

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


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(10))
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop()
{
  Serial.println("hit any key then enter to run the list");
  while (!Serial.available())
  {
    ;
  }
  Serial.read();
  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");

  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs)
{
  while (true)
  {

    File entry =  dir.openNextFile();
    if (! entry)
    {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++)
    {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory())
    {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    }
    else
    {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
  }
}

Its ok bro not problem....but all the examples of SD library.... displays files in SD on Serial Monitor by default.. Now i want to display it on LCD that my problem even that printDirectory() command also prints directly on Serial Monitor.

The above one purely uses serial.print so could be easily adapted to LCD.

Bro it can be printed.... but you'll not be able to print that root.ls(LS_R) on LCD.

shanthasuresh:
Its ok bro not problem....but all the examples of SD library.... displays files in SD on Serial Monitor by default.. Now i want to display it on LCD that my problem even that printDirectory() command also prints directly on Serial Monitor.

So modify it to use the LCD.

shanthasuresh:
Bro it can be printed.... but you'll not be able to print that root.ls(LS_R) on LCD.

No idea. But if a function/method in a library does not do what you want it to do, you either hack the library or you don't use it and write a function that will be able to do what you need (printDirectory in this case).

Life can be very easy if you don't make it complicated :wink: