RFID Door lock with Voice | RC522 | Arduino | DFPlayer
RFID is an acronym for Radio Frequency Identification and refers to a technology whereby digital data encoded in RFID tags or RFID cards (the one you use it in your Office or College ID Cards with RFID) that are captured by a reader via radio waves. RFID is similar to barcoding in that data from a tag or cards are captured by a device that stores the card ID in a database with additional details. The RFID Tag/Card has a unique ID and the reader reads that ID and it looks for the additional details like Employee Name, Blood Group, Position, Date of Birth,etc in its database. Note that, RFID only holds the unique ID only all other details are processed on the reader side only. Hence it is used in security access. When it is lost outside your premises, nobody can access your personal details without that back-end database.
In our blog, we will learn how to read the card ID and use it to grant or deny access along with a voice response.
The following blog explains how an RFID system is used to grant or deny access for a particular person from entering a certain place.
Normally RFID system uses a tag or electromagnetic card which has its own UID (Unique Identification). The Reader (two-way radio transmitter-receiver) sends a signal to the tag and read its response. In simple way it sends some digital pulse and reads back it, then check the received pulse and then compare both to identify its difference.
RFID – RC522 Description:
Input Voltage: 3.3v
Card Frequency:13.56 MHz
Pin Details:
VCC – This supplies power for the module. This can be anywhere from 2.5 to 3.3 volts. You can connect it to 3.3V output from your Arduino.
RST – This is an input for Reset and power-down. When this pin goes low, hard power-down is enabled.
GND – This is the Ground Pin.
IRQ – This is an interrupt pin that can alert the microcontroller when RFID tag comes into its vicinity.
MISO / SCL – This pin acts as Master-In-Slave-Out when the SPI interface is enabled, acts as a serial clock when the I2C interface is enabled and acts as serial data output when the UART interface is enabled.
MOSI(Master Out Slave In) – This is SPI input to the RC522 module.
SCK(Serial Clock) – This accepts clock pulses provided by the SPI bus Master.
SS / SDA – This pin acts as Signal input when the SPI interface is enabled, acts as serial data when the I2C interface is enabled and acts as serial data input when the UART interface is enabled.
Pin Configuration (Arduino UNO Board):
Pin | Connected to Pin in Arduino Board |
SDA | Digital 10 |
SCK | Digital 13 |
MOSI | Digital 11 |
MISO | Digital 12 |
IRQ | No Connection |
GND | GND |
RST | Digital 9 |
VCC | 3.3 V |
MFRC522 Library:
The MFRC522 and SPI libraries are inbuilt libraries that can be installed and included using Library Manager.
Header Files used in the Library:
SPI – Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances.
MFRC522 – This is the main library used to initialize and control the RFID – RC522.
Download link: https://github.com/miguelbalboa/rfid
DFPlayer Mini by DFRobot – DFPlayer Mini (or) MP3-TF-16P Module is a tiny low-cost mp3 module with an inbuilt Amplifier. Use the older Version 1.0.0, other versions are not working properly with some modules like MP3-TF-16P.
Download link: https://github.com/DFRobot/DFRobotDFPlayerMini
Components Used:
- Arduino UNO.
- RFID – MFRC522.
- LED – Red.
- LED – Green
- Servo Motors (For the cost effective doorlock demonstration).
- Relay (used for controlling doorlock with external power supply)
- Male To Female Jumper Cables.
- Male To Male Jumper Cables.
- DFPlayer Mini
- Speaker
Audio Files:
If you are using Chrome, right click and open the file in a new tab and press the three dot icon and choose ‘Download’ option. The file will be downloaded. For example, I used the Jarvis voice from Iron Man. You can use any other voices also. Search for Text to Speech Converters in Google and you will find a lot of tools.
001 – A Jarvis Initialization Voice at startup
005 – Access Granted Voice
003 – Access Denied Voice
002 – Face ID Accepted Voice (Optional)
You need to copy these files in the SD Card. In the code, you need to mention the filename. Note that the mp3 will play only if the filename starting with 001 or 002. Otherwise, it will choose the first copied MP3.
Circuit Diagram:
Pre – Requisites:
- Connect the circuit as per the circuit diagram. On the RX Pin of MP3 Module, we use a 1K Ohm resistor. Because the TX and RX Pins are 3.3V Logic (Arduino Uno pins are 5V level) and the resistor will drop some current & voltage to avoid crackling sounds on the speaker.
- The module accepts only MP3 Files. To convert your files to MP3 before using it. You can use many online converters for this.
- The naming should be 001, 002, 003 and so on. Else the player will pick the first file copied into the SD Card.
- Copy the files to the SD Card.
Code to Read RFID Cards/Tags ID:
Use this code to read the UID Cards/Tags unique ID and display it in Serial Monitor. This code will display it in Hexa-decimal and Decimal Values. Note down the HEX ID and we need to update it in the Access Granted if condition on the next code.
//Code to Read Tags and Display it in Serial Monitor #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; // Init array that will store new NUID byte nuidPICC[4]; void setup() { Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 for (byte i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; } Serial.println(F("This code scan the MIFARE Classsic NUID.")); Serial.print(F("Using the following key:")); printHex(key.keyByte, MFRC522::MF_KEY_SIZE); } void loop() { // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. if ( ! rfid.PICC_IsNewCardPresent()) return; // Verify if the NUID has been readed if ( ! rfid.PICC_ReadCardSerial()) return; Serial.print(F("PICC type: ")); MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); Serial.println(rfid.PICC_GetTypeName(piccType)); // Check is the PICC of Classic MIFARE type if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { Serial.println(F("Your tag is not of type MIFARE Classic.")); return; } if (rfid.uid.uidByte[0] != nuidPICC[0] || rfid.uid.uidByte[1] != nuidPICC[1] || rfid.uid.uidByte[2] != nuidPICC[2] || rfid.uid.uidByte[3] != nuidPICC[3] ) { Serial.println(F("A new card has been detected.")); // Store NUID into nuidPICC array for (byte i = 0; i < 4; i++) { nuidPICC[i] = rfid.uid.uidByte[i]; } Serial.println(F("The NUID tag is:")); Serial.print(F("In hex: ")); printHex(rfid.uid.uidByte, rfid.uid.size); Serial.println(); Serial.print(F("In dec: ")); printDec(rfid.uid.uidByte, rfid.uid.size); Serial.println(); } else Serial.println(F("Card read previously.")); // Halt PICC rfid.PICC_HaltA(); // Stop encryption on PCD rfid.PCD_StopCrypto1(); } /** * Helper routine to dump a byte array as hex values to Serial. */ void printHex(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } } /** * Helper routine to dump a byte array as dec values to Serial. */ void printDec(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], DEC); } }
RFID Doorlock with Voice response Complete Code:
To use servo motor in the code uncomment all servo motor library and my servo instances.
#include <Arduino.h> #include <DFRobotDFPlayerMini.h>//library for DFplayer #include <SoftwareSerial.h> #include <MFRC522.h> #include <SPI.h> //#include <Servo.h> #define SDA_Pin 10//Define SDA Pin in digital pin 10 #define RST_Pin 9//Define Reset Pin in digital pin 9 #define LED_R 4 //Define Red Led in digital pin 4 #define LED_G 5 //Define Green Led in digital pin 5 #define Relay 2 //Define relay in digital pin 2 #define ACCESS 2000 //Define delay for GRANTING Access #define DENIED 2000 //Define delay for Denying Acces //#define servopin 3 //Define servo pin in digital pin 5 to access the door SoftwareSerial mySoftwareSerial(6, 7); // RX, TX Arduino MFRC522 MFRC(SDA_Pin , RST_Pin); //Create Instance for MFRC522 DFRobotDFPlayerMini myDFplayer; //Create DFPlayer Instance/Object //Servo myservo; //Create object to control the Servo void setup() { mySoftwareSerial.begin(9600); //DFPlayer Initialization Serial.begin(115200);//Initialize Serial Communication SPI.begin();//Initialize SPI bus MFRC.PCD_Init();//Initialize MFRC522 pinMode(LED_R , OUTPUT); pinMode(LED_G , OUTPUT); digitalWrite(Relay, HIGH); //Keep the Door Locked Always //myservo.attach(servopin);//Attaches the servo pin to Servo object //myservo.write(0);//Closed Condition Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)")); if (!myDFplayer.begin(mySoftwareSerial)) { Serial.println(F("Unable to begin:")); Serial.println(F("1.Please recheck the connection!")); Serial.println(F("2.Please insert the SD card!")); while (true) //Incase DFPlayer Failed, it wont start at all. { delay(0); } } Serial.println(F("DFPlayer Mini online.")); myDFplayer.play(001); //Startup MP3 Serial.println("Insert Your Card......."); Serial.println(); } void loop() { if (! MFRC.PICC_IsNewCardPresent()) //Look For New Cards { return; } if (! MFRC.PICC_ReadCardSerial())//Select One Of the Cards { return; } //To Show the UID on Serial Monitor Serial.println("UID Tag:"); String content = ""; byte letter; for (byte i = 0; i < MFRC.uid.size; i++) { Serial.print(MFRC.uid.uidByte[i] < 0x10 ? "0" : " "); Serial.println(MFRC.uid.uidByte[i], HEX); content.concat(String(MFRC.uid.uidByte[i] < 0x10 ? " 0" : " ")); content.concat(String(MFRC.uid.uidByte[i], HEX)); } Serial.println(); content.toUpperCase(); if (content.substring(1) == "90 89 87 25")//Replace with Access Granted Card ID here { Serial.println("Access Granted"); Serial.println(); digitalWrite(Relay, LOW); //Relay ON Open Door digitalWrite(LED_G, HIGH); //Green LED ON delay(500); myDFplayer.play(005); //Access Granted MP3 File delay(3000); //myservo.write(90); delay(100); delay(ACCESS); digitalWrite(Relay, HIGH); //Relay OFF Lock the Door digitalWrite(LED_G, LOW); //myservo.write(0); } else { Serial.println(" Access denied"); digitalWrite(LED_R, HIGH); //Red LED ON myDFplayer.play(003); //Access Denied MP3 File delay(DENIED); digitalWrite(LED_R, LOW); } }
The above code reads the data read by the RFID-RC522 reader. If the given UID is registered, the Green LED glows notifying the access being granted and the Relay opens the door. Then the MP3-TF-16P module plays the sound to notify us. If the noted UID is not in the granted list the Red LED glows and notifying the access is denied and the MP3-TF-16P module plays the Access Denied MP3 File.
Comments (3)
Nice blog with all required info
Where to connect door prototype
You can connect an Solenoid Lock on the Relay output. Or you can use a Servo. The Servo Code is commented in the code.