====== NFC card reader with RC522 ======= Vypracoval: Hana Vrtělková ---- ===== Assignment ===== Develop a simple ISO/IEC 14443 NFC card reader. Use the NFC module based on RC522 IC and connect it to an Arduino Uno board. Create an application to read the card UID and send it to the Arduino serial port. Try to change the UID of an unlocked card. ---- ===== Introduction ===== Reason for creating this project was to test student's abilities to program AVR microprocessor using widely known components. ===== Hardware part ===== For this project is used RFID reader MFRC522 managed by Arduino Mega. The board arduino mega is communicating with the reader vai SPI. It is necassary to keep the pin wiring as it is shown on the figure under the following link, because the pins for SPI commmunication are defined and can not be changed. {{https://drive.google.com/file/d/11UIJthfn4M8OZr5yFPtAq-QpO4wPTcBj/view?usp=sharing}} The pin layout is also written in the following table. ^MFRC522 pins ^Mega pins ^ | 3.3V | 3.3V | | RST | D5 | | GND | GND | | RQ | Not connected | | MISO | MISO (D50) | | MOSI | MOSI (D51) | | SCK | SCK (D52) | | SDA | SS (D53) | ===== Software part ===== The application was created in Arduino API using SPI and MFRC522 libraries. ==== Initialization ==== Using implemented function Serial.begin(9600) to initialize serial communication with the PC with given baud rate 9600. SPI.begin() is function from SPI library which initialize the communication via SPI bus. Then the function PCD_Init which initialize the MFRC522 so we can transmit information. void setup() { // put your setup code here, to run once: Serial.begin(9600); // Initialize serial communications with the PC while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); } ==== Reading ==== Reading of the card's content is done using another function of the library MFRC522. Function PICC_DumpDetailsToSerial is used for writing basic information such as card uid, SAK and PICC type. Then depending on the PICC type the authorization is made via KeyA. MFRC522::MIFARE_Key key; mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); MFRC522::PICC_Type piccType; piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); switch (piccType) { case MFRC522::PICC_TYPE_MIFARE_MINI: case MFRC522::PICC_TYPE_MIFARE_1K: case MFRC522::PICC_TYPE_MIFARE_4K: // All keys are set to FFFFFFFFFFFFh at chip delivery from the factory. for (byte i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; } mfrc522.PICC_DumpMifareClassicToSerial(&(mfrc522.uid), piccType, &key); break; case MFRC522::PICC_TYPE_MIFARE_UL: mfrc522.PICC_DumpMifareUltralightToSerial(); break; case MFRC522::PICC_TYPE_ISO_14443_4: case MFRC522::PICC_TYPE_MIFARE_DESFIRE: case MFRC522::PICC_TYPE_ISO_18092: case MFRC522::PICC_TYPE_MIFARE_PLUS: case MFRC522::PICC_TYPE_TNP3XXX: Serial.println(F("Dumping memory contents not implemented for that PICC type.")); break; case MFRC522::PICC_TYPE_UNKNOWN: case MFRC522::PICC_TYPE_NOT_COMPLETE: default: break; // No memory dump here } ===== Principle ===== First of all it is necessary to initialize the communication vai SPI so we can send command from board to the RFID reader. It also includes defining the clock frequency so the communication can be synchronised. Than it is necessary to initialize the MFRC522 reader which is done by sending command to the adresses defined in hexadecimal values. For example to define RX and TX mode which defines reception/transmission data rate and framing, and you also turn on the antenna. When the reader functions the way it should we can use it to transmit commands to the card. ==== Communication principle of the card ==== First is request standard/all. After Power on resetthe card answers to a request REQA or wakeup WUPA command with the answer to the request code. In anticollision loop the identifier of the card is readed. But this part can be omitted. Select part. With this command the reader selects one card for authentification. The card returns Select AcKnowledgement (SAK) code which determines the type of selected card. Three pass authentification. After the selection the reader specifies the memory location of the following memory access and uses the coresponding key to three pass authentification procedure. After succesfull authenticication can be performed memory operations such as read block, write block. === Example of commands === ^Command ^ISO/IEC 14443 ^Command code (hexadecimal) ^ | Request | REQA | 26h (7 bit) | | Wake-up | WUPA | 52h (7bit) | | Select CL1 | Select CL1 | 93h 70h | | Authetification with key A | - | 60h | ===== Source code ===== Libraries not included (SPI library implemented in Arduino API, MFRC522 library can be downloaded on internet).\\ [[https://drive.google.com/open?id=1QvRs6nWOJhlTx936un_5Fq0XPvoUjxam]] ===== Summary ===== Initial idea was to write the application in pure C. Unfortunatelly inicialization of the communication vai SPI and initialization of the MFRC522 and right framing of the commands for authentification was quite complicated and time-consuming. The realization was made in Arduino API using build in library for SPI and downloaded library for MFRC522 which simplified the application.\\ Application can not write any information into the card. ===== Short video ===== {{https://youtu.be/fw8XuA8u60U}} ===== References ===== MFRC522 library:\\ [[https://www.arduinolibraries.info/libraries/mfrc522]] Data sheets:\\ [[https://www.nxp.com/docs/en/data-sheet/MF1S50YYX_V1.pdf?fbclid=IwAR1lF3yzJMDJ8S5kAtc6kM9QkILn4_KPIstq8xaJ7dioMutrpV0eTIXd9zI]]\\ [[https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf]] Images for scheme:\\ [[https://www.amazon.com/SmartProjects-Mega-Arduino-MEGA-Board/dp/B004A7H3DG]]\\ [[https://www.makerfabs.com/index.php?route=product/product&product_id=66]]