Interfacing SD Card with Arduino using SD Card Module

SD Card module with Arduino

Interfacing SD Card with Arduino Uno and Arduino Mega 2560 using SD Card Module

To use an SD card with Arduino, the SD Library and Program are available in Arduino Software v1.05, you just need to connect the CS pin of SD module and Change SD pin in programming to which it is connected.

In the program, we have connected the CS pin of the Module with D53 (SS) and just set 53 on two places in the program. Check out Video for More details.

SD Card module with Arduino

 

Interfacing SD Card Module with Arduino:

interfacing arduino with sd card module

Bluetooth HC-05 & HC-06 have different pin numbers of LEDs only, power and Serial Communication pins are same.

SD Card Module Arduino Mega 2560 Arduino Uno
1 – Gnd GND GND
2 – MISO D50 (MISO) D12 (MISO)
3 – SCK D52 (SCK) D13 (SCK)
4 – MOSI D51 (MOSI) D11 (MOSI)
5 – CS D53 (SS) D10 (SS)
6 – +5 +5V +5V
7 – +3.3 N.C N.C
8 – GND N.C N.C

N.C = Not Connected

Code for Arduino Uno and Arduino Mega:

The Code below is used to connect Arduino with SD Card Module and List all the files and Directories within it.

#include <SPI.h>
#include <SD.h>File root;void setup()
{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(53, OUTPUT);

if (!SD.begin(53)) {
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
//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);
}
entry.close();
}
}

 

Output / Results:

sd card module arduino

 

Video on Interfacing SD with Arduino Uno and Arduino Mega 2560:

Downloads:

Related Links:

One Comment on “Interfacing SD Card with Arduino using SD Card Module”

  1. Thanks for this code, it’s saved me a lot of time searching.
    Just a small point, the second line has become concatenated #include File root;void setup()
    #include
    File root;
    void setup()

Leave a Reply

Your email address will not be published.