Pages: [1]   Go Down
Author Topic: How to connect Leonardo with SD card?  (Read 2399 times)
0 Members and 1 Guest are viewing this topic.
Beijing
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Beijing
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 27, 2013, 03:02:54 am by zintiger » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 614
Posts: 49384
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think either of those photos convey useful information.
Logged

Beijing
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry,I find the pictures referred to the web site is wrong, I would change them when I get to my office latter.
« Last Edit: February 27, 2013, 02:01:55 am by zintiger » Logged

Beijing
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
http://blog.sina.com.cn/s/blog_6e9390570101ihkf.html

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

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?

Logged

Beijing
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
  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:
  Serial.begin(9600);
   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?");
    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) {
 
}
« Last Edit: February 27, 2013, 08:26:08 am by zintiger » Logged

Beijing
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: