Hy,
I have problems with multiple RFID(MFRC522) usage.
I use the MFRC522.h library from GitHub - miguelbalboa/rfid: Arduino RFID Library for MFRC522, and I want to read 8 RFID modules with one Arduino Uno, but I have managed only 2!
I connect all the SPI pins, SS1 to pin10 and SS2 to pin2.
How can I get to read 8 RFID with one Arduino Uno?
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_1_PIN 10 // Configurable, see typical pin layout above
#define SS_2_PIN 2 // Configurable, see typical pin layout above
//#define SS_3_PIN 10
#define LedPin 8
#define NR_OF_READERS 2
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
int a=0;
int b=0;
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
/**
* Initialize.
*/
void setup() {
Serial.begin(115200); // Initialize serial communications with the PC
//while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
pinMode(LedPin,OUTPUT);
SPI.begin(); // Init SPI bus
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
}
}
/**
* Main loop.
*/
void loop() {
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new cards
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
Serial.print(F("Reader: "));
Serial.print(reader);
// Show some details of the PICC (that is: the tag/card)
Serial.print(F(" Card UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
if (mfrc522[reader].uid.uidByte[0] == 140 &
mfrc522[reader].uid.uidByte[1] == 120 &
mfrc522[reader].uid.uidByte[2] == 34 &
mfrc522[reader].uid.uidByte[3] == 43 &
reader==0 ) {
Serial.println(F("pirmā karte atrasta"));
a++;
}
if (mfrc522[reader].uid.uidByte[0] != 140 &
mfrc522[reader].uid.uidByte[1] != 120 &
mfrc522[reader].uid.uidByte[2] != 34 &
mfrc522[reader].uid.uidByte[3] != 43 &
reader==0 ) {
Serial.println(F("pirmā karte nodzes"));
a=0;
}
if (mfrc522[reader].uid.uidByte[0] == 5 &
mfrc522[reader].uid.uidByte[1] == 233 &
mfrc522[reader].uid.uidByte[2] == 26 &
mfrc522[reader].uid.uidByte[3] == 43 &
reader==1 ) {
Serial.println(F("otrā karte atrasta"));
b++;
}
if (mfrc522[reader].uid.uidByte[0] != 5 &
mfrc522[reader].uid.uidByte[1] != 233 &
mfrc522[reader].uid.uidByte[2] != 26 &
mfrc522[reader].uid.uidByte[3] != 43 &
reader==1 ) {
Serial.println(F("otrā karte nodzēs"));
b=0;
}
if (mfrc522[reader].uid.uidByte[0] == 140 &
mfrc522[reader].uid.uidByte[1] == 120 &
mfrc522[reader].uid.uidByte[2] == 34 &
mfrc522[reader].uid.uidByte[3] == 43 &
reader==1 ) {
Serial.println(F("otrā karte nodzēs"));
b=0;
}
if (mfrc522[reader].uid.uidByte[0] == 5 &
mfrc522[reader].uid.uidByte[1] == 233 &
mfrc522[reader].uid.uidByte[2] == 26 &
mfrc522[reader].uid.uidByte[3] == 43 &
reader==0 ) {
Serial.println(F("pirmā karte nodzes"));
a=0;
}
Serial.println();
// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
} //if (mfrc522[reader].PICC_IsNewC
} //for(uint8_t reader
if (a>0 & b>0)
{
digitalWrite(LedPin,HIGH);
delay(1000);
digitalWrite(LedPin,LOW);
}
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
It really does not make sense to use a for loop when every iteration of the loop results in different action. Dump the stupid for loop, and create a function. Call the function 8 times.
Explain what happens when you connect more than 2 devices.
Explain why you are not using memcmp() to compare the array of bytes for a tag to a set of known values.
I wrote the code different from the first one. I have the problem that, when I connect only one RFID reader, then it works, I can change the SS pins for the reader between 10 and 2, and everithing is OK. But when I connect now two RFID readers, nothing happens, I don't know, how to let the Arduino choose one of the two RFID, by changing the chip select.
#include <SPI.h>
#include <MFRC522.h>
#define SS_1_PIN 10
#define SS_2_PIN 2
#define RST_PIN 9
MFRC522 rfid(SS_1_PIN, RST_PIN); // Instance of the class
MFRC522 rfid1(SS_2_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[3];
void setup() {
Serial.begin(9600);
}
void loop() {
RFID();
RFID1();
}
//===========================================
void RFID()
{
//Serial.println("pirmais");
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// Look for new 1 cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( !rfid.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
SPI.end();
}
//==============================================
void RFID1()
{
//Serial.println("otrais");
SPI.begin(); // Init SPI bus
rfid1.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
if ( ! rfid1.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid1.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid1.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid1.uid.uidByte, rfid1.uid.size);
Serial.println();
// Halt PICC
rfid1.PICC_HaltA();
// Stop encryption on PCD
rfid1.PCD_StopCrypto1();
SPI.end();
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
I made the correction for the code which you said, but still my Arduino Uno don't read both RFID readers. The same situation, I think it doesn't select slave device.
#include <SPI.h>
#include <MFRC522.h>
#define SS_1_PIN 10
#define SS_2_PIN 2
#define RST_PIN 9
MFRC522 rfid(SS_1_PIN, RST_PIN); // Instance of the class
MFRC522 rfid1(SS_2_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
rfid1.PCD_Init(); // Init MFRC522
}
void loop() {
RFID();
RFID1();
}
//===========================================
void RFID()
{
//Serial.println("pirmais");
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// Look for new 1 cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( !rfid.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
//==============================================
void RFID1()
{
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
if ( ! rfid1.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid1.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid1.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid1.uid.uidByte, rfid1.uid.size);
Serial.println();
// Halt PICC
rfid1.PICC_HaltA();
// Stop encryption on PCD
rfid1.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
I got another Arduino Uno, and with that the code worked, and also worked the ReadUidMultiReader example code.
It worked perfect with 2 and 3 RFID readers, but when I connected 4, then only two sent the information to the Arduino and one of them sent with a delay. But two of the remaining RFID readers did not react.
#include <SPI.h>
#include <MFRC522.h>
#define SS_1_PIN 2
#define SS_2_PIN 8
#define SS_3_PIN 7
#define SS_4_PIN 4
#define RST_PIN 9
MFRC522 rfid(SS_1_PIN, RST_PIN); // Instance of the class
MFRC522 rfid1(SS_2_PIN, RST_PIN); // Instance of the class
MFRC522 rfid2(SS_3_PIN, RST_PIN); // Instance of the class
MFRC522 rfid3(SS_4_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
void setup() {
Serial.begin(115200);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
rfid1.PCD_Init(); // Init MFRC522
rfid2.PCD_Init(); // Init MFRC522
rfid3.PCD_Init(); // Init MFRC522
}
void loop() {
RFID();
RFID1();
RFID2();
RFID3();
}
//===========================================
void RFID()
{
//Serial.println("pirmais");
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// Look for new 1 cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( !rfid.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
//==============================================
void RFID1()
{
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
if ( ! rfid1.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid1.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid1.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid1.uid.uidByte, rfid1.uid.size);
Serial.println();
// Halt PICC
rfid1.PICC_HaltA();
// Stop encryption on PCD
rfid1.PCD_StopCrypto1();
}
//=============================================
void RFID2()
{
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
if ( ! rfid2.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid2.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid2.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid2.uid.uidByte, rfid2.uid.size);
Serial.println();
// Halt PICC
rfid2.PICC_HaltA();
// Stop encryption on PCD
rfid2.PCD_StopCrypto1();
}
//=============================================
void RFID3()
{
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
if ( ! rfid3.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid3.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid3.uid.uidByte[i];
}
Serial.print(F("In dec: "));
printDec(rfid3.uid.uidByte, rfid3.uid.size);
Serial.println();
// Halt PICC
rfid3.PICC_HaltA();
// Stop encryption on PCD
rfid3.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
I also have the same problem..!!
I need to connect 4 MFRC522 readers in Arduino Mega but I'm only using 2 readers to find the correct and make it work before I use the other 2.. I've tried the code above of stangists 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..
Thank you in advance
nel_buen:
I also have the same problem..!!
I need to connect 4 MFRC522 readers in Arduino Mega but I'm only using 2 readers to find the correct and make it work before I use the other 2.. I've tried the code above of stangists 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..
Thank you in advance
Did you checked that you have enough power for 4 readers?
One RFID reader can take up to 30 mA, so in total could be 120mA.
i have face the same problem, it take me some time to troubleshoot the problem.
i am using low cost RFID rc-522 module cost around 4 USD
1st i have use library mfrc522 example read multi reader, successfully upload to arduino(in this case i use arduino nano)
when i open serial monitor warning appear "Communication failure, is the MFRC522 properly connected?" for both reader
the warning dose not appear if just using 1 reader
then i figure out the problem appear when i connect miso pin to the 2nd reader
then i figure out from google that we need to put pull up resistor to miso pin
the recommended resistor value is 4.7k but i just have 10k , 330R and 220R
i ve successfully run the program with adding pull-up resistor 220R or 330R connected to 5V
Pude utilizar 2 módulos RFID el soft ReadUidMultiReader de las librerias MRFC522 agregando dos diodos zener de 3.1V en cada Pin MISO. Espero que le sea útil a alguien!! Saludos.