Skip to content

SD Card

Overview

At component SD card you can read and write data to a sd card inserted in the socket.

Example

There are two example projects for the Arduino IDE which can be downloaded: SD-Dir.ino (download here) and SD-ReadWrite.ino (download here)

ESP Board sd card
SW406,SW407

Activating the component

Function SWITCH IO port Conflicts with Remarks
CD/DAT3 IO13 SW406-1
DAT2 IO12 SW406-2
DAT1 IO4 SW406-3
DAT0 IO2 SW406-4
CLK IO14 SW406-5
CMD IO15 SW406-6
LED IO32 SW407-1
CDET IO27 SW407-2

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

There are two example projects for the Arduino IDE which can be downloaded: SD-Dir.ino (download here) and SD-ReadWrite.ino (download here)