RFID door lock

Arduino Tutorial 30# How to make Arduino Based RFID (RC522) Door Lock

Posted Leave a commentPosted in Arduino

In this tutorial, we will see how to make arduino based RFID Door Lock. And you can see When I show wrong card or wrong keychain tag, it will make sound. When I show my college ID it will open the door. For closing the door, we have to again show college ID. So let’s get started.

For this you will need

  1. Arduino,
  2. RFID sensor (RC522),
  3. Servo motor,
  4. Piezo buzzer,
  5. Jumper wires,
  6. Breadboard (Optional).

Do connection as shown in diagram.

RFID door lock circuit diagram
RFID door lock circuit diagram

Before uploading our RFID door lock code. Add MFRC522 library (RFID library) in your computer. First of all, we have to find out unique identification number. For doing this

Go to file –> Examples –> MFRC522 –> READNUID

Upload the sketch. After uploading the sketch. Open serial monitor. Show the card which you want to use for Door lock. I am using my college Id card for door lock. Now we can see identification number. copy this number and come to our sketch of RFID door lock.

RFID door lock Sketch for 1 card access
#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>


#define SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code[] = {94,73,241,250}; //This is the stored UID
int codeRead = 0;
int Buzzer = 6;
int activate =0;
Servo myservo;
String uidString;
void setup() {
 
 Serial.begin(9600);
 SPI.begin(); // Init SPI bus
 rfid.PCD_Init(); // Init MFRC522 
 myservo.attach(5); //attach the servo to pin 5
 pinMode(Buzzer,OUTPUT); 
 myservo.write(0); // 0 degree angle for servo



}

void loop() {
 if( rfid.PICC_IsNewCardPresent())
 {
 readRFID();
 }
 delay(100);

}

void readRFID()
{
 
 rfid.PICC_ReadCardSerial();
 Serial.print(F("\nPICC 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;
 }


 Serial.println("Scanned PICC's UID:");
 printDec(rfid.uid.uidByte, rfid.uid.size);

uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);



 int i = 0;
 boolean match = true;
 while(i<rfid.uid.size)
 {
 if(!(rfid.uid.uidByte[i] == code[i]))
 {
 match = false;
 }
 i++;
 }

if(match)
 { if (activate==0){
 myservo.write(90); // 90 degree angle for servo (Door will open)
 Serial.println("\nDoor is open");
 delay(1000);
 activate = activate+1;
 }
 else
 { myservo.write(0); // 0 degree angle for servo (Door will close)
 activate = 0;
 Serial.println("\nDoor is close");
 delay(1000);
 }
 }
 
 else
 {
 tone(6,400,1000);
 Serial.println("\nUnknown Card");
 }


 // Halt PICC
 rfid.PICC_HaltA();

// Stop encryption on PCD
 rfid.PCD_StopCrypto1();
}

void printDec(byte *buffer, byte bufferSize) {
 for (byte i = 0; i < bufferSize; i++) {
 Serial.print(buffer[i] < 0x10 ? " 0" : " ");
 Serial.print(buffer[i], DEC);
 }
}

Identification number which we have copied earlier, paste here.

int code[] = {94,73,241,250}; //This is the stored UID

I have given 0 degree angle for door lock position. readRFID will read the card number. If card  number match with our card number door will open and for door opening 90 degree angle is given. Activate will become 1. You can change these angles according to your door lock opening and closing position. If we again show same card it will close the door. If card number does not match with our card number, buzzer will make sound for 1 second. You can see when I show unknown card or keychain tag it makes sound. When I show right card servo will rotate to 90 degree and when I again show same card servo will rotate back to 0 degree angle.

if you want to add more than one card for access. you can use this code

RFID door lock Sketch for 2 card access
#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>


#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code1[] = {94,73,241,250}; // access card 1
int code2[] = {230,112,166,172}; // access card 2
int codeRead = 0;
int Buzzer = 6;
int activate =0;
Servo myservo;
String uidString;
void setup() {

Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522 
myservo.attach(5); //attach the servo to pin 5
pinMode(Buzzer,OUTPUT); 
myservo.write(0); // 0 degree angle for servo

}

void loop() {
if( rfid.PICC_IsNewCardPresent())
{
readRFID();
}
delay(100);

}

void readRFID()
{

rfid.PICC_ReadCardSerial();
Serial.print(F("\nPICC 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;
}


Serial.println("Scanned PICC's UID:");
printDec(rfid.uid.uidByte, rfid.uid.size);

uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);

int i = 0;
boolean match = true;
while(i<rfid.uid.size)
{
if(!(rfid.uid.uidByte[i] == code1[i]) && !(rfid.uid.uidByte[i] == code2[i])) // add card here with "&&" and also change code number
{
match = false;
}
i++;
}


if(match)
{ if (activate==0){
myservo.write(90); // 90 degree angle for servo (Door will open)
Serial.println("\nDoor is open");
delay(1000);
activate = activate+1;
}
else
{ myservo.write(0); // 0 degree angle for servo (Door will close)
activate = 0;
Serial.println("\nDoor is close");
delay(1000);
}
}

else
{
tone(6,400,1000);
Serial.println("\nUnknown Card");
}


// Halt PICC
rfid.PICC_HaltA();

// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}

void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2DKDZyI (RFID RC522)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

http://amzn.to/2uKfEDf  (Piezo buzzer)

http://amzn.to/2weYm4R (servo motor)

KEEP LEARNING KEEP MAKING 🙂

Password lock on door using arduino

Arduino Tutorial #20 Password lock on door using arduino

Posted Leave a commentPosted in Arduino

In this tutorial we will see how to make door lock with password and when someone try wrong password buzzer will make sound.  So let’s get started. For this you will

  1. Arduino,
  2. Keypad,
  3. Servo motor,
  4. Buzzer,
  5. Breadboard (optional),
  6. Jumper wire,
  7. Steel wire to connect servomotor and sliding door lock.

 

Circuit diagram for password door lock using arduino
circuit diagram for password lock on door
circuit diagram for password lock on door

 

Do connection as shown in diagram.

Sketch for door lock password using arduino
#include <Servo.h>
#include <Keypad.h>

//Variables
int min_Ang = 0; 
int max_Ang = 135; 
int character = 0;
int activated =0;
char Pass_char[4] = {' ', ' ', ' ', ' '}; 
Servo myservo;
int buzzer=10;



//Keypad config
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
 {'1','4','7','*'},
 {'2','5','8','0'},
 {'3','6','9','#'},
 {'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
 myservo.attach(11); //attach the servo to pin 11
 pinMode(buzzer,OUTPUT); 
 myservo.write(min_Ang); 
 
}
 
void loop(){

char customKey = customKeypad.getKey(); //this function reads the presed key
 
 if (customKey){

if (character ==0)
 { 
 Pass_char[0]= customKey; 
 }

if (character ==1)
 { 
 Pass_char[1]= customKey; 
 }

if (character ==2)
 { 
 Pass_char[2]= customKey; 
 }

if (character ==3)
 { 
 Pass_char[3]= customKey; 
 activated=1;
 }
 
 character=character+1;
 }

if (activated == 1)
 {
// you can change your password here (current passowrd is 1357)

if(character==4 && Pass_char[0]=='1' && Pass_char[1]=='3' && Pass_char[2]=='5' && Pass_char[3]=='7' )
 {
 myservo.write(max_Ang);
 activated = 2;
 delay(1000);
 }
 else
 {

digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 activated = 0;
 character=0;
 Pass_char[0]= ' ';
 Pass_char[1]= ' '; 
 Pass_char[2]= ' '; 
 Pass_char[3]= ' ';
 }
 }
 if (activated == 2)
 {
 if(customKey == 'C' )
 {
 myservo.write(min_Ang);
 activated = 0;
 character=0;
 Pass_char[0]= ' ';
 Pass_char[1]= ' '; 
 Pass_char[2]= ' '; 
 Pass_char[3]= ' '; 
 }
 } 
}

 

Let’s come to the programming part. We are not using single wire connection for keypad that’s why in this sketch we are using keypad library. Which you can download by given link keypad library. After downloading unzip it. Copy that folder and paste it to directory of Arduino libraries. Now you are ready to go with keypad library.

Include keypad and servo library. Define integer minimum angle which is locked position at 0 degree angle.  Maximum angle is open position at 135 degree angle. You can change this angle according to your door open position. Initially character and activated is zero. Now we are defining character array which contain four element. Which is our password initially it is blank space. Create servo to myservo. Buzzer pin inserted at pin number 10. keypad matrix is taken from keypad library and you can adjust row & column pin number if it is not working correctly. In setup function Myservo is attached to pin number 11. Set pin mode of buzzer as an output. Myservo.write will rotate servo to zero degree angle. In loop function this function will read the pressed key. If any key is pressed it will check character is zero, if it is zero it will assign that pressed character to password character index number 0. Similarly for password character index number 1, 2, 3. It will increase character number on each keypress. When password character index number 3 is completed it will activate 1. If entered password is matched with exact number and sequence, my servo write will rotate the servo and open the door. In this case password is 1357. Now it will activate 2. If C character is pressed it will again lock the door and reset the activated and character equal to zero. It also reset entered password which is containing in form of array in password character. If password is not matched with our password, it will execute code written in else bracket. Buzzer will make sound. It will again reset all parameter activated and character is equal to zero.

You can see when I entered wrong password it makes sound and when I entered right password it rotate the servo 135 degree angle. It comes again to zero degree angle when I press C button.

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2wjd7p4 (Keypad 4×4)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

http://amzn.to/2uKfEDf  (Piezo buzzer)

http://amzn.to/2weYm4R (servo motor)

KEEP LEARNING KEEP MAKING 🙂