ReadAndWrite.ino 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * ----------------------------------------------------------------------------
  3. * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
  4. * for further details and other examples.
  5. *
  6. * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
  7. *
  8. * Released into the public domain.
  9. * ----------------------------------------------------------------------------
  10. * This sample shows how to read and write data blocks on a MIFARE Classic PICC
  11. * (= card/tag).
  12. *
  13. * BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
  14. *
  15. *
  16. * Typical pin layout used:
  17. * -----------------------------------------------------------------------------------------
  18. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  19. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  20. * Signal Pin Pin Pin Pin Pin Pin
  21. * -----------------------------------------------------------------------------------------
  22. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  23. * SPI SS SDA(SS) 10 53 D10 10 10
  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_PIN 10 // Configurable, see typical pin layout above
  35. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  36. MFRC522::MIFARE_Key key;
  37. /**
  38. * Initialize.
  39. */
  40. void setup() {
  41. Serial.begin(9600); // Initialize serial communications with the PC
  42. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  43. SPI.begin(); // Init SPI bus
  44. mfrc522.PCD_Init(); // Init MFRC522 card
  45. // Prepare the key (used both as key A and as key B)
  46. // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
  47. for (byte i = 0; i < 6; i++) {
  48. key.keyByte[i] = 0xFF;
  49. }
  50. Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
  51. Serial.print(F("Using key (for A and B):"));
  52. dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
  53. Serial.println();
  54. Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
  55. }
  56. /**
  57. * Main loop.
  58. */
  59. void loop() {
  60. // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  61. if ( ! mfrc522.PICC_IsNewCardPresent())
  62. return;
  63. // Select one of the cards
  64. if ( ! mfrc522.PICC_ReadCardSerial())
  65. return;
  66. // Show some details of the PICC (that is: the tag/card)
  67. Serial.print(F("Card UID:"));
  68. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  69. Serial.println();
  70. Serial.print(F("PICC type: "));
  71. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  72. Serial.println(mfrc522.PICC_GetTypeName(piccType));
  73. // Check for compatibility
  74. if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
  75. && piccType != MFRC522::PICC_TYPE_MIFARE_1K
  76. && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  77. Serial.println(F("This sample only works with MIFARE Classic cards."));
  78. return;
  79. }
  80. // In this sample we use the second sector,
  81. // that is: sector #1, covering block #4 up to and including block #7
  82. byte sector = 1;
  83. byte blockAddr = 4;
  84. byte dataBlock[] = {
  85. 0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,
  86. 0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,
  87. 0x09, 0x0a, 0xff, 0x0b, // 9, 10, 255, 11,
  88. 0x0c, 0x0d, 0x0e, 0x0f // 12, 13, 14, 15
  89. };
  90. byte trailerBlock = 7;
  91. MFRC522::StatusCode status;
  92. byte buffer[18];
  93. byte size = sizeof(buffer);
  94. // Authenticate using key A
  95. Serial.println(F("Authenticating using key A..."));
  96. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  97. if (status != MFRC522::STATUS_OK) {
  98. Serial.print(F("PCD_Authenticate() failed: "));
  99. Serial.println(mfrc522.GetStatusCodeName(status));
  100. return;
  101. }
  102. // Show the whole sector as it currently is
  103. Serial.println(F("Current data in sector:"));
  104. mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
  105. Serial.println();
  106. // Read data from the block
  107. Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
  108. Serial.println(F(" ..."));
  109. status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
  110. if (status != MFRC522::STATUS_OK) {
  111. Serial.print(F("MIFARE_Read() failed: "));
  112. Serial.println(mfrc522.GetStatusCodeName(status));
  113. }
  114. Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
  115. dump_byte_array(buffer, 16); Serial.println();
  116. Serial.println();
  117. // Authenticate using key B
  118. Serial.println(F("Authenticating again using key B..."));
  119. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
  120. if (status != MFRC522::STATUS_OK) {
  121. Serial.print(F("PCD_Authenticate() failed: "));
  122. Serial.println(mfrc522.GetStatusCodeName(status));
  123. return;
  124. }
  125. // Write data to the block
  126. Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
  127. Serial.println(F(" ..."));
  128. dump_byte_array(dataBlock, 16); Serial.println();
  129. status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
  130. if (status != MFRC522::STATUS_OK) {
  131. Serial.print(F("MIFARE_Write() failed: "));
  132. Serial.println(mfrc522.GetStatusCodeName(status));
  133. }
  134. Serial.println();
  135. // Read data from the block (again, should now be what we have written)
  136. Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
  137. Serial.println(F(" ..."));
  138. status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
  139. if (status != MFRC522::STATUS_OK) {
  140. Serial.print(F("MIFARE_Read() failed: "));
  141. Serial.println(mfrc522.GetStatusCodeName(status));
  142. }
  143. Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
  144. dump_byte_array(buffer, 16); Serial.println();
  145. // Check that data in block is what we have written
  146. // by counting the number of bytes that are equal
  147. Serial.println(F("Checking result..."));
  148. byte count = 0;
  149. for (byte i = 0; i < 16; i++) {
  150. // Compare buffer (= what we've read) with dataBlock (= what we've written)
  151. if (buffer[i] == dataBlock[i])
  152. count++;
  153. }
  154. Serial.print(F("Number of bytes that match = ")); Serial.println(count);
  155. if (count == 16) {
  156. Serial.println(F("Success :-)"));
  157. } else {
  158. Serial.println(F("Failure, no match :-("));
  159. Serial.println(F(" perhaps the write didn't work properly..."));
  160. }
  161. Serial.println();
  162. // Dump the sector data
  163. Serial.println(F("Current data in sector:"));
  164. mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
  165. Serial.println();
  166. // Halt PICC
  167. mfrc522.PICC_HaltA();
  168. // Stop encryption on PCD
  169. mfrc522.PCD_StopCrypto1();
  170. }
  171. /**
  172. * Helper routine to dump a byte array as hex values to Serial.
  173. */
  174. void dump_byte_array(byte *buffer, byte bufferSize) {
  175. for (byte i = 0; i < bufferSize; i++) {
  176. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  177. Serial.print(buffer[i], HEX);
  178. }
  179. }