LCD and SD Card Reader on MEGA

I'm having trouble making my code work on my Mega for connecting to my SD card reader. I can't get my code to "initialization done.".

#include <LiquidCrystal.h> // include the LiquidCrystal library
#include <SPI.h> // 
#include <SD.h> // include the SD library for reading files from an SD card

const int numRows = 2; // number of rows on the display
const int numCols = 16; // number of columns on the display

File songFile; // Use for the song file.

// initialize the display with the correct number of rows and columns
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup()
{
  // set the baud rate for serial communication
  Serial.begin(9600);

  // initialize the display
  //lcd.begin(numRows, numCols);

  // clear the display
  //lcd.clear();

  // initialize the SD card and make sure it is connected
  Serial.begin(9600);
  while (!Serial)
  {
    // Wait for serial port to connect. Needed for native USB port only.
  }
  // Checking if the SD card is connected.
  Serial.print("Initializing SD card... ");
  // Added Delay
  delay(1000);
  if (!SD.begin(10))
  {
    // SD card was not connected.
    Serial.println("initialization failed!");
    while (1);
 }

  // SD card was found.
  Serial.println("initialization done.");
    // SD card was connected.
    File songFile = SD.open("song.txt");
    Serial.println("Found SD card." + songFile);
    // set the cursor position to the first row and first column
    lcd.begin(0, 0);
    // open the song text file in read mode

    if (!songFile)
    {
      Serial.println("Error opening song file!");
    }
    else
    {
      // read the song text from the file and write it on the display
      while (songFile.available())
      {
        lcd.print((char)songFile.read());
      }
    }
     // close the file
     songFile.close();
     Serial.println("The song file has been closed.");
}

void loop()
{
  // nothing happens after setup
}

I took my working code that reads my SD card on my Arduino Uno. I connected them the same way. And I know that code works because I can read and write to the SD card.

#include <SPI.h>
#include <SD.h>

File myFile;      // How the serial displays the test.txt file.
File upLoadFile;  // How I write what I want to the test2.txt

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.
  }
  // Checking if the SD card is connected.
  Serial.print("Initializing SD card... ");
  // Added Delay
  delay(1000);
  if (!SD.begin(10))
  {
    // SD card was not connected.
    Serial.println("initialization failed!");
    while (1);
  }
  // SD card was found.
  Serial.println("initialization done.");
  // Added Delay
  delay(1000);
  // open the file for reading:
  myFile = SD.open("bionicle.txt");
  if (myFile)
  {
    Serial.println("bionicle.txt:");
    // Added Delay
    delay(100);
    // Read from the file until there's nothing else in it:
    while (myFile.available())
    {
      // Writing out the text file, with a delay between each char.
      Serial.write(myFile.read());
      delay(75);
    }
    // Close the file:
    myFile.close();
    Serial.println(" ");
    // Added Delay
    delay(1000);
    Serial.println("Done reading from bionicle.txt.");
    Serial.println("The File is now closed.");
  }
  else
  {
    // If the file didn't open, print an error:
    Serial.println("error opening bionicle.txt");
  }
  // Added Delay
  delay(1000);
  // Open the file to write to it.
  Serial.println("Now opening test2.txt.");
  upLoadFile = SD.open("test2.txt", FILE_WRITE);
  if (upLoadFile)
  {
    Serial.println("The file test2.txt was open, now writing to the file.");
    // This is how we write to the file.
    // Added Delay
    delay(1000);
    upLoadFile.println("We will write something in this file cleverly some day.");
    // close the up load file:
    upLoadFile.close();
    Serial.println("Done writing to test2.txt.");
    Serial.println("The File is now closed.");
  }
  else
  {
    // If the file didn't open, print an error:
    Serial.println("error opening test2.txt");
  }
}

void loop()
{
  // nothing happens after setup
}

I also have a working code for making my LCD display with the same connections on the Mega.

#include <LiquidCrystal.h>

const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup()
{
  lcd.begin(16, 2);
  lcd.print("First line");
  lcd.setCursor(0,1);
  lcd.print("Second line");
}
void loop()
{
}

Do I need to connect my SD card on my Mega differently or declar the pins some how?

The pins for SPI MOSI, MISO and SCK are on different pins on the Mega from the Uno.

Arduino Mega SPI digital I/O pins:

MISO-pin 50
MOSI-pin 51
SCK- Pin 52
SS- pin 53

Arduino Uno SPI digital I/O pins:

MISO-pin11
MOSI-pin12
SCK-pin13
SS-pin10

1 Like

Think you may be using the wrong pin for a Mega...

Try...

SD.begin()

The pins being different are definitely the problem for me right now, I thought they were the same.

I tried a SD module on my Mega connected as shown in post #2 (MISO to pin 50, MOSI to pin 51, SCK to pin 52). Works fine with SS (CS) connected to pin 10 and SD.begin(10); or SS connected to pin 53 and SD.begin(53);

1 Like

They are not.

They aren't the same, but the select pin can still be 10, as commonly used on Uno, or any other, as it is not part of the SPI bus. The standard practice with Mega is to call

  pinMode(53, OUTPUT);

in Setup as preamble to the SD stuff, irrespective of whether you use it or not. Mega's SPI is on 50,51,52. If you have SD on a shield with the ICSP 6pin cluster, as is common, it is good for both Uno and Mega, but you still have to call pin 53!

I think there is a comprehensive explanation of this in the Reference.....

There used to be but it isn't there any more that I can find. I looked because I wanted to cite the reference when I replied early in the thread. There used to be a table with the pin named versus numbers for different processors and an explanation of which pins were default CS and which pins that had to be made OUTPUT for SPI to work.

I voiced my concern in a post to the Website and Forum section, Missing SPI pins for different boards,

When I went to look for that post, just now, I found that I was pointed to an archive with the missing information, Arduino - SPI. The table and the information on the CS pin is there.

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