sd module in Mega "Card failed, or not present" What I doing wrong?

Hi, I'm trying to implement a datalogger in a Mega and I can't comunicate with the Sd card.

This is the example code that I'm using

/*
SD card file dump

This example shows how to read a file from the SD card using the
SD library and send it over the serial port.

The circuit:

  • SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4

created 22 December 2010
by Limor Fried
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
// const int chipSelect = 4;
const int chipSelect = 10;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

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(10, OUTPUT);
pinMode(53, OUTPUT);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");

// 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");

// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}

void loop()
{
}

and the wirings:

MISO on pin 51
MOSI on pin 50
CLK on pin 52
CS on pin 53

What I'm doing wrong?

Thanks in advance

It looks like you are connecting the Slave Select line to pin 53 but then telling the SD library to use pin 10 for Slave Select. Tell it to use 53.

Almisl:
Hi, I'm trying to implement a datalogger in a Mega and I can't comunicate with the Sd card.

This is the example code that I'm using

/*

SD card file dump

This example shows how to read a file from the SD card using the
SD library and send it over the serial port.

The circuit:

  • SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4

created  22 December 2010
by Limor Fried
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
// const int chipSelect = 4;
const int chipSelect = 10;
void setup()
{
// Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

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(10, OUTPUT);
    pinMode(53, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
 
  // 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");

// if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

void loop()
{
}


]
and the wirings:

MISO on pin 51
MOSI on pin 50
CLK on pin 52
CS on pin 53

What I'm doing wrong?

Thanks in advance

Thank you,
I'm re-wired cs to pin 10 but same issue.

=(

Almisl:
Hi, I'm trying to implement a datalogger in a Mega and I can't comunicate with the Sd card.

The SPI pins on the MEGA are

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

An SD shield uses pins 10, 11, 12 and 13 for communications (UNO SPI pins).

If you plug an SD shield into a MEGA, none of the pins connect to the SPI port (which is why it doesn't work).

Just another one of those Arduino "Why On Earth Did They Do It That Way?" things.......

Hi,

I'm having the same problems. I have the attached a photo of the SD card adapter. I'm trying to wire it to an Arduino Mega ADK R3.
I'm using a 8gb micro SD card through an adapter, formatted to fat32.

I have tried connecting SCK to pin 52, Miso to 50, Mosi to 51 and CS to SS (pin 53). Also, I have tried it with a 1.8 SPI TFT-SD card combo.

I have modified the code, to wire chipselect to 53, also tried it with pin 10 (changed the wiring). I have seen it in an other topic that someone added a

digitalWrite(53, HIGH);

right after the

pinMode(53, OUTPUT);

line. I have tried my arduino with the CardInfo sample (just modified the sample code to fit the pin 53).

Can anyone help me Please?

Thank You!

Thank you, thank you. I'm re-wired the connections

50 MISO
51 MOSI
52 SCK
53 CS

in the sketch

...
const int chipSelect = 53;
...
pinMode(53, OUTPUT);
...

and just foward to the next step. Card is found, but can't open the file. This is the result: "Initializing SD card...card initialized.error opening datalog.txt". The Sd-card is an 4Gb card and formated as Fat16.

Thanks in advance

Almisl:
Thank you, thank you. I'm re-wired the connections

50 MISO
51 MOSI
52 SCK
53 CS

in the sketch

...
const int chipSelect = 53;
...
pinMode(53, OUTPUT);
...

and just foward to the next step. Card is found, but can't open the file. This is the result: "Initializing SD card...card initialized.error opening datalog.txt". The Sd-card is an 4Gb card and formated as Fat16.

Thanks in advance

I have already tried it, it isn't working. I have read taht for a 4GB card you have to use FAT32. Use FAT16 to a 1GB card, or smaller.

johnwasser:
It looks like you are connecting the Slave Select line to pin 53 but then telling the SD library to use pin 10 for Slave Select. Tell it to use 53.

I'm far from certain about what's going on here, but I think it is down to libraries and hardware.

I got most of my stuff going on a Uno and was then obliged to move to a Mega. What I have that appears to be different from the above is.

  1. A sketch that uses the library SPI.h

  2. An Ethernet+SD shield that uses the 6-pin ICSP cluster which is common to both Uno and Mega. The pins 11,12,13 thereon appear to be just pass-through, but pin 10 is connected.

All I did was plug it all in, uploaded the Uno sketch, and we were off and running without even thinking about it. Now, after a couple of months of reliable datalogging, I read this and wonder how it all happened. I have the line

pinMode(10, OUTPUT);

and pin 53 doesn't get a mention. Maybe the library handled it. Maybe this line should be removed. Looking back at my oldest identifiable sketch that uses the SD in the Uno, I now see that the library SPI.h is not listed. I also note that, in my recent attempt to account for each library, I haven't done so with this one, but my code will not compile without it.

I have made mistakes before. I have a display on the SPI bus on the Uno but it is now clear that it is simply on pins 11, 13 when I plug it into the Mega. And quite happy about it.

Here is my preamble and setup, the SD section of which may be of use.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>                  
#include <Ethernet.h>            // Ethernet 
#include <HttpClient.h>          // Cosm lib
#include <Cosm.h>                // Cosm lib
#include <PCD8544.h>             // Nokia 5110
#include <SD.h>                  // SD card
#include <string.h>              // from Date As Filename 
#include "RTClib.h"              // from Date As Filename
#include "Wire.h"                // Original RTC lib for LCD disp, SD card, and serial

#define DS1307_ADDRESS 0x68

RTC_DS1307 RTC;
static PCD8544 lcd;

File myFile;
char filename[] = "00000000.CSV";

// Custom symbols
static const byte DEGREES_CHAR = 1;
static const byte degrees_glyph[] = { 0x00, 0x07, 0x05, 0x07, 0x00 };
static const byte SLASH_CHAR = 2;
static const byte slash_glyph[] = {0x00,0x20,0x10,0x08};

byte InThermo[8] =  {
  0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F};
byte OutThermo[8] = {
  0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {
  0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95}; 

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char cosmKey[] = "l6... la dadedahdah...0Zz0g";

int  second, minute, hour, weekDay, monthDay, month, year;

int k=0;
float InTemp, OutTemp, DrainTemp;    // used for SD write only

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";
char calcId1[] = "diff";

const int bufferSize = 140;
char bufferValue[bufferSize]; // enough space to store the string we're going to send
CosmDatastream datastreams[] = {
  CosmDatastream(sensorId0, strlen(sensorId0), DATASTREAM_FLOAT),
  CosmDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
  CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  CosmDatastream(calcId1, strlen(calcId1), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
CosmFeed feed(83153, datastreams, 4);    //put your number of feeds here 

EthernetClient client;
CosmClient cosmclient(client);

void setup() {
   lcd.begin(84, 48);
     // Register the custom symbols...
  lcd.createChar(DEGREES_CHAR, degrees_glyph);
  lcd.createChar(SLASH_CHAR, slash_glyph);
  Wire.begin();
  Serial.begin(9600);
  Serial.print("    filename   ");
  delay(300);//Wait for newly restarted system to stabilize
  lcd.setCursor (0,0);
  lcd.print("Initializing");
  delay(2000);
  lcd.setCursor (0,1);

  pinMode(10, OUTPUT);

  if (!SD.begin(4)) 
  {
    lcd.print("failed!");
    delay (2000);
    return;
  }
  lcd.print("init. OK!");
  delay(2000);
      getFileName();
      Serial.println(filename);
  lcd.clear();

  Serial.println("LABEL,Time,InTemp,OutTemp,diff,DrainTemp");

  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);
  sensors.setResolution(DrainThermo, 12);

  Serial.println("Starting multiple datastream upload to Cosm...");
  Serial.println();

  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }
}

The description of the "CS" functionality can be very confusing.
The arduino can possibly operate the SPI interface in slave mode and the CS
pin described is actually the chip select for an incoming SPI connection, where as
you are normally trying to create an outgoing SPI connection with the arduino
as the master. You could try using a different pin for it.

The other thing to try, is a different SD card.

You are trying to open an existing file ? Does it already exist ? It may not work
if the file doesn't exist.

Try opening a new file, and writing something to it.

Almisl:
Hi, I'm trying to implement a datalogger in a Mega and I can't comunicate with the Sd card.

This is the example code that I'm using

/*
SD card file dump

This example shows how to read a file from the SD card using the
SD library and send it over the serial port.

This sketch reads the card. So:

  1. Is the card formatted?
  2. Is there a file on it to read?
  3. More to the point, if so, did you write the file on the card with the Mega?

Hi guys,

all works ok!!!!!!

THANKS TO ALL!

I'm doing exactly you tell me but nothing happen, my problem stay unsolved. In this point I'm remove all the conexions and start again with new wires and ... it is run well, then I'm looking for scratches on the wires and found one of them breaked.

My problem was solved.
Thank you I'm appreciate the help given.

This post can be closed

Before the post is closed, can you post your code & wiring here that worked?

THANK YOU! :slight_smile:

OK, got my SD socket to work :slight_smile:

Just for anyone else, having the same problems, always think of:

  1. start from a scratch, don't be afraid to clear all wiring
  2. a bigger prototype board (830 pins, or more), has 2*2 power lines (the two side power lines divide to two other lines!! - I was missing this point)