Hi! We are having trouble in initializing the SD Card using the arduino mega, but we tried in uno and it works just fine. How do we fix this? we tried simple codes like:
#include <SPI.h>
#include <SD.h>
#define SD_CS 53
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(SD_CS, OUTPUT);
if (!SD.begin(SD_CS)) {
Serial.println("SD Card failed or not detected.");
return;
}
Serial.println("SD Card initialized successfully.");
Serial.println("Listing files on SD card...");
File root = SD.open("/");
printDirectory(root, 0);
Serial.println("Done.");
}
void loop() {}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (!entry) {
break; // No more files
}
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 {
Serial.print("\tSize: ");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
We connected the pins like:
VVC - 5V
GND - GND
MISO - 50
MOSI - 51
SCK - 52
CS - 53



