This Article is a learning guide for using RC522 RFID to write data into MIFARE 1K RFID Card. This device is used for storing custom data such as student information or employee details.
Introduction
This article is all about writing data to RFID cards using Arduino and RC522 RFID. Before moving forward at first read a little bit about “WHAT IS RFID”?
RFID is an abbreviation of Radio Frequency Identification. As it’s clear from the name that RFID uses Electromagnetic waves for transferring data or information. There are two main components of RFID that are:
[su_note note_color=”#b09907″]
- RFID Transponder: Its function is to works as cards, tags, keys fob or a sticker, etc.
- RFID Transceiver: It’s function to reader /writing as it has the capability to read or write data to/ from RFID Transponder.[/su_note]
RFID MODULE WORKING:
The RFID consists of an Antenna ???? for emitting EM waves with High Frequency. There is another Antenna ???? used by RFID Tags/Transponder and also contains IC for storing data.
Now when RFID Tag is received from high-frequency EM waves from the Another Antenna known as Reader Antenna then its Antenna charges the IC tag.
The IC then scans the stored information with a returning radio signal.
Now here another question arises that what is IC? For answer click for details.
What are RFID tags used for? Click for details.
APPLICATIONS:
[su_note note_color=”#b09907″]Mostly All new technologies are using RFID MODULE. The most used and brilliant technology that uses RFID Module is contact-less payment and automatic check-out system in supermarkets, banks, and office, goods tracking in warehouses also use it.[/su_note]
What is an RC522 RFID Module :
It’s a cheap RFID Reader Module that is based on MFRC522 RFID IC from NXP. It supports a large variety of RFID tags such as:
[su_note note_color=”#b09907″]
1: MIFARE 1K,
2: MIFARE 4K,
3: MIFARE Mini
4: ISO / IEC 14443 protocol-based cards and tags.[/su_note]
It works on 13.46 MHZ Frequency and the operating range is up to 50mm. It also supports three types of serial communication with Microcontrollers their names are mentioned below:
[su_note note_color=”#b09907″]
- SPI
- UART
- I2C[/su_note]
NOTE:
[su_note note_color=”#aeada6″]We had written a detail article on Arduino RC522 RFID Module-based Access Control System For detail information checked it out.[/su_note]
A Brief Look at Memory Map of MIFARE 1K Tag
It is very essential to briefly understand the Memory Map of MIFARE 1K Tag and the Importance of each memory location. After studying this with full attention you will be able to know where Memory is stored, where it’s reserved, and which places are free for storage purposes.
FOR EXAMPLE:
When the ‘Dumpinfo’ is uploaded and then for checking open the Serial Monitor. Now see its process, the Arduino scans all the information, and then it will separate all the content related to MIFARE 1K RFID Tag on the serial screen.
The given image shows the output from the ‘Dumpinfo’ file on the serial Monitor. Let us check it out.
Analyzing Serial Monitor Output
From the start, it’s clearly showing the version of MFRC522 IC. From the above image or screenshot the results are:
[su_note note_color=”#9c8a0b”] 0×92[/su_note]
In the above result, “9” is used for MFRC522 IC and “2” is used for 2.0 software versions. Now, UID, SAK, AND RFID Tags are available after RFID Card scanning.
In the given image, the UID is ‘6C 08 88 17’, SAK is ‘08’ and the card is MIFARE 1K.
Actually normal MIFARE 1K RFID Tag storage capacity is 1K Byte but it’s divided into 16 sectors ( 0 to 15) then the sector is divided into four blocks. Now from the above image, the storage method can be studied.
Understanding memory Map of MIFARE 1K Tag.
Before going forward keep in mind that these numbers of sectors are given for explanation purpose and for understanding memory layout.
So let’s get started,
[table id=12 responsive=”scroll”/]
[su_divider top=”no” size=”5″]
Every Block storage capacity is 16 Bytes data. So lets calculate it :
16 (Sectors) × 4 (Blocks) × 16 (Byte) = 1024 (Bytes) = 1K
RESERVATIONS OR USES OF SECTORS:
Usually, Block 0 of Sector 0 is used for storing processing Data. This Block consist of 4 Byte UID (Unique ID) and when it’s reserved for MIFARE 1K Tags or MIFARE 4K, MIFARE Mini tags from NXP and Advanced Tags. For example MIFARE Plus, MIFARE Ultralight, MIFARE DESFIRE. Their storage capacity is 7 Byte UID.
Now there is also a question what Sector Trailer in it? Click the given link.
The shortcut definition is;
[su_note note_color=”#9c8a0b”]In each Sector,3 Blocks are reserved for storing user’s data. But few blocks are specified such as Block 3 from Sector 0, Block 7 from Sector 1 are named Sector Trailer.[/su_note]
In shortcut, there is 16 Sector and in these sectors, there are 16 Trailers. Now Each Sector Trailer consists of the following data:
- [su_note note_color=”#9c8a0b”]
- A mandatory 6 Byte Key A.
- 4 Bytes for Access Bits.
- Optional 6 Byte Key B (if not used, data can be stored).[/su_note]
NOTE:
[su_note note_color=”#9c8a0b”]All Sector are equally divided into 3 data blocks and 1 Sector Trailer but Sector 0 is a little bit different as It’s used for data processing so it’s is divided into 2 data Blocks and 1 Sector trailer and also there is another 9 Byte space reserved for user’s data known as “Access Bits”.[/su_note]
Here a question arises that what is Access bit? For detailed answers check out the link.
Little-bite Definition of “Access bit ” given below:
The Access bit is added in Sector Trailer and it’s in charge of analyzing the conditions of all blocks in a Sector. For example for the specification of Access condition in 3 data block and sector trailer, 3-bite storage is needed. The Access condition Analyses Reading, Writing, Increment, Decrement, Transferring, and Restoring.
RESULTS:
Finally, after all the above discussions, it is clear that 47 bytes of data can be stored in MIFARE 1K RFI Data.
Now let’s move forward toward Writing data to RFID Tags using Arduino and RC522 RFID Module.
Interfacing RFID Using RC522 with Arduino
From the above explanation we know that MFRC522 IC controls 3 – types of Serial communication;
[su_note note_color=”#9c8a0b”]
• UART
• SPI
• I²C [/su_note]
IN all these three serial communications the most common and fast serial communication is the SPI interface.
The given image is the pinout of the RC522 RFID Module.
The hardware SPI pin is used for best communication between Arduino and RC522. The given tables show Arduino and RC522 Modules.
[table id=14 responsive=”scroll”/]
[su_divider top=”no” size=”5″]
Components Required For Interfacing RFID using Arduino and RC522 RFID Module
The given below are the components needed;
[su_note note_color=”#9c8a0b”]
- RC522 RFID Reader / Writer Module.
- Arduino UNO.
- MIFARE 1K RFID Tag.
- Connecting Wires.[/su_note]
Circuit Diagram
The given image clarifies the Arduino and RC522 RFID Module connections.
Write Data on RFID Card
While adding programs to blocks keep in mind that the data should be 16 bytes and also any simple program can be added.
Code
Given below is the Arduino code for writing detail MIFARE 1K RFID Tag. Copy it.
#include #include /*Using Hardware SPI of Arduino */ /*MOSI (11), MISO (12) and SCK (13) are fixed */ /*You can configure SS and RST Pins*/ #define SS_PIN 10 /* Slave Select Pin */ #define RST_PIN 7 /* Reset Pin */ /* Create an instance of MFRC522 */ MFRC522 mfrc522(SS_PIN, RST_PIN); /* Create an instance of MIFARE_Key */ MFRC522::MIFARE_Key key; /* Set the block to which we want to write data */ /* Be aware of Sector Trailer Blocks */ int blockNum = 2; /* Create an array of 16 Bytes and fill it with data */ /* This is the actual data which is going to be written into the card */ byte blockData [16] = {“Electronics-Hub-“}; /* Create another array to read data from Block */ /* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */ byte bufferLen = 18; byte readBlockData[18]; MFRC522::StatusCode status; void setup() { /* Initialize serial communications with the PC */ Serial.begin(9600); /* Initialize SPI bus */ SPI.begin(); /* Initialize MFRC522 Module */ mfrc522.PCD_Init(); Serial.println(“Scan a MIFARE 1K Tag to write data…”); } void loop() { /* Prepare the ksy for authentication */ /* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */ for (byte i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; } /* Look for new cards */ /* Reset the loop if no new card is present on RC522 Reader */ if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } /* Select one of the cards */ if ( ! mfrc522.PICC_ReadCardSerial()) { return; } Serial.print(“\n”); Serial.println(“**Card Detected**”); /* Print UID of the Card */ Serial.print(F(“Card UID:”)); for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? ” 0″ : ” “); Serial.print(mfrc522.uid.uidByte[i], HEX); } Serial.print(“\n”); /* Print type of card (for example, MIFARE 1K) */ Serial.print(F(“PICC type: “)); MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); Serial.println(mfrc522.PICC_GetTypeName(piccType)); /* Call ‘WriteDataToBlock’ function, which will write data to the block */ Serial.print(“\n”); Serial.println(“Writing to Data Block…”); WriteDataToBlock(blockNum, blockData); /* Read data from the same block */ Serial.print(“\n”); Serial.println(“Reading from Data Block…”); ReadDataFromBlock(blockNum, readBlockData); /* If you want to print the full memory dump, uncomment the next line */ //mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); /* Print the data read from block */ Serial.print(“\n”); Serial.print(“Data in Block:”); Serial.print(blockNum); Serial.print(” –> “); for (int j=0 ; j<16 ; j++) { Serial.write(readBlockData[j]); } Serial.print(“\n”); } void WriteDataToBlock(int blockNum, byte blockData[]) { /* Authenticating the desired data block for write access using Key A */ status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(“Authentication failed for Write: “); Serial.println(mfrc522.GetStatusCodeName(status)); return; } else { Serial.println(“Authentication success”); } /* Write data to the block */ status = mfrc522.MIFARE_Write(blockNum, blockData, 16); if (status != MFRC522::STATUS_OK) { Serial.print(“Writing to Block failed: “); Serial.println(mfrc522.GetStatusCodeName(status)); return; } else { Serial.println(“Data was written into Block successfully”); } } void ReadDataFromBlock(int blockNum, byte readBlockData[]) { /* Authenticating the desired data block for Read access using Key A */ byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(“Authentication failed for Read: “); Serial.println(mfrc522.GetStatusCodeName(status)); return; } else { Serial.println(“Authentication success”); } /* Reading data from the Block */ status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen); if (status != MFRC522::STATUS_OK) { Serial.print(“Reading failed: “); Serial.println(mfrc522.GetStatusCodeName(status)); return; } else { Serial.println(“Block was read successfully”); } }
Conclusion
This Article is all about Programing RC522 RFID Reader / Write Module and Arduino UNO to RFID Card for writing or Storing data. After reading this article you must know about the memory layout of MIFARE Classic 1K RFID Tags, memory locations, and an RFID Card.