ChangeUID.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example to change UID of changeable MIFARE card.
  4. * --------------------------------------------------------------------------------------------------------------------
  5. * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6. *
  7. * This sample shows how to set the UID on a UID changeable MIFARE card.
  8. *
  9. * @author Tom Clement
  10. * @license Released into the public domain.
  11. *
  12. * Typical pin layout used:
  13. * -----------------------------------------------------------------------------------------
  14. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  15. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  16. * Signal Pin Pin Pin Pin Pin Pin
  17. * -----------------------------------------------------------------------------------------
  18. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  19. * SPI SS SDA(SS) 10 53 D10 10 10
  20. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  21. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  22. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  23. *
  24. * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
  25. */
  26. #include <SPI.h>
  27. #include <MFRC522.h>
  28. #define RST_PIN 9 // Configurable, see typical pin layout above
  29. #define SS_PIN 10 // Configurable, see typical pin layout above
  30. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
  31. /* Set your new UID here! */
  32. #define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}
  33. void setup() {
  34. Serial.begin(9600); // Initialize serial communications with the PC
  35. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  36. SPI.begin(); // Init SPI bus
  37. mfrc522.PCD_Init(); // Init MFRC522 card
  38. Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));
  39. }
  40. // Setting the UID can be as simple as this:
  41. //void loop() {
  42. // byte newUid[] = NEW_UID;
  43. // if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
  44. // Serial.println("Wrote new UID to card.");
  45. // }
  46. // delay(1000);
  47. //}
  48. // But of course this is a more proper approach
  49. void loop() {
  50. // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. And if present, select one.
  51. if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
  52. delay(50);
  53. return;
  54. }
  55. // Now a card is selected. The UID and SAK is in mfrc522.uid.
  56. // Dump UID
  57. Serial.print(F("Card UID:"));
  58. for (byte i = 0; i < mfrc522.uid.size; i++) {
  59. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  60. Serial.print(mfrc522.uid.uidByte[i], HEX);
  61. }
  62. Serial.println();
  63. // Dump PICC type
  64. // MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  65. // Serial.print(F("PICC type: "));
  66. // Serial.print(mfrc522.PICC_GetTypeName(piccType));
  67. // Serial.print(F(" (SAK "));
  68. // Serial.print(mfrc522.uid.sak);
  69. // Serial.print(")\r\n");
  70. // if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
  71. // && piccType != MFRC522::PICC_TYPE_MIFARE_1K
  72. // && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  73. // Serial.println(F("This sample only works with MIFARE Classic cards."));
  74. // return;
  75. // }
  76. // Set new UID
  77. byte newUid[] = NEW_UID;
  78. if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
  79. Serial.println(F("Wrote new UID to card."));
  80. }
  81. // Halt PICC and re-select it so DumpToSerial doesn't get confused
  82. mfrc522.PICC_HaltA();
  83. if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
  84. return;
  85. }
  86. // Dump the new memory contents
  87. Serial.println(F("New UID and contents:"));
  88. mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  89. delay(2000);
  90. }