RFID and Bluetooth Door Lock

Published

Introduction: RFID and Bluetooth Door Lock

Hi

This is my first Instructable and my first arduino project, I hope you will find it useful as well.

After looking through the projects on Instructables I could only find either a bluetooth door lock or a rfid door lock, not both so I decided to create one myself. Though the build is not totally complete I have tested it out properly and it does work without any problems.

I would first like to make clear that the 'Bluetooth app and the arduino code for reading bluetooth, and also the arduino code for reading the rfid' WAS NOT created by me. They have been sourced from other projects in Instructables. I would like to thank 'Taifur' for the Bluetooth app and code to go with it and apologise to the person who created the code for reading the RFID, as I have forgotten who it was.

16 April 2016 - New Arduino code and Fritzing diagram uploaded due to changes I have made to the originals.

Step 1: The Parts Needed

Parts required.

1 - Door Lock with Thumb Turn on the inside of the door

1 - Arduino Uno r3 (or compatible)

1 - 360 degree Servo

1 - HC-06 Bluetooth Module

1 - RC522 RFID Reader (Possbly Sintron, can not remember)

1 - 16 x 2 - 4 wire LDC screen

1 - RGB LED

2 - 220Ω Resistors

1 - Momentary Push Switch (to operate from the inside to lock/unlock the door)

1 - 1kΩ Resistor

2 - Double Back Boxes (1 approx 45mm depth and the other approx 33mm depth)

Jumper Wires

Power Supply ( I was going to use 2 x 9v batteries, but have decided to use mains powered 9v supply instead as more reliable)

Screws or strong double sided tape

1 - Servo Holder (I designed and printed my own)

** ----------------Waiting for delivery ---------------------------------------**

2 - Double Blanking Plates

1 - Length of 12 core alarm cable (length dependant on placement)

2 - 5amp Block Connectors (12 wires each)

** ---------------------------------------------------------------------------------**

Andriod Bluetooth Lock App. BTControl.apk

Step 2: The Wiring

Power:

9v Regulated Power Supply (or batteries)

RFID Reader:

VCC - Pin 3.3v

GND - GND

Reset (RST) - Pin D9

SDA(SS) - Pin D10

MOSI - Pin D11

MISO - Pin D12

SCK - Pin D13

LCD Screen:

VCC - 5v

GND - GND

SDA - Pin A4

SCL - Pin A5

Momentary Switch:

VCC - 5v (as shown above)

GND to 1kΩ resistor (as shown above)

Pin 8 arduino (as shown above)

RGB LED:

Pin D7 to 220Ω resistor then to LED pin (Red)

Pin D6 to 220Ω resistor then to LED pin (Green)

Common (the long wire) - GND

Servo:

VCC - 5v

GND - GND

Signal - Pin D5

Bluetooth Module:

VCC - 5v

GND -GND

TX - D0 (RX)

RX - D1 (TX)

Fritzing Diagram:

Step 3: Putting It All Together

The Arduino, Bluetooth Module and Momentary Switch + RGB Led circuit fits into the 45mm Back Box. I have just used Blu-Tac to hold them in place as they will not be moved once fixed to the wall. I have soldered the switch, led and resistors to a circuit board along with creating a GND and VCC connections on either side of the circuit board, a Block Connector will also be placed in for the 12 Core Alarm Cable to connect the RFID and LCD Screen to the arduino. The lid will get 2 holes drilled in it to accommodate the switch and led.

The RFID and LCD Screen fits within the 33mm Back Box with a slot cut out for the LCD Screen to fit through. These will the be wired to the arduino with the 12 Core Alarm Cable and Block Connector.

For the Servo to connect to the door lock I 3D printed a holder to be screwed to the door and attached to the door lock.

Step 4: The Code - (Updated on 23 June 2018)

/*
* ----------------------------------------------------------------------------
 * This sketch uses the MFRC522 library ; see  https://github.com/miguelbalboa/rfid

 * for further details and other examples.
 * 
 * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
 * 
 * This sketch show a simple locking mechanism using the RC522 RFID module.
 * ----------------------------------------------------------------------------
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 */
 
#include <SPI.h>
#include <Servo.h>
#include <MFRC522.h>
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


#define RST_PIN         9    // Configurable, see typical pin layout above
#define SS_PIN          10

#define Green_LED 6       //pin for green "door unlocked" indicator LED
#define Red_LED 7         //pin for red "door locked" indicator LED

char array1[]=" Swipe Your Tag";  //the string to print on the LCD
char array2[]=" On The Reader.";
char array3[]=" Tag Accepted. ";
char array4[]="  Locking.     ";
char array5[]=" Unlocking.    ";
int tim = 1;  //the value of delay time

// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line

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

String read_rfid;
String ok_rfid_1="94e68de2";
String ok_rfid_2="f5642a50";
String ok_rfid_3="4249622993c81";

int addr = 1;
int eVal = 0;
int lockPosition;
int servo_position;    
int BUTTON1 = 8;

void setup() {
  Serial.begin(9600);         // Initialize serial communications with the PC
  while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();                // Init SPI bus
  mfrc522.PCD_Init();         // Init MFRC522 card
  
  {
  lcd.init();  //initialize the lcd
  lcd.backlight();  //open the backlight 
  lcd.begin(16,2);
  }
    
  pinMode(Green_LED, OUTPUT);  //set all input/output pins, including whether they should be high (5v) or low (ground)
  pinMode(Red_LED, OUTPUT);
  digitalWrite(Green_LED, LOW);
  digitalWrite(Red_LED, HIGH);
  pinMode(BUTTON1,INPUT);

myservo.attach(5);

lockPosition = EEPROM.read(1);
//  Serial.print(EEPROM.read(1));
  if(lockPosition == 1){
    digitalWrite(Red_LED, LOW);
    digitalWrite(Green_LED, HIGH);
  }
  else {
    digitalWrite(Red_LED, HIGH);
    digitalWrite(Green_LED, LOW);       
    lockUnlock();
  }
    swipeText();

/*
 * Dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

void lockUnlock() { // locks or unlocks door

    myservo.attach(5); 
    if(lockPosition == 1) {
      lock2();
      servo_position = 0;
      myservo.write(servo_position);
      digitalWrite(Red_LED, HIGH);
      digitalWrite(Green_LED, LOW);      
      delay(3000);
      lockPosition = 2;
      eVal = lockPosition;      
      EEPROM.write(1,eVal);
//      Serial.print(eVal);
      
      } 
      else if(lockPosition == 2) {
        lock1();
        servo_position = 350;
        myservo.write(servo_position);     
        digitalWrite(Red_LED, LOW);
        digitalWrite(Green_LED, HIGH);        
        delay(3000);
        lockPosition = 1;
        eVal = lockPosition;      
        EEPROM.write(1,eVal);
//        Serial.print(eVal);  
      }  
      myservo.detach();   
}

void swipeText()
{
  lcd.clear();  //Clears the LCD screen and positions the cursor in the upper-left corner.
  lcd.setCursor(0,0);  // set the cursor to column 15, line 0
  for (int positionCounter1 = 0; positionCounter1 < 15; positionCounter1++)
  {
    lcd.print(array1[positionCounter1]);  // Print a message to the LCD.
    delay(tim);  //wait for 250 microseconds
  }
  lcd.setCursor(0,1);  // set the cursor to column 15, line 1
  for (int positionCounter = 0; positionCounter < 15; positionCounter++)
  {
    lcd.print(array2[positionCounter]);  // Print a message to the LCD.
    delay(tim);  //wait for 250 microseconds
  }  
}

void lock1()
{
  lcd.clear();
    lcd.setCursor(0,0);
  for (int positionCounter1 = 0; positionCounter1 < 15; positionCounter1++)
  {
    lcd.print(array3[positionCounter1]);
    delay(tim);
  }
  lcd.setCursor(0,1);
  for (int positionCounter1 = 0; positionCounter1 < 10; positionCounter1++)
  {
    lcd.print(array4[positionCounter1]);
    delay(tim);
  }  
}

void lock2()
{
  lcd.clear();
  lcd.setCursor(0,0);
  for (int positionCounter1 = 0; positionCounter1 < 15; positionCounter1++)
  {
    lcd.print(array3[positionCounter1]);
    delay(tim);
  }
  lcd.setCursor(0,1);
  for (int positionCounter1 = 0; positionCounter1 < 12; positionCounter1++)
  {
    lcd.print(array5[positionCounter1]);
    delay(tim);
  }  
}

void loop() {
    
    if(digitalRead(BUTTON1) == HIGH){
      lockUnlock();
      swipeText();
    }

// Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;</p><p>    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    
    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_1) {
      //ok, open the door.
      lockUnlock();
      swipeText();      
    }
    //Add below as many "keys" as you want
    if (read_rfid==ok_rfid_2) {
      //also ok, open the door
      lockUnlock(); 
      swipeText();           
    }
    //Add below as many "keys" as you want
    if (read_rfid==ok_rfid_3) {
      //also ok, open the door
      lockUnlock();
      swipeText();
    }               
}</p>

Share

    Recommendations

    • Oil Contest

      Oil Contest
    • Planter Challenge

      Planter Challenge
    • Clocks Contest

      Clocks Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    1 Questions

    Hello! I want to make you project to learn more and to present it at school. Please tell me what did you included in the code where you said 6 times : "#include".

    What did you included ?

    Thank you!

    Hi

    Sorry for the delay, the code has now been sorted out for you with the #includes now added.

    The problem occurred when I copied the code from Arduino IDE and I had not realised that it had not copied correctly.

    I hope you enjoy the project

    4 Comments

    Hi philipfigari

    As you can see from my diagram that I have not used a voltage divider on the HC-06 bluetooth module. I have had no problems wiring it this way, though a voltage divider can make the voltage more stable.

    Below is a diagram showing how to divide the voltage on the TX/RX line.

    Voltage divider.jpg