Go Down

Topic: SD Card Shield configuration on Arduino mega 2560 REV3? (Read 20480 times) previous topic - next topic

NoahDavis

May 07, 2013, 10:37 pm Last Edit: May 07, 2013, 10:44 pm by NoahDavis Reason: 1
Hello,

I'm new to the world of arduino, sorta. I have two Arduinos, an Uno REV2, and the Mega REV3. I bought the Mega thinking I would be able to carry my SD Card Shield (Seeedstudio 3.0 I believe) to work on my new Mega. Once I got home, I thought, okay, I'll align the pins accordingly, did that, got a "initialization failed!" from the ReadWrite example for SD Cards (It works on my Uno just fine). I then read that I have to attatch pins 11,12,13 to Mega's 50,51,52, did that, and then attatch the SS pin, which on the Uno is pin 10, to the Mega's pin 53. Did that, and I still get an "initialization failed!". I connected the 4 pins (11-50,12-51,13-52 and 10-53) via standard jumpers. My setup:

SD Card Shield is attached to Uno.
Connected the pins as this page says (http://arduino.cc/de/Reference/SD)
Pins were: 11-50,12-51,13-52 and 10-53
Once the Arduino Uno was plugged in and powering, aswell as the Mega, and opened the ReadWrite and launched the program, making sure the "Mega 2560 or Mega ADK" board was selected, and the serial port was correct (For me, COM4).

CODE:
Code: [Select]
/*
  SD card read/write

This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

#include <SD.h>

File myFile;

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("Initializing 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(53, OUTPUT);
   
  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
// nothing happens after setup
}



I still received the "initialization has failed!" error in the Serial Monitor. Ideas?

Thank you.

NoahDavis

SOLUTION: After reading the Seeedstudio wiki on their SD Card Shield, it appears that either they've designed their shield differently, or Arduino's SD Reference is messed up.

PIN CORRECTION:
11>50
12>51
13>52
4 OR 10>53

Cyberking1994

Is there a difference between 11-50 and 11 > 50? If so, how does one wire 11 > 50 as opposed to 11-50?

NoahDavis

There's no difference between 11-50 and 11>50. I used 11-50 when stating you connect pin 11 to pin 50, and later for some reason switched to the > instead. My bad! D:

RudiAhlers

Is it possible to use the Micro SC breakout board, with headers on a Mega 2560 with pins 10 - 13, instead of 50 - 53?
i.e. how to I "re-map" the pins on the Mega 2560 to be the same as the UNO, in order to fit this breakout board when the header pins are already soldered onto it?

KirAsh4


SOLUTION: After reading the Seeedstudio wiki on their SD Card Shield, it appears that either they've designed their shield differently, or Arduino's SD Reference is messed up.

PIN CORRECTION:
11>50
12>51
13>52
4 OR 10>53


The SS pin on the Uno is pin 10, however the code in the sample sketch allows you to use another pin, such as pin 4.  It also states that regardless of what other pin you're using, pin 10 is still used and needs to be set as an OUTPUT.  If you adjusted the code to use pin 10 (SS) instead of 4, if should work.  Pin 4 is just an alternative pin because the Arduino Ethernet shield uses the SPI port, which are pins 10, 11, 12, and 13, so the built-in SD card reader needed to go elsewhere and they picked pin 4.  If you don't have anything on the SPI port, you can use pin 10 (SS) instead of 4.

fat16lib

The Arduino SPI pins are:







SPIUnoMega
SS1053
MOSI1151
MISO1250
SCK1352


So:
11>51
12>50
13>52

Any pin can be used for SD chip select.
Code: [Select]

  SD.begin(CS_PIN);


For more see http://arduino.cc/en/Reference/SPI.

liudr

#7
Jun 17, 2013, 02:16 am Last Edit: Jun 17, 2013, 02:19 am by liudr Reason: 1
This is yet another cheap SD card shield/module. There is no logic level shifter, just resistors. And the SPI pins are hardwired to Arduino pins 10-13. If you want to use this shield on MEGA without rewiring, you can enable the software SPI mode in the SD library.

Instruction here:

http://learn.adafruit.com/adafruit-data-logger-shield/for-the-mega-and-leonardo

Make sure this line in the SD.h file is not commented to enable software spi

#define MEGA_SOFT_SPI 1

adamatcooranbong

Hi Guys,

I'm having similar issues. I'm using a MEGA ADK and the Arduino WiFi shield to log data to the SD card - not actually using the WiFi function of the shield.

For some reason I can't detect the SD card. I have tried several SD cards and starting to think it is an issue with the MEGA ADK. I have two of these boards and dosnt work on either of them.

The same WiFi shield and SD cards work fine on my DUE board but need it to work on my MEGA. I use the following code.

Code: [Select]
// libraries used
#include <Debounce.h>
#include <SPI.h>
#include <SD.h>
#include <math.h> // math library not imported by default

/*
THIS IS A HACK setting the ADC prescale to 16 for faster analog read speed.
C function used:
sbi() "clear the bits"
cli() "set the bit"
C variables use in the source code use during complie time
srb "shift register name" sketch variable
bit "address of the register in bits" sketch variable
_SFR_BYTE() "shift register name" dummy variable to access the source code
_BV() "address of the register in bits" dummy variable to access the source code
*/
#ifndef cbi
#define cbi(sfr,bits) (_SFR_BYTE(sfr) &= ~_BV(bits))
#endif
#ifndef sbi
#define sbi(sfr,bits) (_SFR_BYTE(sfr) |= _BV(bits))
#endif

// constant variables before compile
#define stopSwitch 48 // stop switch connected to pin 48

// create instances of the classes included in the libraries
Debounce debouncer1=Debounce(20,stopSwitch);

// declare global constants and their data types
const float deltaVolts=5.0/pow(2,10); // 5.0 volts input divided by the 2^10 (MEGA) or 2^12 (DUE) ADC i.e. 12 bits of resolution

// declare global variables and their data types
int sensorValue;
int id=1;
float volts;

// declare files
File dataFile;

// setup code here, to run once
void setup()
{
  //analogReference(DEFAULT);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  Serial.begin(115200); // initialize serial communication
  while(!Serial)
  {
  }
  Serial.print("Initializing SD card..."); // print to the serial monitor a message
  pinMode(53,OUTPUT); // pin 53 must be left as an OUTPUT for the SD library to work
  pinMode(10,OUTPUT); // pin 10 must be left as an OUTPUT so it can be set HIGH
  digitalWrite(10,HIGH); // pin 10 needs to be put HIGH to explicitly deselect the WiFi HDG104 chip
  pinMode(stopSwitch,INPUT);
  digitalWrite(stopSwitch,LOW);
  if(!SD.begin(4)) // Chip Select pin 4 default channel on WiFi sheild for SD communication. If the statement !SD.begin(SS) comes back as FALSE execute the "if" statement
  {
    Serial.println("Card failed or not present"); // print to screen the SD card status
    return; // start the loop again and check if the SD is faulty or not installed
  }
  else
  {
    Serial.println("Card initialized"); // else everything is all good
  }
  if(SD.exists("volts.csv"))
  {
    Serial.println("File exists and now being removed");
    SD.remove("volts.csv");
  }
  else
  {
    Serial.println("No file could be found");
  }
  Serial.print("Sample No.");
  Serial.print(",");
  Serial.println("Volts");
  dataFile=SD.open("volts.csv",FILE_WRITE);
  dataFile.print("Sample No.");
  dataFile.print(",");
  dataFile.println("Volts");
  dataFile.close();
  //analogReadResolution(12); // default resolution is 10 bit statment changes to 12 bit for the DUE board but line not use when using the MEGA or low boards
}

// main code
void loop()
{
  debouncer1.update(); // update the debouncer
  sensorValue=analogRead(A0);
  volts=deltaVolts*sensorValue;
  dataFile=SD.open("volts.csv",FILE_WRITE);
  if(dataFile && debouncer1.read()==LOW)
  {
    dataFile.print(id,DEC);
    dataFile.print(",");
    dataFile.println(volts,DEC);
    dataFile.close();
    Serial.print(id,DEC);
    Serial.print(",");
    Serial.println(volts,DEC);
    float jam=log10(volts);
  }
  else
  {
    dataFile.close();
    return;
  }
  id++;
}


But keeps stopping at
Quote

if(!SD.begin(4))

on my ADK.

Any help would be much appreciated.

liudr

What is stopWatch pin? Its pin number?

Which SD library are you using, the adafruit SD library or the arduino IDE SD library? Do you have an SD library from adafruit on your computer? There is a version of adafruit SD library that can use software SPI on pins 11-13 to save their areses for their SD logger shield but you should NOT use software SPI.

adamatcooranbong

Hi Liudr,

Sorry for not getting back to your reply sooner. Only starting to learn the functionality of this forum i.e. didn't know how too keep track of replies etc.

I have solved the issue. I needed to deselect the WiFi chip by setting it hig as per the following code.
Code: [Select]
pinMode(10,OUTPUT); // pin 10 must be left as an OUTPUT so it can be set HIGH
  digitalWrite(10,HIGH); // pin 10 needs to be put HIGH to explicitly deselect the WiFi HDG104 chip[code]

To answer your question I'm just using the Arduino SD library.

Thanks for the reply.

Cheers

Adam
[/code]

ybjcool

I have the same problem, pins are right for mega 2560

50 51 52 and  ss is 53, but it still doesn't work on the mega2560

thanks for the #7 liudr replay .   the sd library which has recommended is worked very well

thanks a lot again !   

Maybe the original sd library has some problems to support mega, i guess.   but I have tried uno and leonardo. it is fine.

Kostli


This is yet another cheap SD card shield/module. There is no logic level shifter, just resistors. And the SPI pins are hardwired to Arduino pins 10-13. If you want to use this shield on MEGA without rewiring, you can enable the software SPI mode in the SD library.

Instruction here:

http://learn.adafruit.com/adafruit-data-logger-shield/for-the-mega-and-leonardo

Make sure this line in the SD.h file is not commented to enable software spi

#define MEGA_SOFT_SPI 1



I don't find the code that site refer to.
Could anyone tell me the file and the lines to find this piece of code?
Also the library that exixt in the link is not compaling and for this reason i have this message:
[C:\Users\?????\Documents\Arduino\libraries\SD\utility\Sd2Card.cpp: In member function 'uint8_t Sd2Card::init(uint8_t, uint8_t, int8_t, int8_t, int8_t)':
C:\Users\?????\Documents\Arduino\libraries\SD\utility\Sd2Card.cpp:290: error: 'digitalPinToPort' was not declared in this scope
C:\Users\?????\Documents\Arduino\libraries\SD\utility\Sd2Card.cpp:290: error: 'portOutputRegister' was not declared in this scope
C:\Users\?????\Documents\Arduino\libraries\SD\utility\Sd2Card.cpp:291: error: 'digitalPinToBitMask' was not declared in this scope
C:\Users\?????\Documents\Arduino\libraries\SD\utility\Sd2Card.cpp:294: error: 'portInputRegister' was not declared in this scope]

liudr

Big blue button that says down this library. Once you install it in the correct folder, restart arduino IDE and try again.

Kostli


Big blue button that says down this library. Once you install it in the correct folder, restart arduino IDE and try again.

And then do I have anything else to do to work on arduino MEGA? For examble, do ihave  to change any piece of code?

Go Up