Closed:AtMega2560+SD card reader, SD file not found, but it mounts

Closed: SD.h will not open .json - must be renamed to .jso

Hello again,

I have an ATmega2560 with the following wiring to a hiletgo SD Rom reader
gnd → gnd
5v → 5v
cs → 53
sck → 52
miso → 50
mosi → 51

I have the following ino file:

#include <SD.h>
void setup() {
  // 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.
  // comment out for ESP32:
  //pinMode(53, OUTPUT);     // change this to 53 on a mega

    // change to SD.begin() for ESP32
    // or SD.begin(53) for the AtMega2560
    if(!SD.begin()) {
      Serial.println("Unable to mount SD");
      while(true); // halt
  File f ="/data.json", "r");
  if(NULL==f) {
    Serial.println("Could not open file.");
  } else {
    Serial.println("Opened /data.json.");

void loop() {
  // put your main code here, to run repeatedly:

The SD card is freshly formatted and has /data.json (approx 200kB) as its only file

This code works on the ESP32, but although it mounts the SD successfully (begin returns true) it will not open a file on the Mega, only on the ESP32.

Despite the comments, the code mounts the SD rom with the default SD.begin() on the Mega and without the pinMode call. It still doesn’t read the file

If this wiring isn’t right for the AtMega2560 please correct me. It’s curious though that it mounts. Again, on my ESP32, this code runs no problem

Here’s my code for the Mega, compiled and tested. Note that SD only supports 8.3 file names.


 * SD card attached to SPI bus as follows:
 ** MOSI - pin 51
 ** MISO - pin 50
 ** CLK - pin 52
 ** CS - pin 4
#include <SD.h>
// SD.h It uses short 8.3 names for files.

File jsonFile;

void setup()
  pinMode(53, OUTPUT); // Needs setting
  pinMode(4, OUTPUT); // CS 
  if (!SD.begin(4))
    Serial.println("Initialization failed!");
    } // Infinite loop
  jsonFile ="index.txt");
  if (!jsonFile)
    Serial.println("Unable to open file");  
    } // Infinite loop  
  Serial.println("All good!");

void loop()

This line is needed:

  pinMode(53, OUTPUT); // Needs setting

when 53 is not used as SS to keep the Mega as SPI master.
Otherwise, SS going low could put the Mega into SPI slave mode.

Does the hiletgo SD Rom reader have it's own 5V to 3.3V regulator for power? SD cards need 3.3V power and signals.

How was the card formatted? I think the tool here is recommended:


When I try to open "index.txt" (which only contains "Hello World!") it works.

When I try to open my 200kB JSON file it does not.

I guess maybe there's a hidden limit on SD.h's file sizes. Too bad I don't know what it is.

CrossRoads, turns out my wiring was correct, and it appears to be a file size issue? my 200kB file will not open. My tiny text file will.

Not the file size, the file name. Rename it data.jso it should open.

To repeat what I said earlier, note that SD only supports 8.3 file names!

You could try sdfat.h instead of sd.h

My SdFat attempt was not working at all, but maybe I'll give it another go

Whoops. I totally spaced off the ".json"

Edit: Renaming the file worked. Sorry I didn't catch it the first time it was mentioned. I just spaced off that the extension was 4 chars.