ReadUidMultiReader.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example sketch/program showing how to read data from more than one PICC to serial.
  4. * --------------------------------------------------------------------------------------------------------------------
  5. * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6. *
  7. * Example sketch/program showing how to read data from more than one PICC (that is: a RFID Tag or Card) using a
  8. * MFRC522 based RFID Reader on the Arduino SPI interface.
  9. *
  10. * Warning: This may not work! Multiple devices at one SPI are difficult and cause many trouble!! Engineering skill
  11. * and knowledge are required!
  12. *
  13. * @license Released into the public domain.
  14. *
  15. * Typical pin layout used:
  16. * -----------------------------------------------------------------------------------------
  17. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  18. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  19. * Signal Pin Pin Pin Pin Pin Pin
  20. * -----------------------------------------------------------------------------------------
  21. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  22. * SPI SS 1 SDA(SS) ** custom, take a unused pin, only HIGH/LOW required **
  23. * SPI SS 2 SDA(SS) ** custom, take a unused pin, only HIGH/LOW required **
  24. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  25. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  26. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  27. *
  28. * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
  29. *
  30. */
  31. #include <SPI.h>
  32. #include <MFRC522.h>
  33. #define RST_PIN 9 // Configurable, see typical pin layout above
  34. #define SS_1_PIN 10 // Configurable, take a unused pin, only HIGH/LOW required, must be different to SS 2
  35. #define SS_2_PIN 8 // Configurable, take a unused pin, only HIGH/LOW required, must be different to SS 1
  36. #define NR_OF_READERS 2
  37. byte ssPins[] = {SS_1_PIN, SS_2_PIN};
  38. MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
  39. /**
  40. * Initialize.
  41. */
  42. void setup() {
  43. Serial.begin(9600); // Initialize serial communications with the PC
  44. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  45. SPI.begin(); // Init SPI bus
  46. for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  47. mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
  48. Serial.print(F("Reader "));
  49. Serial.print(reader);
  50. Serial.print(F(": "));
  51. mfrc522[reader].PCD_DumpVersionToSerial();
  52. }
  53. }
  54. /**
  55. * Main loop.
  56. */
  57. void loop() {
  58. for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  59. // Look for new cards
  60. if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
  61. Serial.print(F("Reader "));
  62. Serial.print(reader);
  63. // Show some details of the PICC (that is: the tag/card)
  64. Serial.print(F(": Card UID:"));
  65. dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
  66. Serial.println();
  67. Serial.print(F("PICC type: "));
  68. MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
  69. Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
  70. // Halt PICC
  71. mfrc522[reader].PICC_HaltA();
  72. // Stop encryption on PCD
  73. mfrc522[reader].PCD_StopCrypto1();
  74. } //if (mfrc522[reader].PICC_IsNewC
  75. } //for(uint8_t reader
  76. }
  77. /**
  78. * Helper routine to dump a byte array as hex values to Serial.
  79. */
  80. void dump_byte_array(byte *buffer, byte bufferSize) {
  81. for (byte i = 0; i < bufferSize; i++) {
  82. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  83. Serial.print(buffer[i], HEX);
  84. }
  85. }