Multiple Devices on SPI Error


I have this Schematic for a RC522 and micro SD card.

The problem happens very early in the program in the setup() function.

If I disconnect one of the devices the other works properly but not at the same time. I think the problem is how I select the slave with the SS pins. Here is my code. What can be wrong?.

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

// Pines SPI
#define SS_RF 10
#define RST_PIN 9
#define SS_SD 8 

// Instancia a la clase MFRC522
MFRC522 mfrc522(SS_RF, RST_PIN);

File Registro;

void EscribirArchSer(String cad)
{
  digitalWrite(SS_SD,LOW);
  digitalWrite(SS_RF,HIGH);
  
  Serial.println(cad);
  Registro = SD.open("status.txt", FILE_WRITE);
  if(Registro){
    Registro.println(cad);
  }
  Registro.close();

  digitalWrite(SS_SD,HIGH);
  digitalWrite(SS_RF,LOW);
}

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  SPI.begin();
  
  digitalWrite(SS_SD,HIGH);
  digitalWrite(SS_RF,LOW);
  delay(50);
  mfrc522.PCD_Init();
  mfrc522.PCD_DumpVersionToSerial();
  delay(50);
  digitalWrite(SS_RF,HIGH);
  digitalWrite(SS_SD,LOW);
  delay(50);
  Serial.println("Iniciando SD...");
  if (!SD.begin(SS_SD)) {
    Serial.println("Fallo de Inicializacion");
    return;
  }
  Serial.println("Inicializacion Correcta");
  delay(50);
  digitalWrite(SS_SD,HIGH);
  digitalWrite(SS_RF,LOW);

  EscribirArchSer("#############################################");
  EscribirArchSer("################ RESET PLACA ################");
  EscribirArchSer("#############################################");

  EscribirArchSer(F("Acerca la tarjeta al lector para escanear."));
  EscribirArchSer(F("Comprobando las claves:"));
  EscribirArchSer("Key-A: ");
  EscribirArchSer(mostrarByteArray(keyA.keyByte, MFRC522::MF_KEY_SIZE));
  EscribirArchSer("Key-B: ");
  EscribirArchSer(mostrarByteArray(keyB.keyByte, MFRC522::MF_KEY_SIZE));
}

The Error I get:
image

The problem is that some SD card adapters dont work well when there are other SPI devices in use.

Some of them can be hacked to work properly, but a lot of stuff such as SD cards is 3.3V logic and making them behave properly on 5V logic Arduinos such as your Mega is not so easy.

1 Like

Perty picture but it does not come close to a schematic. No power connections to the arduino, no pin numbers, no identification of the part pins etc. Post links to technical information on each of your hardware devices. Validate the voltage of each device and if not the same how do you interface to them.

If @srnet is on the right track (and I think he/she is), your SD card should work with both SPI devices connected. You should be able to easily prove that by taking one of the SD examples and run it with both devices connected; just don't try to access the MFRC (just keep SS_RF HIGH).

Please post a link to the schematic of the SD module.

Hii,
A circuit diiagram with component labels and pin names would be appreciated.

Tom... :smiley: :+1: :coffee: :australia: