RFID & SD Card using both together


I have an Arduino Mega 2560. When I connect the RFID-RC522 to the Arduino it works fine. I also have a MicroSD shield, when connected this works fine. When I connect them both at the same time it does not work. So I started reading up about SPI, watched some videos and read some posts about it. I have a basic understanding of it.

I have put a 330K Resistor between the SD and the RFID on the MOSI connection, when I do this the RFID works with them both wired up, but the SD does not work.

#define CS_RFID 53 // Chip Select for RFID
#define CS_SD 10 // Chip Select for SD Card

pinMode(CS_SD, OUTPUT);
digitalWrite(CS_SD, LOW);
digitalWrite(CS_RFID, HIGH); // Disable the RFID

I have separate CS for each device, one on 10 and the other 53, I have set them as outputs. When I set the RFID CS HIGH it still works, it’s like I can not turn it off, this seems to be the problem.

Im trying to turn RFID off, write to a file, then turn RFID back on.

Has anyone got some advice for me please?

code.txt (1.09 KB)

Have you tried with 330 Ohm rather than 330k Ohm ?

Also a Sdcard may take up to 200mA peak - so better power it directly and join GND rather than through your arduino (depending on what else you do there)

I don’t understand why you would need to mess with the MOSI line. It’s a Master SPI output that drives the relevant slave SD and RFID inputs at the same time, but only the one with its CS low should be paying attention. So I don’t see the need for a resistor.

But the MISO pin is another matter. Both the SD and the RFID modules drive this line. But it should be an active output only from the one with its CS active (low). However, your microSD module likely has a voltage translator chip, and if the SD MISO line goes through a gate of this chip, the output may be driven high by the translator even if the SD itself is open drain or tristate because its CS line is off.

Something similar could be taking place with the RFID module, in which case you may have the two MISO outputs being actively driven in opposite directions. Bus contention they call it. If both modules work correctly when the other is absent, but fail when both are present, this seems to me to be the most likely explanation.

One solution might be to install a pullup resistor on the processor’s MISO pin, then connect that pin to the SD and RFID pins through diodes - oriented so that either module can bring the line low, but neither can force it high. That should eliminate any contention. I’ll attach a schematic showing changes for the SD module.

And you may also need a pullup resistor on the SD card’s MISO pin. This would prevent the input of the voltage translator gate from floating if the SD’s MISO pin floats when its CS is off. Some SD cards behave that way, others do not, but to be safe, you would want to provide for this possibility so all brands of SD cards would work.

Edit: Another possibility if you can physically get to the pin is to disconnect the output enable pin for the relevant gate of the level shifter chip from ground (always enabled), and connect it instead to the CS pin. Then you wouldn’t need any diodes or resistors, and the shifter output would be tristate unless CS is active, which is exactly what we want. Actually, these modules should have been designed that way in the first place.

You are right - ideally you would not need to mess around with it but seems this worked for some SD cards - messing around enough with voltage.

Ideal approach would probably be to tristate the line