Can i use 2 rc522 rfid cards one ine arduino?

Sorry, didn't realize you were powering the Arduino at 3.3V. That wasn't clear from the earlier posts.

For SPI, SCK. MOSI, MISO all connect in parallel to all devices.
Then one device at a time is communicated with by use of a unique SS pin to each device.

How much current do the slave devices need? Which Arduino are you using? Some have a beefier output power current capacity than others.

A reader like this
https://www.amazon.com/IZOKEE-RFID-RC522-13-56MHz-Arduino-Raspberry/dp/B076HTH56Q/ref=asc_df_B076HTH56Q/?tag=hyprod-20&linkCode=df0&hvadid=309750549985&hvpos=1o1&hvnetw=g&hvrand=11024653295931799908&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=9001869&hvtargid=pla-564122493348&psc=1
draws 30mA or less, so two would probably be okay on even a Promini with just a 150mA regulator.

CrossRoads:
Sorry, didn't realize you were powering the Arduino at 3.3V. That wasn't clear from the earlier posts.

For SPI, SCK. MOSI, MISO all connect in parallel to all devices.
Then one device at a time is communicated with by use of a unique SS pin to each device.

How much current do the slave devices need? Which Arduino are you using? Some have a beefier output power current capacity than others.

my arduino is made by keyestudio. I'm powering it with usb atm, but need to pick a ac to dc power supply for it soon. it has a 5v and a 3.3v output.I have no clue what amperage it can handle.

Grumpy_Mike:
Yes.

Something that converts a 5V signal into a 3V3 signal.
This could be just a potential divider - this tends to slow down the edges but is the cheapest
You can use a transistor in the common base mode or use FETs
Or you can use a chip with a 3V3 power supply if this chip has a 5V tolerant input.
No that sample code is crap and simply falls through a host of if statements and returns to the start if any one fails. That is not something you can do when you have two readers because you never get to look at the second one.

Look at that code, understand what it is doing and then write it properly to cope with two readers.

will this work?

void loop() {
  // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
  if (!mfrc522.PICC_IsNewCardPresent()&&!mfrc5221.PICC_IsNewCardPresent()) { //if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    return; //if it did not find a new card is returns a '0' and we return to the start of the loop
  }
  // Select one of the cards
  if (mfrc522.PICC_ReadCardSerial()== 1) { //if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
       //Store Card UID
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = mfrc522.uid.uidByte[i];
    }
  }  
  // Select one of the cards
  else if (mfrc5221.PICC_ReadCardSerial()== 1) { //if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
      //Store Card UID
     for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = mfrc5221.uid.uidByte[i];
    } //if it returns a '0' something went wrong and we return to the start of the loop
  }else{
    return;
  }

fixed code error in last post.

 // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
  if (!mfrc522.PICC_IsNewCardPresent()||!mfrc5221.PICC_IsNewCardPresent()) { //if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    return; //if it did not find a new card is returns a '0' and we return to the start of the loop
  }

You only know that a card is present (or not) but you don't know on which reader. You fixed that partially in the edited version.

You will be far better of writing two functions to handle each of the two mfrc devices. One function for the entrance reader and one function for the exit reader.

You should also give the two mfrc instances decent names, e.g. mfrc_entrance and mfrc_exit and use those names.

Have you considered how you're going to transfer SPI signal over 10-plus meters? I don't think that TTL signals will be reliabe at those distances.

sterretje:

 // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)

if (!mfrc522.PICC_IsNewCardPresent()||!mfrc5221.PICC_IsNewCardPresent()) { //if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    return; //if it did not find a new card is returns a '0' and we return to the start of the loop
  }



You only know that a card is present (or not) but you don't know on which reader. You fixed that partially in the edited version.


You will be far better of writing two functions to handle each of the two mfrc devices. One function for the entrance reader and one function for the exit reader.

You should also give the two mfrc instances decent names, e.g. mfrc_entrance and mfrc_exit and use those names.

Have you considered how you're going to transfer SPI signal over 10-plus meters? I don't think that TTL signals will be reliabe at those distances.

if i'm doing it right, the 2nd if statement should choose the reader with a card?

Does it make sense to do a mfrc522.PICC_ReadCardSerial() if no new card is present on that reader? I have no idea of the side effect, but it does not make sense.

As said, use two functions. Go back to your setup with one reader and copy the reading part to a seperate function, e.g. readEntrance().

Next duplicate that function and name it readExit().

e.g.

bool readEntrance()
{
  // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
  if (!mfrc_Entrance.PICC_IsNewCardPresent()) { //if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    return false; //if it did not find a new card is returns a '0' and we return to the start of the loop
  }
  // Select one of the cards
  if (!mfrc_Entrance.PICC_ReadCardSerial()) { //if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
    return false; //if it returns a '0' something went wrong and we return to the start of the loop
  }

  ...
  ...

  return true;
}

All the current returns will return false to indicate that something is 'wrong' (no card, failed to read etc). It will only return true if the function successfully completed.

Note that readExit() needs to be adjusted to use the other instance (mfrc_Exit).

Your loop() can than be something like

void loop()
{
  // check entrance
  bool entranceHasCard = readEntrance();

  if(entranceHasCard == true)
  {
    // do whatever you have to do when a card was read at the entrance
    ...
    ...
  }

  // check exit
  bool exitHasCard = readExit();

  if(exitHasCard == true)
  {
    // do whatever you have to do when a card was read at the exit
    ...
    ...
  }


  // other stuff
  ...
  ...

}

I see what you are saying. Ill work on it in the morning. Thank you.

my arduino is made by keyestudio. I'm powering it with usb atm, but need to pick a ac to dc power supply for it soon. it has a 5v and a 3.3v output.I have no clue what amperage it can handle.

Well as you are not using a read Arduino then you better ask keyestudio that question.

As the two readers essentially do the same thing, that is open the door I would write the bulk of the code as one function and pass into it the results from your reader.

How does this look? I didn't use functions but i believe this will work without always returning false and not trying to read anything it shouldn't be reading.

void loop() {
  // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
  if (mfrc522.PICC_IsNewCardPresent() == 1) { //if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    if (mfrc522.PICC_ReadCardSerial()== 1) { //if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
       //Store Card UID
      for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = mfrc522.uid.uidByte[i];
      }
    }  
  }else if(mfrc5221.PICC_IsNewCardPresent() == 1){
    if (mfrc5221.PICC_ReadCardSerial()== 1) { //if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
      //Store Card UID
      for (byte i = 0; i < 4; i++) {
        nuidPICC[i] = mfrc5221.uid.uidByte[i];
      } //if it returns a '0' something went wrong and we return to the start of the loop
    }
  }else{
    return; //if it did not find a new card is returns a '0' and we return to the start of the loop
  }

ty

At a first glance this looks fine. However you should look at hiving off the duplicate code after each if into a function.

That would be proper. I also need to handle which reader is being used for writing to the cards. but I can handle that with a basic "CurrentUsedReader" variable.

thank you very much for your help.

Or make an array of objects, like this (I think?):

MFRC522 mfrc522[2] = {
  MFRC522(SS_PIN, RST_PIN),
  MFRC522(SS_PIN1, RST_PIN1)
}; // instatiate an array of MFRC522 reader objects.

Then you can iterate over the array with a for() loop.

ok..... so.. like 5 questions later, and deleting those 5 questions. all on this post.... this is what I ended up with. i can't believe it works. sooooo happy. all thats left is to see how far it can go and. yay!

i kinda changed stuff tho.

#include <SPI.h>//include the SPI bus library
#include <MFRC522.h>//include the RFID reader library
#include <EEPROM.h>
#define SS_1_PIN 5  //slave select pin
#define SS_2_PIN 6  //slave select pin
#define RST_PIN 7  //reset pin
#define NR_OF_READERS 2
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
//MFRC522 mfrc522(SS_PIN, RST_PIN); // instatiate a MFRC522 reader object.
MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.
MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key', which will hold the card information

unsigned long intervalval=4000;
byte nuidPICC[4];
int PasswordBlock = 2; //this is the block number we will write into and then read. Do not write into 'sector trailer' block, since this can make the block unusable.
int BanKeyBlock = 4;//this is the block that stores a key programmed into a tag to be deactivated when that tag is used
byte blockcontent[16] = {"ppcc"}; //an array with 16 bytes to be written into one of the 64 card blocks is defined!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
byte deactivatecardblockcontent[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//all zeros. This can be used to delete a block.
byte readbackblock[18]; //This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.
String txt; //This is where we store block 2, the password

uint8_t reader;
void setup() {
//create A0 as led
  pinMode(4, OUTPUT);//led
  pinMode(3, OUTPUT);//RPWM (right pwm on motor controller)
  pinMode(2, INPUT_PULLUP);//signal from sensors
  pinMode(8,OUTPUT);//LPWM (left pwm on motor controller)
  digitalWrite(8, LOW);
  digitalWrite(3, LOW);
  Serial.begin(9600); // Initialize serial communications with the PC
  SPI.begin(); // Init SPI bus
  for (reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();
  }
  Serial.println("Scan a MIFARE Classic card");
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF; //keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
  }
}

void loop() {
  // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
  for (reader = 0; reader < NR_OF_READERS; reader++){
  if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
  Serial.println("found card");
  //Store Card UID
      for (byte i = 0; i < 4; i++) {
        nuidPICC[i] = mfrc522[reader].uid.uidByte[i];
      }