How to Make an Intelligent Access Control System?
1.Introduction
Nowadays, many residential districts use this function to open the door by swiping the card, which is very convenient. In this lesson, we will study the RFID-RC522 module that can open the door by swipe card.
2.Components Required
|
|
|
|
---|---|---|---|
Keyestudio Plus Mainboard*1 |
RFID-RC522 Module*1 |
Servo*1 |
Key Chain*1 |
|
|
|
|
F-F Dupont Wires |
USB Cable*1 |
Swipe Card*1 |
|
3.Component Knowledge
MF RC522 chip:
MF RC522 is a highly integrated card reader chip for 13.56MHz contactless communication. The MF RC522 utilizes advanced modulation and demodulation concepts to fully integrate all types of passive contactless communication methods and protocols at 13.56MHz. Supports 14443A compatible transponder signals. Digital section handles ISO14443A frames and error detection.
In addition, the fast CRYPTO1 encryption algorithm is also supported. MFRC522 supports higher speed contactless communication of MIFARE series with bi-directional data transfer rate up to 424kbit/s. As a new member of the 13.56MHz highly integrated read and write card series chip family, MF RC522 has a lot of similarities to MF RC500 and MF RC530, and also has many features and differences. It uses SPI mode to communicate with the host , which is conducive to reducing cable, shrinking the size of the PCB board, and lowering the costs.
RFID-RC522 module:
RF Identification (RFID) is a wireless communication technology that recognizes specific targets and reads and write related data over the radio signal. RFID-RC522 module adopts Philips MFRC522 original reader circuit chip design, easy to use, low cost, suitable for equipment development,development of advanced applications, the need for RF card terminal design or production. This module can be loaded directly into a variety of readers molds.
The module adopts a voltage of 5V, through the I2C interface and a few wires can be directly connected to any CPU mainboards or microcontrollers for communication, which can ensure the module stable and reliable work. The modules are easy to use, highly reliable and small in size, helping customers to easily and quickly apply contactless cards to their systems.
Specifications:
-
Operating voltage: DC 5V
-
Operating current: 13mA to 100mA/DC 5V
-
Idling current: 10-13mA/DC 5V
-
Sleep current: <80uA
-
Peak current: <100mA
-
Operating frequency: 13.56MHz
-
Maximum power: 0.5W
-
Supported card types: mifare1 S50、mifare1 S70、mifare UltraLight、mifare Pro、mifare Desfire
-
Environmental operating temperature: -20 to 80 degrees Celsius
-
Environment storage temperature: -40 to 85 degrees Celsius
-
Relative Humidity: 5% to 95%
-
Data transfer rate: The maximum is 10Mbit/s.
4.Read the Card Number Value
We start by reading the card number values of the swipe card and key chain using a simple code , and print them in the serial monitor. The wiring diagram is as follows.
Note: The library files are required in the code. If you have already added the library files such as “MFRC522_I2C” and “Wire”, you can ignore the process of adding library files.
Decompress the library files in the folder, that is, put the decompressed “MFRC522_I2C” folder and “Wire” folder into “Arduino libraries” under the compiler installation directory.
After successful placement, you need to restart the compiler, otherwise the compilation will not work.
e.g:C: \Program Files\Arduino\libraries
/*
Read_the_door_card_number_value
http//www.keyestudio.com
*/
#include
#include "MFRC522_I2C.h"
// 0x28 is the i2c address of SDA, if doesn't match,please check your address with i2c.
MFRC522 mfrc522(0x28); // create MFRC522.
void setup() {
Serial.begin(115200); // initialize and PC's serial communication
Wire.begin(); // initialize I2C
mfrc522.PCD_Init(); // initialize MFRC522
ShowReaderDetails(); // dispaly PCD - MFRC522 read carder
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
}
void loop() {
//
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
}
// select one of door cards. UID and SAK are mfrc522.uid.
// save UID
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
}
void ShowReaderDetails() {
// attain the MFRC522 software
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// when returning to 0x00 or 0xFF, may fail to transmit communication signals
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
}
}
Upload the code to the Plus mainboard, connect the wires and power on first. Then open the serial monitor, set the baud rate to 115200. The card number values of the swipe card and key chain can be read separately by bringing the door card and key chain near the sensing area of the module. As shown in the figure below.
5.Circuit Diagram and Wiring diagram of RFID-RC522 Controlling Servo
Now we use a RFID-RC522 module, swipe card and servo to simulate an intelligent access control system. When the swipe card is close to the RFID-RC522 module induction area, the servo rotates. Wiring according to the figure below.
6.Code
Note: The library files are required in the code. If you have already added the library files such as “MFRC522_I2C”, “Wire” and “Servo”, you can ignore the process of adding library files.
Decompress the library files in the folder, that is, put the decompressed “MFRC522_I2C” folder, “Wire” folder and “Servo” folder into “Arduino libraries” under the compiler installation directory.
After successful placement, you need to restart the compiler, otherwise the compilation will not work.
e.g:C: \Program Files\Arduino\libraries
Note: For different RFID-RC522 swipe cards, the values may be different. You can replace the corresponding swipe card values in the code program based on your own swipe card values, otherwise upload the code to the Plus Mainboard, which may cause RFID-RC522 module and swipe card fail to control the servo.
For example: You can use values which read from your own swipe cards to replace values in code
/*
Intelligent_access_control_system
http//www.keyestudio.com
*/
#include
#include "MFRC522_I2C.h"
#include
#define SERVO_PIN 9
#define USER_NUM 4
byte servoRun = 0;
byte userCard[USER_NUM][4] = { // card storage array
{28, 184, 119, 33},
{147, 173, 247, 32},
{57, 182, 70, 194}
};
// 0x28 is i2c address on SDA. Check your address with i2cscanner if not match.
// MFRC522 mfrc522(0x28, RST_PIN); // Create MFRC522 instance.
MFRC522 mfrc522(0x28); // Create MFRC522 instance.
void setup() {
Serial.begin(115200); // Initialize serial communications with the PC
Wire.begin(); // Initialize I2C
mfrc522.PCD_Init(); // Initialize MFRC522
pinMode(SERVO_PIN, OUTPUT); // servo controld the port
}
void loop() {
if(servoRun == 1){ // If the servo running variable is 1, it will run.
servoControl(); // the function to ocntrol the servo
}
if ( ! mfrc522.PICC_IsNewCardPresent()) return; // find cards
if ( ! mfrc522.PICC_ReadCardSerial()) return; // Verify if the NUID is readable
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && // check MIFARE type
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println("Can identify this card!");
return;
}
byte i;
for (i=0; i<USER_NUM; i++){ //
byte i2;
for(i2=0; i2<4; i2++){ //
if(mfrc522.uid.uidByte[i2] != userCard[i][i2]) break; // when it is break,this card is not verified
}
if(i2 == 4){ // when i2 is 4,which indicates that the four-digit value of this card has been verified.
Serial.println("Find an accessful card."); // output the successful information
servoRun = 1; // set the variable of opening the door to 1
break; // find the pass card, exit the loop.
}
}
if(i==USER_NUM){ // If i is equal to the number of users, the upper loop does not find the pass card.
Serial.print("Find a unknown card, its uid:");
for(i=0; i<4; i++){ // output UID
Serial.print(mfrc522.uid.uidByte[i], DEC);
Serial.print(" ");
}
Serial.println();
}
mfrc522.PICC_HaltA(); // Make the IC card placed in the card reading area enter the dormant state, no longer repeat the card reading
mfrc522.PCD_StopCrypto1(); // stop to code the card-reading module
}
void servoControl(){ // servo controls functions
Serial.println("Servo run!"); // output sign to output the function
servoPulse(0); // servo rotate to 0°
delay(500); // wait the servo to rotate
servoPulse(180); // servo rotate to 180°
servoRun = 0; // set the variable of running the servo to 0
}
void servoPulse(int myangle) //Define an impulse function
{
int pulseWidth=(myangle*11)+500;// convert the angle to the pulse width of 500-2480
digitalWrite(SERVO_PIN,HIGH); // set the port of the servo to high
delayMicroseconds(pulseWidth); // delay the time of the pulse width
digitalWrite(SERVO_PIN,LOW); // set the port of the servo to low
delay(20-pulseWidth/1000);
}
7.Result
Upload the code to the Plus Mainboard, connect the wires and power on first. Then open the serial monitor, after bringing the swipe card close to the RFID-RC522 module induction area 2 or 3 times, the servo turns once. At the same time, “Find an accessful card. Servo run!” appears 2 or 3 times on the serial monitor.