RFID tag and SD card module issue

So I started a project with my Arduino UNO where I connected an SD card module and an RFID reader. They share the SPI bus regarding MISO, MOSI and SCK, and therefore the code needed to be tweaked using digitalWrite() and manipulating HIGH and LOW values (I suppose). I have everything wired properly and when running the code the serial monitor shows that both the SD card and the RFID reader are initialized, however the RFID tags are not noticed at all. Both parts work individually with different codes so I suppose it has to do with the SPI connection, but I'm not sure how to fix it. Any ideas? Thanks a lot!

#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>

#define SD_CS 4       // SD card CS pin
#define RFID_CS 10    // RFID CS pin
#define RST_PIN 7     // RFID reset pin

MFRC522 rfid(RFID_CS, RST_PIN);
File logFile;

void setup() {
  Serial.begin(9600);
  delay(1000); 

  pinMode(SD_CS, OUTPUT);
  pinMode(RFID_CS, INPUT);

  digitalWrite(SD_CS, HIGH);
  digitalWrite(RFID_CS, HIGH);

  
  SPI.begin();

 
  digitalWrite(RFID_CS, HIGH);
  digitalWrite(SD_CS, LOW);
  if (SD.begin(SD_CS)) {
    Serial.println("SD card initialized.");
  } else {
    Serial.println("SD card FAILED!");
    while (1);
  }
  digitalWrite(SD_CS, HIGH); // Release SD

  // Init RFID
  digitalWrite(SD_CS, HIGH);
  digitalWrite(RFID_CS, LOW);
  rfid.PCD_Init();
  Serial.println("RFID reader initialized.");
  digitalWrite(RFID_CS, HIGH); // Release RFID
}

void loop() {
  // Activate RFID
  digitalWrite(SD_CS, HIGH);
  digitalWrite(RFID_CS, LOW);

  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    Serial.println("RFID tag detected!");

    // Read UID
    String uid = "";
    for (byte i = 0; i < rfid.uid.size; i++) {
      if (rfid.uid.uidByte[i] < 0x10) uid += "0";
      uid += String(rfid.uid.uidByte[i], HEX);
    }
    uid.toUpperCase();
    Serial.print("UID: ");
    Serial.println(uid);

    // Release RFID
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
    digitalWrite(RFID_CS, HIGH);

    // Access SD to write
    digitalWrite(SD_CS, LOW);
    logFile = SD.open("rfid_log.txt", FILE_WRITE);
    if (logFile) {
      logFile.println(uid);
      logFile.close();
      Serial.println("UID saved to SD card.");
    } else {
      Serial.println("Error opening rfid_log.txt");
    }
    digitalWrite(SD_CS, HIGH);

    delay(1000); // Simple debounce
  }

  // Always release both CS
  digitalWrite(SD_CS, HIGH);
  digitalWrite(RFID_CS, HIGH);
}

Probably not. The device libraries should take care of manipulating the CS lines.

That said, the SPI bus is not well standardized and some modules simply conflict with others in releasing control of shared bus lines. I recall reading that some SD card modules are among those offenders, but it could also be the reader.

If that is the case in your situation, one solution is to use software SPI for one of the modules, and not share the common SPI lines.

But first, you should suspect your own code. There is should be no need for you to manipulate the CS lines.

Which SD card module, exactly?

Do you have level shifters on the (almost always) 3.3V only MFRC522 module?

+1 for mismatch of logic levels. Even if the SPI bus has level shifters, there is no reason to believe that the CS lines have them. You MUST check!

Best to move to 3.3V processors as virtually everything else is already there, and 5V logic will soon be a relic of the past.

Why? RFID_CS is the chip select for the RFID module. It surely is an output for the Arduino.

One common design is known for not releasing the MISO line, which will interfere with anything else connected on the SPI bus.

I have a typical microsd card adapter with 6 pins (CS, SCK, MOSI, MISO, VCC, GND) and it works on 5V, I connected it to 5V and the RFID reader to 3.3V (it's the RC522 and it applies to it too). Also the microsd adapter has a voltage regulator on the back but I don't think that would make a difference in my case

which design exactly? i have a microsd adapter with 6 pins, MH written in the corner and a voltage regulator

it was on output previously and the result is the same, i was just trying it and accidentally left it like that haha

Without knowing exactly which uSD card adapter you have, it's not possible to say whether it is one of the ones that suffers from a design flaw that prevents it from sharing the SPI bus. Good luck with your project.

Good to know, thanks.

What else might not be correct? Sheesh!