Rfid door lock


 Arduino code:

/*

 code started

RFID DOOR LOCK

#define LOCK = D3;

#define BUZZER = D2;

#define LED_G = D4;

#define LED_R = D5;

*/

#include <Wire.h>

#include <SPI.h>

#include <MFRC522.h>

#include <LiquidCrystal_I2C.h> 

// Set the LCD address to 0x27 for a 16 x 2 line display

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define SS_PIN 10

#define RST_PIN 9

#define LED_G 4 //define green LED pin

#define LED_R 5 //define red LED

#define BUZZER 2 //buzzer pin


#define LOCK 3

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.


int button = 6;

void setup() 

{

  Serial.begin(9600);   // Initiate a serial communication

  SPI.begin();      // Initiate  SPI bus

  mfrc522.PCD_Init();   // Initiate MFRC522

  

 pinMode(LOCK, OUTPUT);

  pinMode(LED_G, OUTPUT);

  pinMode(LED_R, OUTPUT);

  pinMode(BUZZER, OUTPUT);

  noTone(BUZZER);

  pinMode(button,INPUT);

  Serial.println("Place your card on reader...");

  Serial.println();

 // initialize the LCD

   lcd.init();                      // initialize the lcd 

  lcd.init();

  // Print a message to the LCD.

  lcd.backlight();

  lcd.setCursor(0,0); // column, row

  lcd.print("Place Your Card "); 

   lcd.setCursor(0,1); // column, row

  lcd.print("   Locked   ");

 }

void loop() 

{

  if (digitalRead(button) == HIGH)

   lcd.setCursor(0,1); // column, row

  lcd.print("   Locked   ");

  tone(BUZZER, 2000);

    delay(100);

    noTone(BUZZER);

 delay(50);

 digitalWrite(LOCK, HIGH);

 delay(2000);

digitalWrite(LOCK, LOW);

}

  // 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) == "B6 DB BC 2B" ) //change here the UID of card/cards or tag/tags that you want to give access

  {

    Serial.println("Access Granted");

    Serial.println();

    delay(500);

    digitalWrite(LED_G, HIGH);

    lcd.setCursor(0,1); // column, row

  lcd.print("   Un-Locked  ");

    tone(BUZZER, 2000);

    delay(100);

    noTone(BUZZER);

    delay(50);

    tone(BUZZER, 2000);

    delay(100);

    noTone(BUZZER);

    digitalWrite(LOCK, HIGH);

    delay(4000);

    digitalWrite(LOCK, LOW);

    delay(4000);

    digitalWrite(LED_G, LOW);

  }

else

{

  lcd.setCursor(0,1); // column, row

  lcd.print(" Wrong Card/Tag");

    Serial.println(" Access denied");

    digitalWrite(LED_R, HIGH);

    tone(BUZZER, 1500);

    delay(500);

    digitalWrite(LED_R, LOW);

    noTone(BUZZER);

    delay(100);

    digitalWrite(LED_R, HIGH);

    tone(BUZZER, 1500);

    delay(500);

    digitalWrite(LED_R, LOW);

    noTone(BUZZER);

    delay(100);

    digitalWrite(LED_R, HIGH);

    tone(BUZZER, 1500);

    delay(500);

    digitalWrite(LED_R, LOW);

    noTone(BUZZER);

     lcd.setCursor(0,1); // column, row

  lcd.print("   Locked   ");

 } 

 }


Post a Comment

0 Comments