2
votes

I have a sketch running a MFRC522 RFID reader which works fine but my aim is to log the card swipes on a microSD card.

The problem is that as soon as I add the SD card reader, whether it is initialised or not, the RFID reader stops working. It seems to be a problem on the SPI bus. I have tried adding pull up resistors to the circuit and setting the chip select pins to HIGH before initialising either of the boards but nothing seems to work.

Here is my code:

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

// RFID constants & objects
#define RFPin 10
#define resetPin 9
MFRC522 mfrc522(RFPin, resetPin);

// SD constants
#define SDPin 8

// Other global variables
String IDString;
byte IDList[4];

void setup() {
  pinMode(2, OUTPUT);// For testing

  // Set both chip select pins high
  pinMode(SDPin, OUTPUT);
  digitalWrite(SDPin, HIGH);
  pinMode(RFPin, OUTPUT);
  digitalWrite(RFPin, HIGH);
  delay(10);

  // Init serial bus
  Serial.begin(9600);
  // Wait for serial bus to open
  while (!Serial);// Opens even when not USB connected

  // Init SPI bus
  SPI.begin();
  // Initialise RFID board
  mfrc522.PCD_Init();

  delay(1000);// Just in case SPI is still busy

  // Initialise SD card board
  if (!SD.begin(SDPin)) {
    // SD card board failed to initialise
    Serial.println("SD failed");
    return;
  }
}

void loop() {

  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  for(int i = 0; i < 4; i++) {
    digitalWrite(2, HIGH);
    delay(100);
    digitalWrite(2, LOW);
    delay(100);
  }

  IDString = "";

  // Get 4 byte ID
  for (int i = 0; i < 4; i++) {
    IDList[i] = mfrc522.uid.uidByte[i];
    // Serial.print(IDList[i], HEX);
    if(IDList[i] < 16) {
      IDString += "0" + String(IDList[i], HEX) + "-";
    } else {
      IDString += String(IDList[i], HEX) + "-";
    }
  }
  mfrc522.PICC_HaltA(); // Stop reading
  IDString = IDString.substring(0, IDString.length() - 1);

  Serial.println(IDString);
  .
  .
  .
  Rest of code

Everything initialised ok but Once in the main loop, the program never gets past the first statement: to check if an RFID card is present.

Can anybody help? Could it be de to the fact that the RFDI board is 3.3V driven and the SD board is 5V driven (but has a 3.3V regulator)? Each has it's own separate power wire

EDIT #1: When the MicroSD board is plugged in but not powered the RFID board works fine I have also tried adding a subroutine to pull both chip select pins high at the beginning of every loop to no avail.

EDIT #2: The SD board works in this sketch and I can get the card details from it.

EDIT #3: The RFID card works again once I remove the MISO line from the SD board. Obviously the SD module isn't releasing the MISO line...

1

1 Answers

1
votes

It's not the most elegant solution but I have it working now by attaching an NPN transistor between the MISO output of the SD board and the MISO line to pin 12. It takes one further pin to block/unblock the MISO line to be used by the SD board but it works.

As I said this is not elegant and I would still be eager to hear a better solution.