Sd reader, uno, mega

I have an UNO, and built an SD card reader board, and have a program to read a file.
It all works.

Enter Mega, and begin mega-headache.

I used the exact same SD board I made, and slightly modified the program to accommodate the Mega.
I documented what I did in the program.

I move the wires from my homemade board straight over to the mega according to the pin in the program below, pins coming from documentation at Arduino.cc

I changed only one line of code, documented below, which is change 10 to 53 for the CS.

And.... no good.

The output says:
Initializing SD cart I'm at ONE.
Initialization failed! I'm at TWO

that's all.
(Yes I know there is a typo on cart should be card....)

I've spent the entire morning chasing this around, and don't even know what to try next.

Thanks in advance for any help.

#include <SPI.h>
#include <SD.h>
File myFile;

//this is for Arduino    MEGA MEGA MEGA

/*
changes from Uno version:
UNO                    MEGA
pin 13, clk, now goes to 52
pin 12, MISI, now goes to 51
pin 11, MOSI, now goes to 50
pin 10, CS, now goes to   53

also, below, if (!SD.begin(10)) {    now has (53). I think that can be () as default

and.... it doesn't work

*/


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Initializing SD cart I'm at ONE.");
  delay(100);

  if (!SD.begin(53)) {
    Serial.println("initialization failed! I'm at TWO");
    while (1)
      ;
  }

  Serial.println("initialization done.");
  // open the file for reading:
  myFile = SD.open("pinList.txt");
  if (myFile) {
    Serial.println("pinList.txt:");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test1.txt");
  }
}
void loop() {
  // nothing happens after setup
}

My pinout shows that MOSI is on 51 and MISO is on 50.

Just a hint for the next time that you design a board that uses SPI:

Use the ICSP header instead of the "normal" pins. The only signal that is not available on the ICSP header is the CS and you can use any "normal" pin for that.

Hallelujah. The heavens are raining happiness on my head.
Yes, works.

What pinout are you referring to?

I looked at:
https://www.arduino.cc/reference/en/libraries/sd/begin/
says:
This begins use of the SPI bus (digital pins 11, 12, and 13 on most Arduino boards; 50, 51, and 52 on the Mega)

I assumed the order was intact. Devilishly misleading....

Thanks a million. If you could point me at where you are seeing that, I am doubly thankful.
i.e. 11 -> 50
12 -> 51
13 -> 52

I have a folder on my PC exclusively for Arduino schematics and annotated board pinouts for handy reference. Here's a website that has the MEGA2560 pinout I use:

There are similar pinout diagrams for other Arduino boards too.

I really appreciate your light speed help! Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.