How to use the RFID Set with Arduino
How to use the RFID Set with Arduino
INTRODUCTION
Radio-frequency identification (RFID) uses electromagnetic fields to identify and track tags attached to objects automatically. An RFID tag consists of a tiny radio transponder, a radio receiver, and a transmitter. When triggered by an electromagnetic interrogation pulse from a nearby RFID reader device, the tag transmits digital data, usually an identifying inventory number, back to the reader.
APPLICATIONS
The RFID system can be used to build a door lock system, user authentication system and is also widely used on ATM and credit cards.
WIRING DIAGRAM
GND – GND
VCC - 5V
RST – Pin 9
MISO – Pin 12
MOSI – Pin 11
SCK – Pin 13
NSS – Pin 10
CODE
(Note: Download the required libraries.)
https://github.com/miguelbalboa/rfid
Note, you will have to use the code below to find the UID (Unique Identifier) of your card:
#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 void setup() { 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 delay(4); // Optional delay. Some board do need more time after init to be ready, see Readme mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); } void loop() { // 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; } // Dump debug info about the card; PICC_HaltA() is automatically called mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); }
Open the Serial Terminal and find the UID. It will show like: The number surrounding in the rectangular box is your UID. Note it and use it in the next code.
Main code:
#include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. void setup() { Serial.begin(9600); // Initiate a serial communication SPI.begin(); // Initiate SPI bus mfrc522.PCD_Init(); // Initiate MFRC522 Serial.println("Approximate your card to the reader..."); Serial.println(); } void loop() { // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } //Show UID on serial monitor Serial.print("UID tag :"); String content= ""; byte letter; for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte[i], HEX)); } Serial.println(); Serial.print("Message : "); content.toUpperCase(); if (content.substring(1) == "29 B9 ED 23") //change here the UID of the card/cards that you want to give access { Serial.println("Authorized access"); Serial.println(); delay(3000); } else { Serial.println(" Access denied"); delay(3000); } }
END RESULT
Open the serial terminal and observe that your card is allowed or not.
ADDITIONAL RESOURCES
https://electronicshobbyists.com/rfid-basics-and-rfid-module-interfacing-with-arduino/