Sunday, August 07, 2016

Filled Under: , ,

Using the SD library to print the directory of files on SD card

library listfiles SD
This example shows how to list the files available in the directory of the SD card. Please click here for more information on the SD library.


Step 1: What You Need?

1 x Arduino Board ( Arduino UNO used in this tutorial) 
1 x Arduino Ethernet Shield (or other board with an SD slot)
1 x Formatted SD card

Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.




The Arduino board has to be connected to the Ethernet Shield and also has a USB cable connected to the computer.


Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.


2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.


Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> SD >> listfiles

Click press the "upload" button (see the button with right arrow mark).

/*
Listfiles This example shows how print out the files in a
directory on a SD card The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 2 Feb 2014
by Scott Fitzgerald This example code is in the public domain. */
#include <SPI.h>
#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 native USB port only
} Serial.print("Initializing SD card..."); if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done."); root = SD.open("/"); printDirectory(root, 0); Serial.println("done!");
} void loop() {
// nothing happens after setup finishes.
} void printDirectory(File dir, int numTabs) {
while (true) { File entry = dir.openNextFile();
if (! entry) {
// no more files
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);
}
entry.close();
}
}

The code above is configured for use with an Ethernet shield, which has an onboard SD slot. In the setup(), SD.begin() names pin 4 as the CS pin. This pin varies depending on the make of shield or board you are using.

The main loop() does nothing because the function that prints out the file directory of "/" of the SD card is called from the setup(). This because we need to see it just once.

The printDirectory function scans through the list of entries and prints on serial every file and directory present. For files the size is printed as well.



Unknown

Author & Editor

My Robot Education Sdn. Bhd. (Robotedu.my) was founded in 2015 as the first robotics education centre in Malaysia to provide Arduino-based robotics courses for youths. Our vision is to be able to provide robotics education to every youth in Malaysia.

2 comments:

  1. Nice blog. Thanks for sharing. Why safety is necessary for everywhere. Door frame metal detector manufacturers in Delhi

    ReplyDelete
  2. Using The Sd Library To Print The Directory Of Files On Sd Card ~ Arduino Tutorial >>>>> Download Now

    >>>>> Download Full

    Using The Sd Library To Print The Directory Of Files On Sd Card ~ Arduino Tutorial >>>>> Download LINK

    >>>>> Download Now

    Using The Sd Library To Print The Directory Of Files On Sd Card ~ Arduino Tutorial >>>>> Download Full

    >>>>> Download LINK Dh

    ReplyDelete

 

  • Copyright © Arduino Tutorial ™ is a registered trademark.
    Designed by Templateism. Hosted on Blogger Templates.