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

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.

Hardware Required

  • Arduino board

  • Ethernet Shield (or other board with an SD slot)

  • Formatted SD card

Circuit

EthernetShield Fritz

image developed using Fritzing. For more circuit examples, see the Fritzing project page

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

Schematics

Ethernet Shield Sch

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

The code below 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.


/*

  Listfiles

  This example shows how print out the files in a

  directory on a SD card.Pin numbers reflect the default

  SPI pins for Uno and Nano models

  The circuit:

   SD card attached to SPI bus as follows:

 ** SDO - pin 11

 ** SDI - pin 12

 ** CLK - pin 13

 ** CS - depends on your SD card shield or module.

        Pin 10 used here for consistency with other Arduino examples

    (for MKRZero SD: SDCARD_SS_PIN)

  created   Nov 2010

  by David A. Mellis

  modified 9 Apr 2012

  by Tom Igoe

  modified 2 Feb 2014

  by Scott Fitzgerald

  modified 24 July 2020

  by Tom Igoe



  This example code is in the public domain.

*/
#include <SD.h>

const int chipSelect = 10;

File root;

void setup() {

 // Open serial communications and wait for port to open:

  Serial.begin(9600);

  // wait for Serial Monitor to connect. Needed for native USB port boards only:

  while (!Serial);

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {

    Serial.println("initialization failed. Things to check:");

    Serial.println("1. is a card inserted?");

    Serial.println("2. is your wiring correct?");

    Serial.println("3. did you change the chipSelect pin to match your shield or module?");

    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");

    while (true);

  }

  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();

  }
}

See Also

  • SD library - Your reference for the SD Card library.

  • Notes on using SD cards - What you need to know when you use SD cards

  • CardInfo - Get info about your SD card.

  • Datalogger - How to log data from three analog sensors to an SD card.

  • DumpFile - How to read a file from the SD card.

  • Files - How to create and destroy an SD card file.

  • ReadWrite - How to read and write data to and from an SD card.

Last revision 2015/08/18 by SM