This is ONLY required if you have the older Datalogger shield which does not have the SPI port connection.
This is ONLY required if you are using a Leonardo or Mega with the older Datalogger shield!

If your shield looks like the above, and has the 2x3 pin header on the right, skip this page!

If your shield does not have the 2x3 pin header section and you are using a Mega or Leonardo (e.g. not UNO-compatible) then you can keep reading!

If you are using an Leonardo or Mega with the older datalogging shield, 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. If you have a rev B shield, this is also 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)

Outside 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.  Note that SDBackup must be outside of the libraries folder in order to effectively 'hide' the SD library.

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):
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
and change it to add these pin numbers as follows:
  // 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().
  // 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:
  // 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)) {

This guide was first published on Apr 12, 2013. It was last updated on Mar 08, 2024.

This page (Older Datalogger Shield Leonardo & Mega Library) was last updated on Mar 08, 2024.

Text editor powered by tinymce.