Go Down

Topic: SDcard shield Uno -> Mega 2560 (Read 1 time) previous topic - next topic


I bought this shield http://www.sparkfun.com/products/9899 to use with a mega 2560 and I am having trouble figuring out where to map the ports to (that is if I even have to change them).

I am using some modified code from the sparkfun site.
Code: [Select]

    Basic example reading and writing to the microSD shield
    Based on the SdFat Library by Bill Greiman
    SparkFun Electronics
    This example creates a file (or opens it if the file already exists) named 'Test.txt.'
    A string with the value 'Millis: (number of millis)' is appended to the end of the file.
    After writing to the file, the contents of the entire file are printed to the serial port.

//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>
#include <ctype.h>

//Create the variables to be used by SdFat Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

char name[] = "Test.txt";     //Create an array that contains the name of our file.
char contents[256];           //This will be a data buffer for writing contents to the file.
char in_char=0;
int index=0;                  //Index will keep track of our position within the contents buffer.

void setup(void)

    Serial.begin(9600);        //Start a serial connection.
    pinMode(10, OUTPUT);       //Pin 10 must be set as an output for the SD communication to work.
    card.init();               //Initialize the SD card and configure the I/O pins.
    volume.init(card);         //Initialize a volume on the SD card.
    root.openRoot(volume);     //Open the root directory in the volume.
    Serial.println("SDcard Test Program V0.0.1");
void loop(void){   
    file.open(root, name, O_WRITE);    //Open or create the file 'name' in 'root' for writing to the end of the file.
    sprintf(contents, "Test");    //Copy the letters 'Millis: ' followed by the integer value of the millis() function into the 'contents' array.
    file.print(contents);    //Write the 'contents' array to the end of the file.
    Serial.println("SDcard Written");
    file.close();            //Close the file.
    file.open(root, name, O_READ);    //Open the file in read mode.
    in_char=file.read();              //Get the first byte in the file.
    //Keep reading characters from the file until we get an error or reach the end of the file. (This will output the entire contents of the file).
    while(in_char >=0){            //If the value of the character is less than 0 we've reached the end of the file.
        Serial.print(in_char);    //Print the current character
        in_char=file.read();      //Get the next character
    file.close();    //Close the file
    delay(1000);     //Wait 1 second before repeating the process.

I am currently using the sdfatlib20111205 library (this is the only version that didn't give me a ton of errors).
Right now it isn't working at all. Any guidance as to how to wire this up would be much appreciated.


That shield uses SPI. On the UNO, that is on pins 11, 12, and 13.
SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI communication using the SPI library.

On the Mega, that is on pins 51 to 53.
SPI: 50 (MISO), 51 (MOSI), 52 (SCK), 53 (SS). These pins support SPI communication using the SPI library.

Note that the CS pin is not necessarily the one you want to use, but that the pin MUST be set to OUTPUT, even if it is not the pin you use.

You'll need to jumper from the hardwired pins the shield used to the correct pins on the Mega.
The art of getting good answers lies in asking good questions.


Thank you! That is all I needed. Works perfect now.


For MEGA 2560 and Sparkfun microSD shield
Pin 8 on shield should connect to Pin 53 on MEGA 2560.

Go Up