Cannot initialize SD Datalogger on Mega2560

by chrisi on Mon Sep 05, 2011 4:19 pm

Hi,
I have used the data logging shield successfully on the arduino uno.
Now I switched to the Mega 2560 and have the following situation:

  • I use the IDE version 0022
  • I put the shield in the same pins as with the arduino uno, i.e. it is connected to the digital pins 0 to 13, GND and AREF on one side and from A0 to A5 and the various supply connectors on the other side.
  • I modified my sketch by setting explicitely pin 53 as OUTPUT
  • I modified the datalogger and the cardinfo examples to match the mega
    Here is the modified code from the CardInfo:
    Code: Select all | TOGGLE FULL SIZE
    /*
      SD card test
       
     This example shows how use the utility libraries on which the'
     SD library is based in order to get info about your SD card.
     Very useful for testing a card when you're not sure whether its working or not.
        
     The circuit:
      * SD card attached to SPI bus as follows:
     ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
     ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
     ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
     ** CS - depends on your SD card shield or module

     
     created  28 Mar 2011
     by Limor Fried
     */
     // include the SD library:
    #include <SD.h>

    // set up variables using the SD utility library functions:
    Sd2Card card;
    SdVolume volume;
    SdFile root;

    // change this to match your SD shield or module;
    // Arduino Ethernet shield: pin 4
    // Adafruit SD shields and modules: pin 10
    // Sparkfun SD shield: pin 8
    const int chipSelect = 10;

    void setup()
    {
      Serial.begin(9600);
      Serial.print("\nInitializing SD card...");
      // 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.
      pinMode(53, OUTPUT);     // change this to 53 on a mega


      // we'll use the initialization code from the utility libraries
      // since we're just testing if the card is working!
      if (!card.init(SPI_HALF_SPEED, chipSelect)) {
        Serial.println("initialization failed. Things to check:");
        Serial.println("* is a card is inserted?");
        Serial.println("* Is your wiring correct?");
        Serial.println("* did you change the chipSelect pin to match your shield or module?");
        return;
      } else {
       Serial.println("Wiring is correct and a card is present.");
      }

      // print the type of card
      Serial.print("\nCard type: ");
      switch(card.type()) {
        case SD_CARD_TYPE_SD1:
          Serial.println("SD1");
          break;
        case SD_CARD_TYPE_SD2:
          Serial.println("SD2");
          break;
        case SD_CARD_TYPE_SDHC:
          Serial.println("SDHC");
          break;
        default:
          Serial.println("Unknown");
      }

      // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
      if (!volume.init(card)) {
        Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
        return;
      }


      // print the type and size of the first FAT-type volume
      uint32_t volumesize;
      Serial.print("\nVolume type is FAT");
      Serial.println(volume.fatType(), DEC);
      Serial.println();
     
      volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
      volumesize *= volume.clusterCount();       // we'll have a lot of clusters
      volumesize *= 512;                            // SD card blocks are always 512 bytes
      Serial.print("Volume size (bytes): ");
      Serial.println(volumesize);
      Serial.print("Volume size (Kbytes): ");
      volumesize /= 1024;
      Serial.println(volumesize);
      Serial.print("Volume size (Mbytes): ");
      volumesize /= 1024;
      Serial.println(volumesize);

     
      Serial.println("\nFiles found on the card (name, date and size in bytes): ");
      root.openRoot(volume);
     
      // list all files in the card with date and size
      root.ls(LS_R | LS_DATE | LS_SIZE);
    }


    void loop(void) {
     
    }
  • I updated the SD library with the current adafruit version adafruit-SD-562defe and - yes, you guessed it - no success.
  • I found the hint, that it is recommended to set the CS pin to high, I tried this with pin 53 and pin 10
  • Finally I tried it on an older Mega board without success.
  • If I change it back to the Uno it works great, so I am sure that I did not damage something when mounting the shield, and I know that the card is working.

Any ideas what I can do?

Any hint where to look further is highly appreciated.

chris
chrisi
 
Posts: 2
Joined: Mon Sep 05, 2011 3:47 pm

Re: Cannot initialize SD Datalogger on Mega2560

by adafruit on Wed Sep 07, 2011 5:48 pm

FOR MEGA ARDUINOS edit the SD/utility/Sd2Card.h file after installing and uncomment the line that says #define MEGA_SOFT_SPI 1 to allow the Mega to use the same pinout for SD cards as the Classic Arduinos

the RTC i2c lines are not the same, search the forum for how to adapt the shield for the mega
User avatar
adafruit
 
Posts: 11666
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: Cannot initialize SD Datalogger on Mega2560

by bbq4me on Wed Sep 07, 2011 10:45 pm

This is good info. I am going to try the header file fix to eliminate some wiring I have been using for those (4) pins.

For the SDA, look forMega Digital pin 20 and for SCL, Digital pin 21. I soldered two wires on the logger shield to bring these signals from the appropriate I2C pads to the corresponding Mega digital pins.

That works on my logger on a Mega2560...
bbq4me
 
Posts: 14
Joined: Fri Aug 26, 2011 8:47 am

Re: Cannot initialize SD Datalogger on Mega2560

by bbq4me on Thu Sep 08, 2011 9:07 pm

I found another tip related to how to get the SD card working. I made the changes in the previous post and was getting x01 return code trying to initialize the SD card using the CardInfo sketch. So I took a break for a few hours and started looking around for readme files and found a nice one in the SDfatlib package shown below:
MegaSoftSPI.txt
Code: Select all | TOGGLE FULL SIZE
Features have been added to support an unmodified Adafruit GPS Shield
or Datalogging Shield on an Arduino Mega.

Define MEGA_SOFT_SPI to be non-zero in SdFatConfig.h to use software SPI
on Mega Arduinos.  Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.

Defining MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be
used on Mega Arduinos.  Software SPI works well with GPS Shield V1.1
but many SD cards will fail with GPS Shield V1.0.

Software SPI should work with all Datalogging Shields.

The examples SdFatGPS_CSVSensorLogger.pde and SdFatGPSLogger_v3.pde
should work on on the Mega when MEGA_SOFT_SPI is defined.


After making this update, my SD card started working using the standard pinouts.
Good luck !
bbq4me
 
Posts: 14
Joined: Fri Aug 26, 2011 8:47 am

Re: Cannot initialize SD Datalogger on Mega2560

by chrisi on Fri Sep 09, 2011 4:25 am

Works great, Thank You!

It would be great, if it is possible to set the variable from the sketch and not in the library. Thus it would not depend on an edited lib header which would increase portability.
chrisi
 
Posts: 2
Joined: Mon Sep 05, 2011 3:47 pm

Re: Cannot initialize SD Datalogger on Mega2560

by fat16lib on Fri Sep 09, 2011 5:41 pm

I couldn't make soft SPI pin numbers setable in a sketch since they must be constants when the library is compiled. This allows the compiler to use fast digital I/O instructions needed for soft SPI.

In the future more shields will use the ISP pins so soft SPI will not be required on 2560. I only added soft SPI as a stop gap measure.
fat16lib
 
Posts: 593
Joined: Wed Dec 24, 2008 12:54 pm

Re: Cannot initialize SD Datalogger on Mega2560

by supercolli on Thu Oct 20, 2011 6:11 am

Hi, just purchased the Arduino Mega 2560 and I've the same problem. My specification are the same of those declared by "chrisi" in his first post. The problem is that I've followed the "adafruit" procedure finded in the second post, try to run the 'CardInfo2' example but my Adafruit Shield Sd Card still give me the error message of inizialization failed...
supercolli
 
Posts: 3
Joined: Tue May 10, 2011 5:01 am

Re: Cannot initialize SD Datalogger on Mega2560

by adafruit_support_bill on Thu Oct 20, 2011 6:21 am

Have you previously run this data logger on a regular Arduino?
User avatar
adafruit_support_bill
 
Posts: 28903
Joined: Sat Feb 07, 2009 9:11 am

Re: Cannot initialize SD Datalogger on Mega

by Vitor on Wed Dec 07, 2011 1:41 am

I have the UNO and the MEGA boards.
The Datalogger works great in the UNO

When I install in the MEGA I cannot initialize the SD portion of the datalogger, the RTC portion is working well. I need the MEGA to read 13 analog inputs.
I have looked at all the solutions in this forum and others and it appears I'm missing something.
I have downloaded the SD library and looked for the commented line to no avail. I have only removed the "0" at the end.
In the sketch I have changed the chip select to 10 and I have changed this " pinMode(10, OUTPUT);" to 10 and 53

My Sd2Card.h file does not have a commented //#define MEGA_SOFT_SPI line
This is what I have in Sd2Card.h file

/**
* Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
*
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
* but many SD cards will fail with GPS Shield V1.0.
*/
#define MEGA_SOFT_SPI 0
//------------------------------------------------------------------------------
#if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
#define SOFTWARE_SPI
#endif // MEGA_SOFT_SPI


I'm at a loss here, please help.
Vitor
 
Posts: 12
Joined: Sun Jun 26, 2011 1:43 pm

Re: Cannot initialize SD Datalogger on Mega2560

by adafruit_support_bill on Wed Dec 07, 2011 6:38 am

Change
Code: Select all | TOGGLE FULL SIZE
#define MEGA_SOFT_SPI 0

to
Code: Select all | TOGGLE FULL SIZE
#define MEGA_SOFT_SPI 1
User avatar
adafruit_support_bill
 
Posts: 28903
Joined: Sat Feb 07, 2009 9:11 am

Re: Cannot initialize SD Datalogger on Mega2560

by Vitor on Wed Dec 07, 2011 10:07 am

Thank you.
I have tried replacing the 0 with 1, no change.
Vitor
 
Posts: 12
Joined: Sun Jun 26, 2011 1:43 pm

Re: Cannot initialize SD Datalogger on Mega2560

by adafruit_support_bill on Wed Dec 07, 2011 10:36 am

What sketch are you running? What output do you see on the serial monitor?
User avatar
adafruit_support_bill
 
Posts: 28903
Joined: Sat Feb 07, 2009 9:11 am

Re: Cannot initialize SD Datalogger on Mega2560

by Vitor on Wed Dec 07, 2011 9:38 pm

I'm using sketch 023

After modifying the Sd2Card.h file, does anything special needs to be done? Compiling?

This is what I get in the serial monitor in the Arduino Mega

Initializing SD card...initialization failed. Things to check:
* is a card is inserted?
* Is your wiring correct?
* did you change the chipSelect pin to match your shield or module?


This is what I get in the Arduino Uno with the same sketch (with out the changes for the Mega).

Initializing SD card...Wiring is correct and a card is present.
Card type: SD1
Volume type is FAT16
Volume size (bytes): 1017249792
Volume size (Kbytes): 993408
Volume size (Mbytes): 970
Files found on the card (name, date and size in bytes):
_SYSTEM_/ 2011-08-12 10:13:50
THUMBS.DB 2007-06-20 15:39:50 5120

Thank you for your help.
Vitor
 
Posts: 12
Joined: Sun Jun 26, 2011 1:43 pm

Re: Cannot initialize SD Datalogger on Mega2560

by adafruit_support_bill on Wed Dec 07, 2011 10:06 pm

I'm using sketch 023

I'm not familiar with that one. Can you provide a link?
User avatar
adafruit_support_bill
 
Posts: 28903
Joined: Sat Feb 07, 2009 9:11 am

Re: Cannot initialize SD Datalogger on Mega2560

by Vitor on Thu Dec 08, 2011 10:42 am

Sorry, it was late last night.

I'm using Ladyada's CardInfo sketch, no modifications done, other than the changes for the Mega board
const int chipSelect = 10
#define MEGA_SOFT_SPI 0
pinMode(10, OUTPUT) (I have tried 53 here also).

Same sketch minus the above noted modifications for the Uno board.
Vitor
 
Posts: 12
Joined: Sun Jun 26, 2011 1:43 pm