Hello there guys. It’s your hardware friend Kayode. Hope you have been making and creating. Well if your answer is yes, am quite elated because you have been practicing. Today I want to introduce the concept of RFID.
What is RFID? RFID stands for Radio Frequency Identification. Transfer of data is done using electromagnetic fields over a short distance. RFID has become so popular in recent years. It is used in places like malls to identify goods, in offices and homes for access control, for identification of animals if they get lost and on vehicles for toll collection. They are even used in hospitals to tag disposable equipment in operating rooms to ensure that no foreign objects are left inside patients. An RFID system consists majorly of two components;
- RFID TAG: The tags come in all kinds of shapes and sizes and can be made so small that they can be injected into humans or even animals. They transmit a unique serial data to the reader using electromagnetic fields. Each tag has its own UID (Unique identification). RFID tags are of two types.
- PASSIVE TAGS: These tags have no battery and are powered by the electromagnetic field created by the reader. They contain a coil and an electronic microchip.
- ACTIVE TAGS: These tags have their own power source. This means that they are battery assisted They wait for an external source to wake them up and then use their own power source to transmit, giving a greater range.
- RFID READER: The radio is a transceiver. It sends a signal to the tag and also reads its response. A reader consists of an antenna coil, radio frequency module and a control unit. The antenna coil is responsible for generating a high-frequency electromagnetic field.
PRINCIPLE OF OPERATION OF RFID
As stated earlier, the reader consists of a control unit, radio frequency module and an antenna coil. The coil generates a high-frequency electromagnetic field. When the tag is brought close to the reader, voltage due to electromagnetic induction is generated at the coil of the tag. This voltage is used to power the microchip in the tag.
When the tag is powered, it can now retrieve data from the reader and can also send to it. The tag makes use of a method known as load manipulation. Turning on and off a load at the antenna of the tag will affect the rate at which power is consumed at the antenna of the reader. This can be measured as voltage drop. The changes in the voltage can then be captured as a series of ones and zeros. That’s how transmission takes place between the tag and reader.
MFRC22 RFID READER AND MIFARE TAG
For this project, we will make use of the MFRC522 RFID reader which uses SPI protocol to communicate with the Arduino. The reader has the following features;
- MFRC522 chip based board
- Operating frequency: 56MHz
- Supply voltage: 3.3V
- Current: 13-26mA
- Range: Approximately 3cm
- SPI interface
The RFID tags we will be using are based on the MIFARE protocol.
MFRC522 LIBRARY DOWNLOAD
We will need the above-mentioned library for our project. Follow the steps below;
- Download the MFRC522 library here
- Unzip the library
- Install it in the Arduino IDE
- Finally, restart the Arduino IDE
OBTAINING THE UNIQUE IDENTIFICATION OF THE TAGS
Setup the circuit as shown below.
The MFRC522 library comes preloaded with some examples. Go to File>Examples>MFRC522>Dumpinfo and upload the code. Open the serial monitor to obtain tag UID. Move the card close to the card until all information is displayed as shown below.
The card UID is indicated in green. Please write it down as we will use it later. The tag has 1KB of memory divided into 16 sectors (0-15) and each sector has 4 blocks. Each sector is protected by two keys, A and B.
COMPONENTS
We will need the following components in order to build our door access system
- Arduino Uno
- Breadboard
- MFRC522 RFID module
- Servo motor
CIRCUIT FOR DOOR ACCESS CONTROL SYSTEM
The wiring and circuit of the project is shown below;
READER PIN-OUT————————–TO ARDUINO
SDA ————————————————-10
SCK————————————————–13
MOSI————————————————11
MISO————————————————12
IRQ—————————————————Not connected
GND————————————————-GND
RST—————————————————-9
3.3V—————————————————3.3V
NOTE: Do not power the reader with 5V. You will fry the reader
The pulse pin of the servomotor is connected to digital pin 6, Vcc pin to 5V and GND to GND of Arduino.
OPERATION OF THE ACCESS CONTROL SYSTEM
For our project, we have two tags registered. If a registered tag is brought close to the RFID module, the card is read and accepted and the name of the user is also displayed on the serial monitor alongside the UID of the tag. The servo motor which acts as our lock opens the door for three seconds and then actuates back to its initial position.
CODE EXPLANATION
We will do a little breakdown of the code and I will leave you with the code at the end. First, we will include the libraries for RFID, SPI, and servo motor. We will also define and declare variables we need for the code and create instances of the libraries. We will also create two arrays to store the tag UID and username. I just felt like fooling around that’s why I used Eden Hazard’s name alongside my name. LOL…..
#include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. String cards[] ={ "E0 70 D1 18","E8 D7 0B 89"}; // change to your tag UID String users[] = {"Alade kayode","Eden Hazard"}; // change to your username #include <Servo.h> Servo myservo;
In the setup section, we start serial communication and also initiate SPI and MFRC522 communication. The pin to which the servo is attached is also specified.
void setup() { Serial.begin(9600); // Initiate a serial communication SPI.begin(); // Initiate SPI bus mfrc522.PCD_Init(); // Initiate MFRC522 Serial.println("Scan Your Tag..."); Serial.println(); myservo.attach(6); // attaches the servo on pin 6 to the servo object }
In the loop section, we first look for new tags. If a new tag is available, it is selected and then the UID of the tag is stored and then compared with the already registered tags. If they correspond, access is granted and the door is opened. If the tag does not correspond, access is denied.
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) == cards[0] ) //change here the UID of the tag that you want to give access { Serial.println("Access Granted"); Serial.print("User: "); Serial.println(users[0]); unlock(); lock(); } else if (content.substring(1) == cards[1] ) //change here the UID of the tag that you want to give access { Serial.println("Access Granted"); Serial.print("User: "); Serial.println(users[1]); unlock(); lock(); } else { Serial.println(" Access denied"); delay(1000); } }
This function is used to unlock the door for 3 seconds.
void unlock() { for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(3000); }
The last function in the code is used to lock the door
void lock() { for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
That’s all about the code. Find the full source code below.
CODE #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. String cards[] ={ "E0 70 D1 18","E8 D7 0B 89"};// change to your tag UID String users[] = {"Alade kayode","Eden Hazard"};// change to your username #include <Servo.h> Servo myservo; void setup() { Serial.begin(9600); // Initiate a serial communication SPI.begin(); // Initiate SPI bus mfrc522.PCD_Init(); // Initiate MFRC522 Serial.println("Scan Your Tag..."); Serial.println(); myservo.attach(6); // attaches the servo on pin 6 to the servo object } 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) == cards[0] ) { Serial.println("Access Granted"); Serial.print("User: "); Serial.println(users[0]); unlock(); lock(); } else if (content.substring(1) == cards[1] ) { Serial.println("Access Granted"); Serial.print("User: "); Serial.println(users[1]); unlock(); lock(); } else { Serial.println(" Access denied"); delay(1000); } } void unlock() { for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(3000); } void lock() { for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
If you do things right you should see something like the screenshot shown below
That will be all guys. Let me know if you have any questions. Till next time, keep creating.
Yours geekly,
Kayode.