Go Down

Topic: How to connect Leonardo with SD card? (Read 6470 times) previous topic - next topic


I'm trying to use SD card by Leonardo, but failed.
I find that the pins defined in Sd2PinMap.h is different with most sample codes for UNO. I also connect the SD as follows, but fails,too.
How can I connect the both?

#elif defined(__AVR_ATmega32U4__)
// Teensy 2.0

// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 6;
uint8_t const SCL_PIN = 5;

// SPI port
uint8_t const SS_PIN = 0;
uint8_t const MOSI_PIN = 2;
uint8_t const MISO_PIN = 3;
uint8_t const SCK_PIN = 1;


Read about SPI and the Leonardo, http://arduino.cc/en/Main/arduinoBoardLeonardo, http://arduino.cc/en/Main/arduinoBoardLeonardo
The ICSP header has the SPI signals, they are not on the normal pins.

Your SD shield should have a 6-pin socket for the ICSP header.
Which SD shield are you using ?

The pin numbers in the Sd2PinMap.h are the pin number of the ATmega32U4 chip, those number are NOT the Arduino pin numbers.
See this pin mapping, http://arduino.cc/en/Hacking/PinMapping32u4


Feb 26, 2013, 02:35 pm Last Edit: Feb 27, 2013, 09:02 am by zintiger Reason: 1
Mr.Erdin, I use SD modules,not SD shield,to cennect the Leonardo,as the following photo:

http://arduino.cc/en/Reference/SPI shows:

The Leonardo SS does not specified any pin. In my understanding, any P0-P13 can be defined as SS to connect CS of SD card.but I tried Pin0,Pin4, Pin8 adn P10, and failed again.


I don't think either of those photos convey useful information.
The art of getting good answers lies in asking good questions.


Feb 27, 2013, 01:53 am Last Edit: Feb 27, 2013, 08:01 am by zintiger Reason: 1
Sorry,I find the pictures referred to the web site is wrong, I would change them when I get to my office latter.


I don't know where and how to settle my pictures in the net, so give my blog here to show both picture should appear in the upper post. Thank you.

And, I wonder can it be seen correctly in ur country. Pls let me know.


I can see the pictures. No problem.

You can use any pin as SS, but the original hardware 'SS' pin has to be made an output. Even if that pin is not used.
You can read it in the SPI page you already know, http://arduino.cc/en/Reference/SPI
I think pin 17 is the the original SS pin.

Could you add this to your code: pinMode( 17, OUTPUT);

Can you upload your sketch ?


Feb 27, 2013, 02:20 pm Last Edit: Feb 27, 2013, 02:26 pm by zintiger Reason: 1
Thank you.I tried the PIN 17, but the SD card still can note be initialized.
My sketch is, from the example of SD lib, CardInfo.ino:
Code: [Select]
  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.
Pin 4 used here for consistency with other Arduino examples

created  28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
// 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 = 4;   

void setup()
// Open serial communications and wait for port to open:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  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(17, OUTPUT);     //Here, zintiger changed this to 17 for Leonardo as you mentioned.

  // 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?");
  } 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:
    case SD_CARD_TYPE_SD2:

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

  // 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);
  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.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;

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

void loop(void) {


At last, I give up Leonardo. Thank you all for helping me.

Go Up