SD Shield on a Mega

Hello guys, I would like to ask if there are any SD Shields that are compatible with the Arduino Mega. If there are, how would you know if it is Mega-compatible? I tested this shield with my Mega but the SD Card always failed to initialize. This is the Shield that I used: “http://www.e-gizmo.com/KIT/SD%20%20MMC%20Shield.html”. I’m not sure if it’s my wiring at fault. I have attached a photo of my wiring.
This is the code I used to test the shield and card:

#include <SD.h>

const int chipSelect = 53;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(53)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  
  // make a string for assembling the data to log:
  String dataString = "Hello World";
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println("Hello World");
    dataFile.close();
    // print to the serial port too:
//    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
//  else {
//    Serial.println("error opening datalog.txt");
//  } 

}

void loop()
{
}

If you are going to show just pictures of shields, you should show pictures of the bottom of the shields. If the shield has a 6 pin ICSP connector, it will work with the Mega. If not, it probably won't.

The picture shows connections to pins 12 and 13, but nothing to pin 11. All three pins need to be connected to the appropriate pins on the Mega. You need to get wire of other colors, too. All that green wire is too hard to follow.

Sorry about the wires being the same color. My other wires are not able to reach that is why I had to use all green. This is the pin connection that I followed: 11-50, 12-51, 13-52, 4 or 10-53. I have attached a pic of the underside of the shield and after looking at pics of the 6 pin ICSP, it looks like it does not have one. I have an UNO as well and the shield works fine on it.

On the 328-based machines, 10 is SS, 11 is MOSI, 12 is MISO, and 13 is SCK. On the Mega, 53 is SS, 51 is MOSI, 50 is MISO, and 52 is SCK.

So, it looks like your connections are wrong.

It worked. Thank you for your help PaulS. I was following the wiring from another thread though. Sorry for the trouble.

I don't know if this is the correct topic to ask this question, but how do you make the file that it creates a .csv instead of .txt?

but how do you make the file that it creates a .csv instead of .txt?

Simply put csv at the end of the name, instead of txt.

Yeah I figured that out. Sorry for asking a simple question. But I have a new problem. When it opens up in Excel, there are gaps in the data. It starts at A40 then it ends at A43 then starts again at A85 ends at A88. I can’t seem to see the problem in my code. though.

Here is my code:

#include <LiquidCrystal.h>                                                     //include LCD library
#include <SD.h>

long phReadInterval = 500; 
long previousMillisPH = 0; 
int PHState = HIGH; 
float PH_Val; 
const int chipSelect = 53;                                                     //SD Card

LiquidCrystal lcd(8,9,4,5,6,7);
String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                           
  //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(9600);                                                       //for SD Card
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port_0 to 38400
     Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     lcd.begin (16,2);
     Serial.print("Initializing SD Card...");
     pinMode(53, OUTPUT);
     if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");                              //Don't do anything more
    return;
  }
  Serial.println("card initialized.");  
 }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  


  void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }



 void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
  getPHvalue(); 

    lcd.setCursor(0,1); 
    lcd.print("PHvalue: "); 
    //lcd.print(PH_Val);// does not work
    char charBuf[10];
    sensorstring.toCharArray(charBuf, 10);
    lcd.print(charBuf);
 
    
    File dataFile = SD.open("datalog.csv", FILE_WRITE);
 
  if (dataFile) {
    dataFile.println(charBuf);
    dataFile.close();
  }
 
    else {
    Serial.println("error opening datalog.csv");
 
 }
 }
void getPHvalue() {                                                          
     
  unsigned long currentMillisPH = millis(); 
 
  if(currentMillisPH - previousMillisPH > phReadInterval) { 
    previousMillisPH = currentMillisPH;   
    if (PHState == HIGH)  { 
      PHState = LOW; 
    } 
    else  { 
      PHState = HIGH; 
      Serial.print("r\r");  // 'R' reads one value, see stamp doc 
      while (Serial.available() > 0) { 
        // Serial1.parseFloat looks for the next valid float number 
        // in the serial stream. In this case on hardware serial 1 
        PH_Val = Serial.parseFloat(); 
        // look for the carriage return. That's the end of your sentence: 
        if (Serial.read() == '\r') { 
          Serial.println(PH_Val);  // can be used for debug, pc display etc. 
        return;
       } 
      } 
    } 
  } 
}

can't seem to see the problem in my code. though.

I can't, either. That crappy formatting has a lot to do with why I can't. Perhaps, you have the same problem.

I'd suggest that you put every { on a new line, and use Tools + Auto Format.

      Serial.print("r\r");  // 'R' reads one value, see stamp doc 
      while (Serial.available() > 0) {

But, not immediately.

What do you mean by not immediately?

What do you mean by not immediately?

You ask for information. Sooner or later, that request gets to the pH probe. After a while, you get a response. But, that whole process does not happen in the few nanoseconds between sending the request and the check for a response.

Ah. I understand know. I did not think about that. I thought it had something to do with my code. But is there a way to make the display of the values continuous?

But is there a way to make the display of the values continuous?

Don't make a new request until after you've gotten a response to the last request.

But I thought that you already answered it?

But I thought that you already answered it?

I meant "Don't ask the pH sensor for more data until you have received a response from the last request", not for you to not ask questions here. You are free to ask us all the questions you want, with or without waiting for answers. You are not free to ask the pH sensor another question until it has answered (completely) the last one.

Oh. Thanks for the clarification. I meant is it possible to arrange the data in one row only. Like the first value would be in A1, the second in B2, etc.

I meant is it possible to arrange the data in one row only. Like the first value would be in A1, the second in B2, etc.

A1 and B2 aren't the same row or column, and are not names that mean anything in a text file (which a csv file is).

Oh sorry about that. I meant B1.

The code you have posted opens the file, prints a value, followed by a carriage return and line feed. If you want the results in a row, instead of a column, you should be printing the value and a comma, not a CR/LF.

Alright then. I'll try experimenting with the code. I'll post the results. Thanks!

I’m getting a new problem with my project. When I put an SD card in the socket, the LCD display goes haywire. But when there is no SD card in the socket the LCD displays the value just fine. I’m pretty sure it’s got nothing to do with the code but with my wiring. For the LCD wiring I followed the instructions here: (http://chetanpatil.info/1/post/2011/10/breadboard-lcd-arduino-mega-2560.html). But I made some minor changes. I used LCD Pin 14- Pin 7 on Mega, LCD Pin 13- Pin 6, LCD Pin 12- Pin 5, LCD Pin 11- Pin 3. Regardless of those changes the LCD works fine except when the SD Card is inserted in the socket.