0

I have a problem!

I need to connect 5 MFRC522 readers on an Arduino Uno but it is not working !

With one or two MFRC522 modules it works, when I use three or more nothing works anymore... The serial reading is not working, nothing is read with more than 2 modules. I'm so sad. So tonight I need your help, tell me what is not working in my code or on my setup!

I first thought that it was the Arduino that was not delivering enough current to the modules (on the 3.3v line) but this does not seem to be the case. We added some external ATX PSU power, and connected grounds. Nothing changed.

So we had a look at the code and we believe that it is a library problem. Nobody seems to have successfully connected more than 3 MFRC522 modules. Is there really a problem in the library? How could we achieve success?

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

#define SS_PIN_1 10 // PIN SDA pour le module RFID (1)
#define RST_PIN_1 9 // PIN RESET pour le module RFID (1)
#define SS_PIN_2 8 // PIN SDA pour le module RFID (2)
#define RST_PIN_2 7 // PIN RESET pour le module RFID (2)
#define SS_PIN_3 6 // PIN SDA pour le module RFID (3)
#define RST_PIN_3 5 // PIN RESET pour le module RFID (3)
#define SS_PIN_4 4 // PIN SDA pour le module RFID (4)
#define RST_PIN_4 3 // PIN RESET pour le module RFID (4)
#define SS_PIN_5 2 // PIN SDA pour le module RFID (5)
#define RST_PIN_5 A0 // PIN RESET pour le module RFID (5)
#define RELAY_PIN A1 // Pin du signal du Relais
#define RELAY_ON 1024 // Valeur de seuil d'activation du relais
#define RELAY_OFF 0 // Valeur de seuil de désactivation du relais
#define MASTER_BUTTON_PIN A2 // Pin du bouton d'ouverture prioritaire
#define CONFIGURE_BUTTON_PIN A3 // Pin du bouton de reprogrammation des badges
MFRC522 rfid1(SS_PIN_1, RST_PIN_1);
MFRC522 rfid2(SS_PIN_2, RST_PIN_2);
MFRC522 rfid3(SS_PIN_3, RST_PIN_3);
MFRC522 rfid4(SS_PIN_4, RST_PIN_4);
MFRC522 rfid5(SS_PIN_5, RST_PIN_5);
byte tag1[4] = {0xD6, 0x92, 0xBF, 0x93};
byte tag2[4] = {0xC6, 0xA3, 0xC0, 0x93};
byte tag3[4] = {0x96, 0x58, 0x68, 0x8E};
byte tag4[4] = {0x06, 0xFB, 0xC1, 0x93};
byte tag5[4] = {0x66, 0xF3, 0x63, 0x8E};

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid1.PCD_Init();
  rfid2.PCD_Init();
  rfid3.PCD_Init();
  rfid4.PCD_Init();
  rfid5.PCD_Init();
  Serial.println("Initialisation du système terminé, en attente de badges...");
}

void activate_relay(){
  analogWrite(RELAY_PIN, RELAY_ON);
  Serial.println("Le relay est activé");
}

void desactivate_relay(){
  analogWrite(RELAY_PIN, RELAY_OFF);
  Serial.println("Le relay est désactivé");
}

void loop() {
  if (! rfid1.PICC_IsNewCardPresent()) {
    return;
  }
  if (! rfid1.PICC_ReadCardSerial()) {
    return;
  }
  if (! rfid2.PICC_IsNewCardPresent()) {
    return;
  }
  if (! rfid2.PICC_ReadCardSerial()) {
    return;
  }
   if (! rfid3.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! rfid3.PICC_ReadCardSerial()) {
    return;
  }
   if (! rfid4.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! rfid4.PICC_ReadCardSerial()) {
    return;
  }
   if (! rfid5.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! rfid5.PICC_ReadCardSerial()) {
    return;
  }
  if (rfid1.uid.uidByte[0] == tag1[0]  & rfid1.uid.uidByte[1] == tag1[1] & rfid1.uid.uidByte[2] == tag1[2] & rfid1.uid.uidByte[3] == tag1[3]) {
    if (rfid2.uid.uidByte[0] == tag2[0]  & rfid2.uid.uidByte[1] == tag2[1] & rfid2.uid.uidByte[2] == tag2[2] & rfid2.uid.uidByte[3] == tag2[3]) {
      if (rfid3.uid.uidByte[0] == tag3[0]  & rfid3.uid.uidByte[1] == tag3[1] & rfid3.uid.uidByte[2] == tag3[2] & rfid3.uid.uidByte[3] == tag3[3]) {
        if (rfid4.uid.uidByte[0] == tag4[0]  & rfid4.uid.uidByte[1] == tag4[1] & rfid4.uid.uidByte[2] == tag4[2] & rfid4.uid.uidByte[3] == tag4[3]) {
          if (rfid5.uid.uidByte[0] == tag5[0]  & rfid5.uid.uidByte[1] == tag5[1] & rfid5.uid.uidByte[2] == tag5[2] & rfid5.uid.uidByte[3] == tag5[3]) {
          } else {desactivate_relay();}
        } else {desactivate_relay();}
      } else {desactivate_relay();}
    } else {desactivate_relay();}
  } else {desactivate_relay();}
}

I've tried the code above but it didn't work. I also tried the ReadUidMultiReader example code but only one reader is working. Both Readers are working individually but only one when I connect them. I shared the SCK, MISO, MOSI, RST, and have different SS pins.

enter image description here

Thank you in advance.

  • I don't see the code – Mazaryk Apr 10 '17 at 19:29
  • @Mazaryk Hello ! I'm glad you'r here to help the comunity ^^. The code has been added. Enjoy :D – masterleo Apr 10 '17 at 20:36
  • 1
    You can use analog pins as digital. You don't need to use analogWrite. Also your code could be a lot shorter with arrays and for loops. – gre_gor Apr 10 '17 at 21:01
  • I'm working on a schematic for you, fritzing <3 – masterleo Apr 11 '17 at 0:40
  • @gre_gor That's a good idea indeed ! Will try it tomorrow when I go back to my local Fablab. – masterleo Apr 11 '17 at 0:41
4

For convenience, here is a comment: You wrote, “I've tried the code above but it didn't work”. That is inadequate to convey what happened and what you wanted to happen. Please edit your question to include a clear statement of symptoms and desired results.

Now on to an answer.

From “I'm only using 2 readers to find the correct and make it work”, I deduce that you have two readers attached. This by itself is enough to keep the code from producing output. Assuming those two readers identify as rfid1 and rfid2, when execution gets to if ( ! rfid3.PICC_IsNewCardPresent()), it executes return; which terminates loop() processing. That is, execution will never get past if ( ! rfid3.PICC_IsNewCardPresent()). To clear up that problem, remove all the code that references rfid variables other than rfid1 and rfid2 (more properly, other than whichever two you have attached).

That will entail removing 18 lines in the early part of loop(), and three lines in the big nested-if part of loop().

Here is another comment: When you are debugging code, it makes sense to report lots of intermediate results, and to report them as soon as they are available. Don't go through a long stack of if statements and get no output and then say it doesn't work; instead, display and check results when you first read them in.

  • I upvoted this question when it used to help me but now I can not un-vote it ? stange website. The project still does not work. – masterleo Apr 18 '17 at 12:50
  • “still does not work” doesn't tell what happened and what you want to happen. Please edit your question to include a clear statement of symptoms and desired results. You edited the question to add a diagram but still just say "didn't work" instead of anything about intermediate results. ¶ The notion that you should unvote this that used to help, but now doesn't because you changed things, is perverted. It discourages me from replying further: If I edit my reply, then you can change your vote; so now I won't change it and probably won't reply further. Oh well. – James Waldby - jwpat7 Apr 18 '17 at 13:34
  • However, you could write a new question with a clear statement of symptoms, desired results, current code, and intermediate-values printout. – James Waldby - jwpat7 Apr 18 '17 at 13:48
  • No, don't worry I won't down-vote your question, so I'm going to edit my question and make a better schematic. – masterleo Apr 19 '17 at 21:08
  • Ok, great. Printing intermediate-values probably would be the most useful item to add. Also, the sequence of 10 if (...) { return; } statements is such that if any one of those tests fail, you return from loop() with no further processing on that pass. You could instead set status variables (or status bits in a word) so at least something gets reported. – James Waldby - jwpat7 Apr 19 '17 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.