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.
Interfacing SD Card Module with Arduino:
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:
Video on Interfacing SD with Arduino Uno and Arduino Mega 2560:
Downloads:
- Code: SD Module Arduino code
Related Links:
- Interfacing GPS Shield for Arduino(ublox NEO-6M-0-001) with Arduino Mega 2560
- Getting feeds from ThingSpeak using ESP8266 and Arduino
- Bluetooth HC-05, HC-06 Command/ Master Mode and AT Commands
- Master RESET ESP8266 Wifi Module using Arduino mega 2560
- Interfacing Bluetooth Modules HC-05, HC-06, JY-MCU BT_BOARD v1.02 and v1.03 with Arduino Uno and Arduino Mega 2560
- Bluetooth Error Codes:
- Interfacing SIM900D GSM Module Board with Arduino Mega 2560
- Interfacing Soil Moisture Sensor with Arduino
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()