September 1st, 2019 by cedcraftscodes
In this tutorial, we will learn how to read and write data to a specific RFID block using MFRC522 and Arduino. If you are storing a person’s first name and surname, then this tutorial is what you are looking for. You can also read the data stored in ID by using the MFRC522 library.
MFRC522 RFID Module
Arduino
Jumper Wires
MFRC522 RFID Module
Pin | Wiring to Arduino Uno |
SDA | Digital 10 |
SCK | Digital 13 |
MOSI | Digital 11 |
MISO | Digital 12 |
IRQ | unconnected |
GND | GND |
RST | Digital 9 |
3.3V | 3.3V |
Modify the block number and byte buffr[] variable below. In my case, I want to write Devcraze.com (12 Characters) in block 4.
You may want to use ASCII Text to Hex converter.Â
Per an RFID block, it must have 16 bytes. So I appended four 0x20 (Space). (Devcraze.com____) and trim it later on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <SPI.h> #include <MFRC522.h> #define RST_PIN 9 // Configurable, see typical pin layout above #define SS_PIN 10 // Configurable, see typical pin layout above MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance MFRC522::MIFARE_Key key; MFRC522::StatusCode status; void setup() { Serial.begin(9600); // Initialize serial communications with the PC SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 card Serial.println(F("Write personal data on a MIFARE PICC ")); } void loop() { // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } if ( ! mfrc522.PICC_ReadCardSerial()) { return; } Serial.setTimeout(20000L); byte block = 4; byte buffr[] = {0x44,0x65,0x76,0x63, 0x72,0x61,0x7a,0x65, 0x2e,0x63,0x6f,0x6d, 0x20,0x20,0x20,0x20}; writeBytesToBlock(block, buffr); Serial.println(" "); mfrc522.PICC_HaltA(); mfrc522.PCD_StopCrypto1(); } void writeBytesToBlock(byte block, byte buff[]) { status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(F("PCD_Authenticate() failed: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } else Serial.println(F("PCD_Authenticate() success: ")); // Write block status = mfrc522.MIFARE_Write(block, buff, 16); if (status != MFRC522::STATUS_OK) { Serial.print(F("MIFARE_Write() failed: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } else Serial.println(F("MIFARE_Write() success: ")); } |
Upload the following Arduino code to read the data stored in block 4.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <SPI.h> #include <MFRC522.h> #define RST_PIN 9 // Configurable, see typical pin layout above #define SS_PIN 10 // Configurable, see typical pin layout above MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance MFRC522::MIFARE_Key key; MFRC522::StatusCode status; //*****************************************************************************************// void setup() { Serial.begin(9600); // Initialize serial communications with the PC SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 card Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read } //*****************************************************************************************// void loop() { // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; byte block; byte len; //------------------------------------------- // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } Serial.println(F("**Card Detected:**")); byte buffer1[18]; block = 4; len = 18; status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(F("Authentication failed: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } status = mfrc522.MIFARE_Read(block, buffer1, &len); if (status != MFRC522::STATUS_OK) { Serial.print(F("Reading failed: ")); Serial.println(mfrc522.GetStatusCodeName(status)); return; } String value = ""; for (uint8_t i = 0; i < 16; i++) { value += (char)buffer1[i]; } value.trim(); Serial.print(value); Serial.println(F("\n**End Reading**\n")); delay(1000); mfrc522.PICC_HaltA(); mfrc522.PCD_StopCrypto1(); } |
Open up your serial monitor and put your RFID tag near the MFRC522 RFID Module.
If you like this tutorial, don’t hesitate to click the social media buttons below 🙂
Comments