RFID-RC522

RFID-RC522

This page is to Setup and Config RFID-RC522

Hardware and Software

Hardware

RFID-RC522

Software

Arduino IDE

Sanki Notes

    • Download Driver (MFRC522) : https://github.com/miguelbalboa/rfid

Examples

This example is to read and write Card

Reference :

            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 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. */

            29. #include <SPI.h>

            30. #include <MFRC522.h>

            31. #define RST_PIN 9 // Configurable, see typical pin layout above

            32. #define SS_PIN 10 // Configurable, see typical pin layout above

            33. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

            34. MFRC522::MIFARE_Key key;

            35. /**

            36. * Initialize.

            37. */

            38. void setup() {

            39. Serial.begin(9600); // Initialize serial communications with the PC

            40. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)

            41. SPI.begin(); // Init SPI bus

            42. mfrc522.PCD_Init(); // Init MFRC522 card

            43. // Prepare the key (used both as key A and as key B)

            44. // using FFFFFFFFFFFFh which is the default at chip delivery from the factory

            45. for (byte i = 0; i < 6; i++) {

            46. key.keyByte[i] = 0xFF;

            47. }

            48. Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));

            49. Serial.print(F("Using key (for A and B):"));

            50. dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);

            51. Serial.println();

            52. Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));

            53. }

            54. /**

            55. * Main loop.

            56. */

            57. void loop() {

            58. // Look for new cards

            59. if ( ! mfrc522.PICC_IsNewCardPresent())

            60. return;

            61. // Select one of the cards

            62. if ( ! mfrc522.PICC_ReadCardSerial())

            63. return;

            64. // Show some details of the PICC (that is: the tag/card)

            65. Serial.print(F("Card UID:"));

            66. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);

            67. Serial.println();

            68. Serial.print(F("PICC type: "));

            69. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);

            70. Serial.println(mfrc522.PICC_GetTypeName(piccType));

            71. // Check for compatibility

            72. if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI

            73. && piccType != MFRC522::PICC_TYPE_MIFARE_1K

            74. && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {

            75. Serial.println(F("This sample only works with MIFARE Classic cards."));

            76. return;

            77. }

            78. // In this sample we use the second sector,

            79. // that is: sector #1, covering block #4 up to and including block #7

            80. byte sector = 1;

            81. byte blockAddr = 4;

            82. byte dataBlock[] = {

            83. 0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,

            84. 0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,

            85. 0x08, 0x09, 0xff, 0x0b, // 9, 10, 255, 12,

            86. 0x0c, 0x0d, 0x0e, 0x0f // 13, 14, 15, 16

            87. };

            88. byte trailerBlock = 7;

            89. MFRC522::StatusCode status;

            90. byte buffer[18];

            91. byte size = sizeof(buffer);

            92. // Authenticate using key A

            93. Serial.println(F("Authenticating using key A..."));

            94. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));

            95. if (status != MFRC522::STATUS_OK) {

            96. Serial.print(F("PCD_Authenticate() failed: "));

            97. Serial.println(mfrc522.GetStatusCodeName(status));

            98. return;

            99. }

            100. // Show the whole sector as it currently is

            101. Serial.println(F("Current data in sector:"));

            102. mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);

            103. Serial.println();

            104. // Read data from the block

            105. Serial.print(F("Reading data from block ")); Serial.print(blockAddr);

            106. Serial.println(F(" ..."));

            107. status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);

            108. if (status != MFRC522::STATUS_OK) {

            109. Serial.print(F("MIFARE_Read() failed: "));

            110. Serial.println(mfrc522.GetStatusCodeName(status));

            111. }

            112. Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));

            113. dump_byte_array(buffer, 16); Serial.println();

            114. Serial.println();

            115. // Authenticate using key B

            116. Serial.println(F("Authenticating again using key B..."));

            117. status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));

            118. if (status != MFRC522::STATUS_OK) {

            119. Serial.print(F("PCD_Authenticate() failed: "));

            120. Serial.println(mfrc522.GetStatusCodeName(status));

            121. return;

            122. }

            123. // Write data to the block

            124. Serial.print(F("Writing data into block ")); Serial.print(blockAddr);

            125. Serial.println(F(" ..."));

            126. dump_byte_array(dataBlock, 16); Serial.println();

            127. status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);

            128. if (status != MFRC522::STATUS_OK) {

            129. Serial.print(F("MIFARE_Write() failed: "));

            130. Serial.println(mfrc522.GetStatusCodeName(status));

            131. }

            132. Serial.println();

            133. // Read data from the block (again, should now be what we have written)

            134. Serial.print(F("Reading data from block ")); Serial.print(blockAddr);

            135. Serial.println(F(" ..."));

            136. status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);

            137. if (status != MFRC522::STATUS_OK) {

            138. Serial.print(F("MIFARE_Read() failed: "));

            139. Serial.println(mfrc522.GetStatusCodeName(status));

            140. }

            141. Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));

            142. dump_byte_array(buffer, 16); Serial.println();

            143. // Check that data in block is what we have written

            144. // by counting the number of bytes that are equal

            145. Serial.println(F("Checking result..."));

            146. byte count = 0;

            147. for (byte i = 0; i < 16; i++) {

            148. // Compare buffer (= what we've read) with dataBlock (= what we've written)

            149. if (buffer[i] == dataBlock[i])

            150. count++;

            151. }

            152. Serial.print(F("Number of bytes that match = ")); Serial.println(count);

            153. if (count == 16) {

            154. Serial.println(F("Success :-)"));

            155. } else {

            156. Serial.println(F("Failure, no match :-("));

            157. Serial.println(F(" perhaps the write didn't work properly..."));

            158. }

            159. Serial.println();

            160. // Dump the sector data

            161. Serial.println(F("Current data in sector:"));

            162. mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);

            163. Serial.println();

            164. // Halt PICC

            165. mfrc522.PICC_HaltA();

            166. // Stop encryption on PCD

            167. mfrc522.PCD_StopCrypto1();

            168. }

            169. /**

            170. * Helper routine to dump a byte array as hex values to Serial.

            171. */

            172. void dump_byte_array(byte *buffer, byte bufferSize) {

            173. for (byte i = 0; i < bufferSize; i++) {

            174. Serial.print(buffer[i] < 0x10 ? " 0" : " ");

            175. Serial.print(buffer[i], HEX);

            176. }

            177. }