How do I interface two SD cards in the same project?

#include <SPI.h>
#include <SD.h>
#define SD_CS     48
#define NAMEMATCH ""
#define PALETTEDEPTH   16     // support 256-colour Palette
char namebuf[32] = "/";
int pathlen;
String nfm = "";
File dataFile;

void setup() {
  Serial.begin(9600);
  if (!bme.begin()) {
    Serial.println(F("Could not find a valid BME680 sensor, check wiring!"));
  }
  pinMode(10, OUTPUT);
  pinMode(SD_CS, OUTPUT);
  if (!SD.begin(10, 11, 12, 13)) {
    Serial.println("Card failed, or not present");
  }
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
  }
  if (! bme.performReading()) {
    Serial.println(F("Failed to perform reading"));
  }
  dataFile.println("data");
  dataFile.close();
  digitalWrite(10, HIGH);
  if (!SD.begin(SD_CS)) {
    Serial.print(F("cannot start SD"));
  }
}

That is the code. But the second SD card never initializes. Why is that?

I have been following this tutorial.

Posting a schematic would help, so far you haven't even told us what Arduino you are using. The SD cards are a SPI interface and each unit needs its own unique select. Did you download KiCad yet?

I'm using a mega with a data logging shield and a 3.95" tft LCD which has it's own sd card. Both SD cards work separately, they just don't both work together.

Yes I've had KiCad for a few months.

While using two SPI devices as a slave you can communicated between them using ChipSelect pin, here you can select one SD card using one CS pin and the other with the other CS pin.

Here in SD.beign what's 10,11,12,13?
Try passing the CS pins separately here.

For example, here your CS pin is 48 ( assuming SD_CS is the one )
So, SD.begin( SD_CS ) for one SD card and for the second use the other CS pin to which your second SD card is connected.

SD cards work at 3.3V but the Mega is a 5V device. For this reason, the shield, and the second SD card module, probably contain 3.3V-5V logic level shifters. Often, the circuits used to shift the levels are as simple and inexpensive as possible and because of that, do not allow other devices to be used on the SPI bus. The problem is that they do not change their MISO pin to high-impedance when the shield/module's SS pin is high.

@samikshap

10 is the chip select pin, the other three are forced SPI pins.

@PaulRB
Are you saying its not possible then?

It may not be possible with the shield and SD card module you have. Can you find schematics for them?

@PaulRB

http://www.lcdwiki.com/3.95inch_Arduino_Display-Mega2560

And I couldn't find a schematic for the datalogging shield, only some data:

Your first link says on the page

On-board 5V/3.3V level shifting IC, compatible with 5V/3.3V operating voltage

This is hopeful, but we need to find out exactly what that chip is to be sure that it will allow other SPI devices in the bus. Maybe this link will help, I cannot open .zip files on my phone so I can't see the schematic.

http://www.lcdwiki.com/res/MAR3953/Altium_Package_library.zip

Your second link does not seem to contain any useful clues. Are there any other links you can post?

It's quite unusual to want to connect 2 SD cards in the same project. Why do you want to do this?

@PaulRB
I want to read images and display them on the lcd screen and write data to the other one. I want to be able to remove the SD card that I am logging data to and not have the images disappear because I removed the only sd card.

That folder contains:

No, I couldn't really find any data at all about the datalogging shield.

@PaulRB

LM1117?

No, that's the voltage regulator, not the logic level shifter. The chip used for that is this one:

This is the kind of chip required to allow multiple devices on the SPI bus. However, this is how it is connected:

Unfortunately, my interpretation of this is that it will not allow multiple devices on the SPI bus because the OE pin is connected to ground. This permanently enables the outputs, including the critical 5V MISO signal. To allow other devices to use the SPI bus, the OE pin should I think have been connected to the SS pin for the SD card, so that when the SS pin is low, the 5V MISO pin would be disabled, allowing other devices on the bus to use it.

Can more experienced forum members please review and give their opinion?

@PaulRB
Do you have recommendations on how to get the data off the SD card without removing it? If that is an option I'd be glad to consider it...

Beam it over via wifi? Does your board have built in wifi?

@build_1971

No. This is a project that will be "on the field", so it can't be internet dependent.

I think I found it

https://forum.hobbycomponents.com/viewtopic.php?t=1779

On this shield, there is no 3.3V-5V level shift for MISO, it connects directly from the SD card reader to the Arduino. For the other pins (MOSI, SCK, SS) there is a CD4050 chip which performs 5V-3.3V level shift. I find this strange but I guess it must work ok. The circuit design is originally Adafruit, so I guess it must be well tested by now.

So for this shield, there should be no problem sharing the SPI bus with other devices. The MISO line will be high-impedance when the SS line is high.

Only the MISO line from SD card on the display is a problem.

There is a small prototyping area on the shield. Perhaps this could be used to build a small circuit to solve the problem? But first of all, I would recommend prototyping that circuit on breadboard and testing it.

A chip that I think could be used to fix the problem could be 74hc125 or 74hc245. 74hc125 is a little smaller, 14 pins instead of 20 on the 74hc245.

You could make a temporary local network by bringing a laptop to your field device. But if you do not have built in wifi on your board, there will be other, easier, ways to achieve what you want.

If this just for yourself, and not a product, you might be able to modify the display module so MISO does not go through the translator chip. You just want MISO to go directly from the card to the header, and you would cut the trace connecting the translator chip to the header. Below are pictures of this fix for the typical microSD card module, which suffers from the same problem.

You might try to trace where the MISO pin of the card holder goes, and where the MISO header pin goes. If they go to a chip, then this is almost certainly the problem.

I'm unable to view the Altium display schematic files, but I suspect they will show MISO being translated, and always on, which is not what you want.

@outbackhut posted the schematic in post #10. It would be good if you could confirm my theory in post #12 that this is the problem here.

The other SD card reader, which is on a shield, has MISO connected direct to the Arduino, with no level shifting, so should be no problem.