If you are a novice to use the RFID MFRC522 module with Arduino, do not worry because this tool provides you with the proper and coherent code for the module.
Advertisement:
Read Later
If you are a novice to use the RFID MFRC522 module with Arduino, do not worry because this tool provides you with the proper and coherent code for the module.
Related Links :
MFRC522 Description
If you are a novice to use the RFID MFRC522 module with Arduino, do not worry because this tool provides you with the proper and coherent code for the module. Just enter the output pins you want to use on the Arduino board and copy the generated code: it is ready to be executed and already compiled.
The MFRC522 is a highly integrated reader/writer IC for contactless communication at 13.56 MHz. The MFRC522 reader supports ISO/IEC 14443 A/MIFARE and NTAG.
The MFRC522’s internal transmitter is able to drive a reader/writer antenna designed to communicate with ISO/IEC 14443 A/MIFARE cards and transponders without additional active circuitry. The receiver module provides a robust and efficient implementation for demodulating and decoding signals from ISO/IEC 14443 A/MIFARE compatible cards and transponders. The digital module manages the complete ISO/IEC 14443 A framing and error detection (parity and CRC) functionality.
The MFRC522 supports MF1xxS20, MF1xxS70 and MF1xxS50 products. The MFRC522 supports contactless communication and uses MIFARE higher transfer speeds up to 848 kBd in both directions.
Note: While registering a new UID to the Arduino's EEPROM, do not forget that it is limited.
Download the MFRC522 library from here.
Define the RST and SS (SDA) pins and connect MOSI, MISO, and SCK pins to Arduino according to the given pinout.
// Connections // Arduino: // MFRC522 // Pin {{ RST_PIN }} ----------------------- RST // Pin {{ SS_PIN }} ----------------------- SDA // Pin * ----------------------- MOSI // Pin * ----------------------- MISO // Pin * ----------------------- SCK /* Typical pin layout used: ----------------------------------------------------------------------------------------- MFRC522 Arduino Arduino Arduino Arduino Arduino Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro Signal Pin Pin Pin Pin Pin Pin ----------------------------------------------------------------------------------------- RST/Reset RST 9 5 D9 RESET/ICSP-5 RST SPI SS SDA(SS) 10 53 D10 10 10 SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 */ #include <EEPROM.h> // We are going to read and write PICC's UIDs from/to EEPROM #include <SPI.h> // RC522 Module uses SPI protocol #include <MFRC522.h> // Library for Mifare RC522 Devices // Create MFRC522 instance. #define SS_PIN {{ SS_PIN }} #define RST_PIN {{ RST_PIN }} MFRC522 mfrc522(SS_PIN, RST_PIN); // Define MFRC5222 module key input. MFRC522::MIFARE_Key key; // Define the process controller and readCard byte. int successUID; byte readCard[4]; // Stores scanned ID read from RFID Module // Define openUID and lastRead strings. String openUID; String lastRead; void setup() { //Protocol Configuration Serial.begin(9600); // Initialize serial communications with PC SPI.begin(); // MFRC522 Hardware uses SPI protocol mfrc522.PCD_Init(); // Initialize MFRC522 Hardware // If you do not register a new UID to EEPROM yet, reload the code after turning these lines into uncommented. // Save the new card or key tag UID to EEPROM. But do not forget it only has 1KB memory. // Serial.print("Approximate the new card or key tag to scan and register new UID."); // do{ // Wait for the new card reading process. // successUID = registerCardUID(); // }while(!successUID); // Get the open UID from EEPROM. getUIDfromEEPROM(); Serial.print("UID is received from EEPROM :\n----------------------------------\n"); Serial.print(openUID); } void loop(){ UID(); } int UID(){ // Get the last UID from MFRC522. if ( ! mfrc522.PICC_IsNewCardPresent()){ return; } if ( ! mfrc522.PICC_ReadCardSerial()){ return; } for(int i=0;i<mfrc522.uid.size;i++){ lastRead += mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "; lastRead += String(mfrc522.uid.uidByte[i], HEX); } // Arrange lastRead for comparing. lastRead.trim(); lastRead.toUpperCase(); Serial.println("New UID :\n----------------------------------\n"); Serial.println(lastRead); // Check whether it is a new card or not. if(lastRead == openUID){ Serial.println("STATUS: OK"); }else{ Serial.println("STATUS: NEW UID DETECTED!"); } } int registerCardUID() { // Detect the new card UID. if ( ! mfrc522.PICC_IsNewCardPresent()) { return 0; } if ( ! mfrc522.PICC_ReadCardSerial()) { return 0; } // Display the new UID. Serial.print("\n----------------------------------\nNew Card or Key Tag UID : "); for (int i = 0; i < mfrc522.uid.size; i++) { // readCard[i] = mfrc522.uid.uidByte[i]; Serial.print(readCard[i], HEX); } Serial.print("\n----------------------------------\n"); // Save the new UID to EEPROM. for ( int i = 0; i < mfrc522.uid.size; i++ ){ EEPROM.write(i, readCard[i] ); } Serial.print("UID is saved successfully to EEPROM.\nIf you want to save another card, use i+4(...) instead i."); // If the card reading process is successful, return 1 and end the reading process. mfrc522.PICC_HaltA(); return 1; } int getUIDfromEEPROM(){ // Get the open UID from EEPROM. for(int i=0;i<4;i++){ openUID += EEPROM.read(i) < 0x10 ? " 0" : " "; openUID += String(EEPROM.read(i), HEX); } // Arrange openUID for comparing. openUID.trim(); openUID.toUpperCase(); }
(1) https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf