For the Mega and Leonardo

Author Gravatar Image WILLIAM EARL
If you are using an Leonardo or Mega, you will have to replace the existing SD card library to add 'SD card on any pin' support. If you have an Uno/Duemilanove/Diecimila, this is not required.

First, find the "core libraries" folder - if you are using Windows or Linux, it will be in the folder that contains the Arduino executable, look for a libraries folder. Inside you will see an SD folder (inside that will be SD.cpp SD.h etc)
In the libraries folder, make a new folder called SDbackup. Then drag the SDfolder into SDbackup, this will 'hide' the old SD library without deleting it
Now we'll grab the new SD library, visit https://github.com/adafruit/SD and click theZIP download button, or click the button below

Uncompress and rename the uncompressed folder SD. Check that the SD folder contains SD.cpp and SD.h

Place the SD library folder your sketchbook libraries folder. You may need to create the libraries subfolder if its your first library. For more details on how to install libraries, check out our ultra-detailed tutorial at http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use

Using the SD Library with the Mega and Leonardo

Because the Mega and Leonardo do not have the same hardware SPI pinout, you need to specify which pins you will be using for SPI communication with the card. For the data logger shield, these will be pins 10, 11, 12 and 13. Find the location in your sketch where SD.begin() is called (like this):
Copy Code
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
and change it to add these pin numbers as follows:
Copy Code
  // see if the card is present and can be initialized:
  if (!SD.begin(10, 11, 12, 13)) {

cardinfo

The cardinfo sketch uses a lower level library to talk directly to the card, so it calls card.init() instead of SD.begin().
Copy Code
  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  while (!card.init(SPI_HALF_SPEED, chipSelect)) {
When calling card.init(), you must change the call to specify the SPI pins, as follows:
Copy Code
  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  while (!card.init(SPI_HALF_SPEED, 10, 11, 12, 13)) {
Last updated on 2014-04-21 at 10.40.30 AM Published on 2013-04-12 at 03.22.30 PM