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.

If you buy something from a this link, myelectricsparks Media may earn a commission. See our Read More.

how to write data to rfid card using rc522 rfid

In this article for better understanding, RC522 RFID Module is used with Arduino (host controller) to write data on RFID Module Card. We already had given detail about Arduino RC522 RFID Module-based Access Control System. Check that out for some important information such as RFID Communication, MFRC522 IC, RC522 RFID Module, etc.

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:

  1. RFID Transponder: Its function is to works as cards, tags, keys fob or a sticker, etc.
  2. RFID Transceiver: It’s function to reader /writing as it has the capability to read or write data to/ from RFID Transponder.
how to write data to rfid card using rc522 rfid

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:

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.

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:

1: MIFARE 1K,

2: MIFARE 4K,

3: MIFARE Mini

4: ISO / IEC 14443 protocol-based cards and tags.

how to write data to rfid card using rc522 rfid

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:

  • SPI
  • UART
  • I2C

NOTE:

We had written a detail article on Arduino RC522 RFID Module-based Access Control System For detail information checked it out.

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:

 0×92

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,

SectorsBlocks
Sector 0 = Blocks 0, 1, 2 and 3.
Sector 1 =Blocks 4, 5, 6 and 7.
Sector 2 =Blocks 7, 8, 9 ,10
Sector 3 =Blocks 11,12,13 ,14
Sector 4 =Blocks 15, 16,17, 18
Sector 5 =Blocks 19, 20, 21 ,22
Sector 6 =Blocks 23, 24 ,25, 26
Sector 7 =Blocks 27, 28 ,29 , 30
Sector 8 =Blocks 31, 32, 33,34
Sector 9 =Blocks 35 ,36 ,37 ,38
Sector 10 =Blocks 39, 40 ,41, 42
Sector 11 =Blocks 43, 44, 45, 46
Sector 12 =Blocks 47, 48 ,49 ,50
Sector 13 =Blocks 51, 52, 53, 54
Sector 14 =Blocks 55, 56, 57, 58
Sector 15 =Blocks 59, 60, 61 and 62

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;

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.

In shortcut, there is 16 Sector and in these sectors, there are 16 Trailers. Now Each Sector Trailer consists of the following data:

  • A mandatory 6 Byte Key A.
  • 4 Bytes for Access Bits.
  • Optional 6 Byte Key B (if not used, data can be stored).

NOTE:

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”.

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;

• UART

• SPI

• I²C

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.

RC522 RFID ModuleArduino UNO
VCC3.3V
RST7
GNDGND
IRQ—-
MISO12
MOSI11
SCK13
SS10

Components Required For Interfacing RFID using Arduino and RC522 RFID Module

The given below are the components needed;

  • RC522 RFID Reader / Writer Module.
  • Arduino UNO.
  • MIFARE 1K RFID Tag.
  • Connecting Wires.

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.