ReadNUID.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example sketch/program showing how to read new NUID from a 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 the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
  8. * Reader on the Arduino SPI interface.
  9. *
  10. * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
  11. * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
  12. * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
  13. * will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
  14. * when removing the PICC from reading distance too early.
  15. *
  16. * @license Released into the public domain.
  17. *
  18. * Typical pin layout used:
  19. * -----------------------------------------------------------------------------------------
  20. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  21. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  22. * Signal Pin Pin Pin Pin Pin Pin
  23. * -----------------------------------------------------------------------------------------
  24. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  25. * SPI SS SDA(SS) 10 53 D10 10 10
  26. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  27. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  28. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  29. *
  30. * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
  31. */
  32. #include <SPI.h>
  33. #include <MFRC522.h>
  34. #define SS_PIN 10
  35. #define RST_PIN 9
  36. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
  37. // Init array that will store new NUID
  38. byte nuidPICC[4];
  39. void setup() {
  40. Serial.begin(9600);
  41. SPI.begin(); // Init SPI bus
  42. rfid.PCD_Init(); // Init MFRC522
  43. Serial.println(F("This code scan the MIFARE Classsic NUID."));
  44. }
  45. void loop() {
  46. // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  47. if ( ! rfid.PICC_IsNewCardPresent())
  48. return;
  49. // Verify if the NUID has been readed
  50. if ( ! rfid.PICC_ReadCardSerial())
  51. return;
  52. Serial.print(F("PICC type: "));
  53. MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  54. Serial.println(rfid.PICC_GetTypeName(piccType));
  55. // Check is the PICC of Classic MIFARE type
  56. if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  57. piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  58. piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  59. Serial.println(F("Your tag is not of type MIFARE Classic."));
  60. return;
  61. }
  62. if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  63. rfid.uid.uidByte[1] != nuidPICC[1] ||
  64. rfid.uid.uidByte[2] != nuidPICC[2] ||
  65. rfid.uid.uidByte[3] != nuidPICC[3] ) {
  66. Serial.println(F("A new card has been detected."));
  67. // Store NUID into nuidPICC array
  68. for (byte i = 0; i < 4; i++) {
  69. nuidPICC[i] = rfid.uid.uidByte[i];
  70. }
  71. Serial.println(F("The NUID tag is:"));
  72. Serial.print(F("In hex: "));
  73. printHex(rfid.uid.uidByte, rfid.uid.size);
  74. Serial.println();
  75. Serial.print(F("In dec: "));
  76. printDec(rfid.uid.uidByte, rfid.uid.size);
  77. Serial.println();
  78. }
  79. else Serial.println(F("Card read previously."));
  80. // Halt PICC
  81. rfid.PICC_HaltA();
  82. // Stop encryption on PCD
  83. rfid.PCD_StopCrypto1();
  84. }
  85. /**
  86. * Helper routine to dump a byte array as hex values to Serial.
  87. */
  88. void printHex(byte *buffer, byte bufferSize) {
  89. for (byte i = 0; i < bufferSize; i++) {
  90. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  91. Serial.print(buffer[i], HEX);
  92. }
  93. }
  94. /**
  95. * Helper routine to dump a byte array as dec values to Serial.
  96. */
  97. void printDec(byte *buffer, byte bufferSize) {
  98. for (byte i = 0; i < bufferSize; i++) {
  99. Serial.print(' ');
  100. Serial.print(buffer[i], DEC);
  101. }
  102. }