Uno with two RFID-readers (RC522)

Hi,

I’m trying to connect two RFID-readers (RC522) to my Uno. I need two because i’m working on a gate with bidirectional access.

I tried to change the chipSelectPin value and initialize again, but thad didn’t work. When I change the pins in setup it works, when I do it in an if/else loop it don’t.

Does somebody know?

edit: code attached.

Regards,
Marthijn

boom.ino (25.7 KB)

You can find my code here

Why can't I find it here? Use the Additional Options... link to attach the code here.

PaulS:

You can find my code here

Why can't I find it here? Use the Additional Options... link to attach the code here.

The link works here, but I attached the code. Thanks!

  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH); //Disable RFID1

  pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(chipSelectPin, LOW); // Activate the RFID reader

Is there some reason to set the mode for the pin by number AND name?

Which of the two RFID readers is now supposed to be active? You need a different chip select pin for each one, although it doesn't appear as though the library even knows about the chip select pin.

PaulS:

  pinMode(6, OUTPUT);

digitalWrite(6, HIGH); //Disable RFID1

pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(chipSelectPin, LOW); // Activate the RFID reader



Is there some reason to set the mode for the pin by number AND name?

Which of the two RFID readers is now supposed to be active? You need a different chip select pin for each one, although it doesn't appear as though the library even knows about the chip select pin.

No, that's a mistake.

On top is chipSelectPin set to 6.

if(sensor == 1){
int chipSelectPin = 14;
int NRSTPD = 15;
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
sensor = 2;
}
else{
int chipSelectPin = 6;
int NRSTPD = 5;
digitalWrite(14, HIGH);
digitalWrite(15, LOW);
sensor = 1;
//}
digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
digitalWrite(NRSTPD, HIGH);
MFRC522_Init();

This code was set in comments (in the loop), but here I tried to change the CSP.

    if(sensor == 1){
      int chipSelectPin = 14;

Here, chipSelectPin is a local variable - it exists only until the } that ends the if statement. The second line is NOT affecting the value of the global chipSelectPin variable.

I see, that’s a very stupid mistake…

void loop()
{
  if(!checkServer){ //Check for card
    digitalWrite(10, HIGH); //disable ethernet
//    digitalWrite(15, LOW);
//    digitalWrite(5, LOW);
//    digitalWrite(14, HIGH); //disable readers
//    digitalWrite(6, HIGH);
//    if(sensor == 1){
//      chipSelectPin = 14;
//      NRSTPD = 15;
//      sensor = 2;
//    }
//    else{
//      chipSelectPin = 6;
//      NRSTPD = 5;
//      sensor = 1;
//    }
//    digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
//    digitalWrite(NRSTPD, HIGH);
//    MFRC522_Init();
    uchar status;
    uchar buffer[MAX_LEN];
    if (selectCard(true)){
      checkServer = true;
      UID.replace(" ", "-");
      UID = UID.substring(0, UID.length() - 1);
    }

This works, but when I submit the commented code it doesn’t…

This works, but when I submit the commented code it doesn't...

Who are you submitting it to? What does the code do, when some stuff (what stuff?) is uncommented?

How are the two readers attached? How do they know which chip select pin is selecting them?

PaulS:

This works, but when I submit the commented code it doesn't...

Who are you submitting it to? What does the code do, when some stuff (what stuff?) is uncommented?

How are the two readers attached? How do they know which chip select pin is selecting them?

All the pins are connected to each board the same. Exept the chipSelectPin and NRSTPD (I don't know what it does) pins, those are going to only 1 reader.

The code (with some lines in comment) works, it scans the number and continues the programme.
My idea with the lines uncommented is to scan in loop 1 reader 1, in loop 2 reader 2, in loop 3 reader 1 etc. by checking the sensor variable (1 or 2) and setting the chipSelectPin to the other.

You need to post a link to the RFID reader you are using, and a link to the library you are using. The assumption that you are making is that the chip select pin actually does something, and that each reader is attached to a separate chip select pin. I need to verify that those are valid assumptions.

I use the same reader as this: http://www.amazon.com/SunFounder-Mifare-Antenna-Proximity-Arduino/dp/B00E0ODLWQ

The library for the reader is in the code. I included the normal SPI lib.

I attached the most recent version of code with this message.

boom.ino (25.6 KB)

I use the same reader as this

I really doubt that Amazon makes those. A link to the manufacturer's site is needed.

The library for the reader is in the code. I included the normal SPI lib.

No, the NAME of the library is in the code. Did you get it at WalMart? If so, I'll stop by on my way home and pick up a copy.

PaulS:

I use the same reader as this

I really doubt that Amazon makes those. A link to the manufacturer's site is needed.

The library for the reader is in the code. I included the normal SPI lib.

No, the NAME of the library is in the code. Did you get it at WalMart? If so, I'll stop by on my way home and pick up a copy.

Do you mean the spi.h file?

I bought mine on eBay, but the Amazon link is the same.

On the reader is: RFID-RC522 Funduino.

When I change the first chipSelectPin, I can switch between the readers.
But when I try it in the loop by using the sensor (1 or 2) variable, it won't switch...

The readers and ethernetshield communicate over the SPI bus.

But when I try it in the loop by using the sensor (1 or 2) variable, it won't switch...

Something happens. You need to tell us what happens. If changing the chipSelectPin variable's value doesn't change the active reader, that's one thing.

All the pins are connected to each board the same. Exept the chipSelectPin and NRSTPD (I don't know what it does) pins, those are going to only 1 reader.

How? Have you bent the pins on the shields so that they don't engage? Have you then connected a jumper wire from each of the chip select pins to the Arduino? A picture showing this might be helpful.