Go Down

Topic: Unable to access 2 SD cards with Arduino Mega 2560 Rev3 (Read 332 times) previous topic - next topic

slokesh

My objective is to copy a file from one SD card to another SD card. To achieve that, I have connected two 16 GB MicroSD cards to Arduino Mega 2560 using 2 Tf Card Memory Shields(Please refer the image below)



As my intention is to communicate with 2 SD cards, I used SPI protocol with CS pins as 04 and 10(and even tried many other digital pins).

My pin connections between SD Module and Arduino Mega are like below
GND - GND
vcc - vcc
MISO - 50
MOSI - 51
SCK - 52

CS1 - 10
CS2 - 04


And my code is like below (My code here doesn't do much except initialization, but even this fails)
Code: [Select]

#include <SPI.h>
#include <SdFat.h>
#include <string.h>

SdFat SD;

void setup() {
  Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println("Initializing SDFat SPI card...");
  //SPI.begin();
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);

  digitalWrite(4, HIGH);
    digitalWrite(10, LOW);
 
  if (!SD.begin(10)) {
    Serial.println("initialization 10 failed!");
  } else {
    Serial.println("initialization 10 done.");
  }

 digitalWrite(10, HIGH);
    digitalWrite(4, LOW);
    
  if (!SD.begin(4)) {
    Serial.println("initialization 4 failed!");
  } else {
    Serial.println("initialization 4 done.");
  }
  
  if (1 == 1) {
    return;
  }
  
}

void loop()
{

}



The versions of SdFat library(by Bill Greiman) that I tried with are 1.1.4, 1.1.3 and 1.0.1.

Other steps that I tried
1. I tested these 2 sheilds separately- by connecting only one at a time, and they both work fine.Moreover, in this mode, I could also access files in them - so there is no issue in either sd cards, or card modules
2. Tried also SD library instead of SdFat
3. Downgraded and upgraded SdFat library to different versions
4. Tried the same code on Arduino Uno, with Uno specific SPI pin configurations. Even that approach did not result in any positive outcome.
5. Tried with both 3.3 V and 5V voltage input to SD card modules
6. Formatted both the cards to FAT16, and even to FAT 32 (formatted the cards with Windows formatting option, also used SdFormatter example of SdFat library)
7. Tried using other digital ports 7, 8, 9, 53 etc..
8. Tried by commenting  digitalWrite(4, HIGH); digitalWrite(10, LOW); digitalWrite(4, LOW); digitalWrite(10, HIGH); from the code
9. Uncommented SPI.begin() and tried
10. Also tried TwoCards example of SdFat library


I am still not sure what I am missing. Kindly help me overcome this issue.

Thank you in advance for your time and consideration.


markd833

If you have tested each SD card adaptor individually and they work fine, then I would guess that the problem is one that seems to crop up quite frequently whenever an SD card is involved in SPI communications.

The SD card adaptor is not releasing the MISO signal when it is de-selected. This means that your other SD card adaptor cannot then use the same MISO signal.

You could try accessing one SD card adaptor using the hardware SPI interface (like you already have been doing) and the other SD card adaptor using a software SPI interface.

ShermanP

I believe markd833 has it exactly right.  Below is a schematic that probably matches your SD modules.  It's typical.  Notice that the enable pin of the level shifter gate for MISO is connected to ground, which means the gate is always driving the MISO line.  What's supposed to happen is that MISO is supposed to be tristate unless that module's CS line is activated (low), which would have only one module driving the line at a time.  So these modules work fine by themselves, but don't play well with others.

You could try to disconnect that gate's enable pin from ground, and connect it instead to CS.  But running MISO through the level shifter doesn't accomplish anything here anyway (MISO is 3.3V coming out of the SD card, and will still be 3.3V coming out of the shifter).  So I think the easiest fix is to just connect MISO directly from the card to the header, and cut whatever trace needs to be cut to disconnect the shift gate from the header.  In my experience, SD cards handle MISO correctly by themselves, although there may be some dispute about that.

I don't know of a microSD module that handles MISO correctly, other than perhaps an Adafruit or Sparkfun product.





slokesh

Thank you both of you for making me understand the reason behind the issue. It was indeed helpful.


Hello markd833,
All my searches are taking me to the sites where SD card access is explained only the way that I had tried - with hardware SPI interface.
I want to know more about the software SPI interfacing.  Can you please elaborate the approach of 'SD card adaptor using a software SPI interface". Or provide me with a link where I can read and learn.
.
Thank you.


Hello ShermanP,
Can I use 2 adafruit SPI modules, the one in below link, to make my use case work?

https://www.digikey.in/product-detail/en/adafruit-industries-llc/254/1528-1462-ND/5761230

Thanks for you help.

ShermanP

Can I use 2 adafruit SPI modules, the one in below link, to make my use case work?

https://www.digikey.in/product-detail/en/adafruit-industries-llc/254/1528-1462-ND/5761230

I think so.  The schematic for that Adafruit module shows MISO being connected directly from the SD card to the module's DO (data out) header pin. This leaves you in the same situation as the fix I proposed above. So if your SD cards know how to behave properly, these should work. 


markd833

There's a software SPI library here but I have not tried it.

Go Up