Tutorial: How to use MFRC522 RFID module using Arduino

Introduction

I am sharing this tutorial so someone might benefited from it somehow.

I am fascinated with RFIDs capability, though this technology has been around many years before me. Now that I have this module, I tried to play with it.

The RFID tags can contain 1 kilo bytes of data, amazing right?

Circuit Diagram

Video Demonstration

Call To Action

If you have any question or suggestion, please write it in the comment box provided below.

If you find this article as helpful, please kindly consider supporting my journey in tinkering Electronics by Subscribing CLICK THIS LINK TO SUBSCRIBE.

Thank you and have a good days ahead.

Source Code

1. Read and Write to RFID tags:

  1 #include <SPI.h>  
  2 #include <MFRC522.h>  
  3 #define RST_PIN     9      // Configurable, see typical pin layout above  
  4 #define SS_PIN     10     // Configurable, see typical pin layout above  
  5 MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.  
  6 MFRC522::MIFARE_Key key;  
  7   // In this sample we use the second sector,  
  8   // that is: sector #1, covering block #4 up to and including block #7  
  9   byte sector     = 1;  
 10   byte blockAddr   = 4;  
 11   byte dataBlock[]  = {  
 12     0x01, 0x02, 0x03, 0x04,  
 13     0x05, 0x06, 0x07, 0x08,  
 14     0x09, 0x0a, 0xff, 0x0b,  
 15     0x0c, 0x0d, 0x0e, 0x0f  
 16   };  
 17 //  byte dataBlock[]  = {  
 18 //    0x00, 0x00, 0x00, 0x00,  
 19 //    0x00, 0x00, 0x00, 0x00,  
 20 //    0x00, 0x00, 0x00, 0x00,  
 21 //    0x00, 0x00, 0x00, 0x00   
 22 //  };  
 23   byte trailerBlock  = 7;  
 24   MFRC522::StatusCode status;  
 25   byte buffer[18];  
 26   byte size = sizeof(buffer);  
 27 /**  
 28  * Initialize.  
 29  */  
 30 void setup() {  
 31   Serial.begin(9600); // Initialize serial communications with the PC  
 32   while (!Serial);  // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)  
 33   SPI.begin();    // Init SPI bus  
 34   mfrc522.PCD_Init(); // Init MFRC522 card  
 35   // Prepare the key (used both as key A and as key B)  
 36   // using FFFFFFFFFFFFh which is the default at chip delivery from the factory  
 37   for (byte i = 0; i < 6; i++) {  
 38     key.keyByte[i] = 0xFF;  
 39   }  
 40 //  Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));  
 41 //  Serial.print(F("Using key (for A and B):"));  
 42 //  dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);  
 43 //  Serial.println();  
 44 //  
 45 //  Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));  
 46   Serial.println("Setup Done");  
 47 }  
 48 /**  
 49  * Main loop.  
 50  */  
 51 void loop() {  
 52   // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.  
 53   if ( ! mfrc522.PICC_IsNewCardPresent())  
 54     return;  
 55   // Select one of the cards  
 56   if ( ! mfrc522.PICC_ReadCardSerial())  
 57     return;  
 58   // Show some details of the PICC (that is: the tag/card)  
 59   Serial.print(F("Card UID:"));  
 60   dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);  
 61   Serial.println();  
 62   Serial.print(F("PICC type: "));  
 63   MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);  
 64   Serial.println(mfrc522.PICC_GetTypeName(piccType));  
 65   // Check for compatibility  
 66   if (  piccType != MFRC522::PICC_TYPE_MIFARE_MINI  
 67     && piccType != MFRC522::PICC_TYPE_MIFARE_1K  
 68     && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {  
 69     Serial.println(F("This sample only works with MIFARE Classic cards."));  
 70     return;  
 71   }  
 72   Serial.println("");  
 73   // Authenticate using key A  
 74   //Serial.println(F("Authenticating using key A..."));  
 75   status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));  
 76   if (status != MFRC522::STATUS_OK) {  
 77     Serial.print(F("PCD_Authenticate() failed: "));  
 78     Serial.println(mfrc522.GetStatusCodeName(status));  
 79     return;  
 80   }  
 81   // Read data from the block  
 82   Serial.print(F("Reading data from block ")); Serial.print(blockAddr);  
 83   Serial.println(F(" ..."));  
 84   status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);  
 85   if (status != MFRC522::STATUS_OK) {  
 86     Serial.print(F("MIFARE_Read() failed: "));  
 87     Serial.println(mfrc522.GetStatusCodeName(status));  
 88   }  
 89   Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));  
 90   dump_byte_array(buffer, 16); Serial.println();  
 91   Serial.println();  
 92   // Write data to the block  
 93   Serial.print(F("Writing data into block ")); Serial.print(blockAddr);  
 94   Serial.println(F(" ..."));  
 95   dump_byte_array(dataBlock, 16); Serial.println();  
 96   status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);  
 97   if (status != MFRC522::STATUS_OK) {  
 98     Serial.print(F("MIFARE_Write() failed: "));  
 99     Serial.println(mfrc522.GetStatusCodeName(status));  
100   }  
101   Serial.println();  
102   // Read data from the block (again, should now be what we have written)  
103   Serial.print(F("Reading data from block ")); Serial.print(blockAddr);  
104   Serial.println(F(" ..."));  
105   status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);  
106   if (status != MFRC522::STATUS_OK) {  
107     Serial.print(F("MIFARE_Read() failed: "));  
108     Serial.println(mfrc522.GetStatusCodeName(status));  
109   }  
110   Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));  
111   dump_byte_array(buffer, 16);   
112   Serial.println();  
113   // Halt PICC  
114   mfrc522.PICC_HaltA();  
115   // Stop encryption on PCD  
116   mfrc522.PCD_StopCrypto1();  
117 }  
118 /**  
119  * Helper routine to dump a byte array as hex values to Serial.  
120  */  
121 void dump_byte_array(byte *buffer, byte bufferSize) {  
122   for (byte i = 0; i < bufferSize; i++) {  
123     Serial.print(buffer[i] < 0x10 ? " 0" : " ");  
124     Serial.print(buffer[i], HEX);  
125   }  
126 }  

2. Sample Application: RFID Security Key:

 1 #include <SPI.h>  
 2 #include <MFRC522.h>  
 3 #define SS_PIN 10  
 4 #define RST_PIN 9  
 5 MFRC522 mfrc522(SS_PIN, RST_PIN);  
 6 void setup()   
 7 {  
 8  Serial.begin(9600);  
 9  SPI.begin();  
10  mfrc522.PCD_Init();  
11  Serial.println("Welcome, scan your card to enter");  
12  Serial.println();  
13 }  
14 void loop()   
15 {  
16  // Check for new RFID  
17  if ( ! mfrc522.PICC_IsNewCardPresent())   
18  {  
19   return;  
20  }  
21  // If the card use is not compatible,  
22  // do not continue  
23  if ( ! mfrc522.PICC_ReadCardSerial())   
24  {  
25   Serial.println("Access Denied");  
26   return;  
27  }  
28  // Process the RFID  
29  Serial.print("UID tag :");  
30  String content= "";  
31  byte letter;  
32  for (byte i = 0; i < mfrc522.uid.size; i++)   
33  {  
34    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");  
35    Serial.print(mfrc522.uid.uidByte[i], HEX);  
36    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));  
37    content.concat(String(mfrc522.uid.uidByte[i], HEX));  
38  }  
39  Serial.println();  
40  Serial.print("Message : ");  
41  content.toUpperCase();  
42  // Verify if the card is in the record  
43  if ( (content.substring(1) == "E4 6B 0B 2A") ||   
44     (content.substring(1) == "E7 45 8E 7A"))  {  
45   Serial.println(" Access granted.");  
46   Serial.println();  
47   delay(3000);  
48  } else {  
49   Serial.println(" Access denied");  
50   delay(3000);  
51  }  
52 }   


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.