Can two Arduinos share an SD card? Locking mechanism?

Hi All...

I need to have two different Arduino devices share an SD card. They would be reading and/or writing different files on the same card.

Hardware would be something like Uno 1 has an SD card shield, and Uno 2 has its SPI bus connected to the SPI bus of Uno 1. Each Arduino is running a different sketch.

Clearly each Arduino can't talk to the SD card simultaneously. So it seems that the answer is some kind of locking mechanism that both Arduinos share, so that either can lock a resource?

I realize I could write code so that the sketch in Uno 1 could act as kind of a server for the SD card that could be queried by Uno 2. That's not the solution I'm looking for.

Thanks...

I do not think connecting 2 arduino SPI buses will work. If you do that, then you will communicating with the UNO's and not the SD card. You are right that you cannot simultaneously communicate with SD card.

I am not 100% sure on the this next comment but I will throw it out there in case it helps. I believe the SPI pins (MOSI, MISO, SCK) transition low to high and vice versa when communicating. Thus, if you have 2 SPI buses connected to an input to an SD card then when 1 UNO tries to read and does a transition, it MAY cause issues for the other UNO. The other UNO, may read gibberish in. Again, I am not sure. I have not tested this.

Are you certain you want to add that much complexity to your application?

Two pins and a tie resolution strategy. That's what it will take to implement a semaphore. Anything less (like trying to use a single pin) and you run the risk of a conflict.

It will be very difficult to make this work. The SPI bus can only have one master so you will need to disable the SPI bus on the Arduino that is not accessing the SD.

You will need to close all files, disable the SPI controller on one Arduino and set the state of all SPI pins and chip select to inputs.

You then need to initialize the SD card and open files on the other Arduiono.

There is probably more that I haven't thought of.

Okay, thanks. Perhaps I'll give up on this idea then. The alternative is to do what i said earlier, which is to write a bit of code to act as a file server of sorts. Perhaps I'll put a high end processor (Atmel UC3 or ARM 7) ont he shield. Then I can run a RTOS and implement a file server process.

fat16lib:
It will be very difficult to make this work. The SPI bus can only have one master so you will need to disable the SPI bus on the Arduino that is not accessing the SD.

You will need to close all files, disable the SPI controller on one Arduino and set the state of all SPI pins and chip select to inputs.

You then need to initialize the SD card and open files on the other Arduiono.

There is probably more that I haven't thought of.

Hi, I tried this method. However, I can only access to the sd card once.
Here is my system: arduino1 and arduino2
arduino1 was writing the file
arduino2 sent a "$" to arduino1
arduino1 recieved "$" and put SDCS, SDDI, SDDO, SDCLK into input. Then it did spi.end() and sent a "#" to arduino2
arduino2 recieved "#" and put SDCS, SDDI, SDDO, SDCLK into output. Then it did SD.begin(10) and started reading file
After arduino2 read the file, it put SDCS, SDDI, SDDO, SDCLK into input, and did a spi.end(). After that, it sent a "#" to arduino1
arduino1 recieved "#" and put SDCS, SDDI, SDDO, SDCLK into output. It did a SD.begin(10), and started to write to the sd card again

This procedure can only work once. I checked sd.begin in the arduino1 for the last step. It returned false. Also, when during the second time the communication started, the SD.begin returned false in arduino2.

How can i make this work? I think there might be something wrong with multiple SD.begin.

I would put a mux chip with the SD card and control who's signals are getting thru (or put a tristate drive with each board).
Have some comm's between the 2 boards so that one can tell the other when it's file accesses are completed, and switch the control logic over.

SD.h has a bug that prevents more than one call to SD.begin(). A fix is posted somewhere in the forum.

SdFat allows multiple calls to begin().