Skip to content

SD Card

Overview

Example

A sample project for the Arduino IDE is provided in SD_Dir.ino which could be downloaded here

Activating the component

Function SWITCH IO port Conflicts with Remarks

Using the component

Setup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <SPI.h>
#include <mySD.h>

#define CS 13
#define CLK 14
#define MISO 2
#define MOSI 15

File root;

void setup()
{

    delay(3000); // delay for SD-Card issue -> upload switches OFF, aber booting switch to ON. Litle bit tricky :-)

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


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


    if (!SD.begin(CS, MOSI, MISO, CLK)) {
        Serial.println("initialization failed!");
        return;
    }
    Serial.println("initialization done.");

    root = SD.open("/");

    printDirectory(root, 0);

    Serial.println("done!");
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void printDirectory(File dir, int numTabs) {
    // Begin at the start of the directory
    dir.rewindDirectory();

    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');   // we'll have a nice indentation
        }
        // Print the 8.3 name
        Serial.print(entry.name());
        // Recurse for directories, otherwise print the file size
        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();
    }
}

Sample project

A sample project for the Arduino IDE is provided in SD_Dir.ino which could be downloaded here