Build a Solenoid Door Lock Using RFID in 5 Steps w/ an Arduino

Source and original idea from Circuit Digest.

Would you like to build another Arduino project? This post will help you build an Arduino solenoid door lock using RFID. You can use this project to improve your home security without shelling out a lot of money.

Understanding the RFID system

If you have been to a hotel, you must have noticed that the doors have a mechanism that reads the room card and quickly unlocks the doors. This is an RFID door lock system at work. In this project, you’ll use a solenoid door lock controlled by Arduino and RFID. A magnet and a Hall effect sensor are used to sense door movement.

The magnet will go on the door while the Hall Effect sensor will go on the door frame. When the Hall Effect sensor and the magnet are near, the Hall Effect sensor is in a low state and the door will remain shut, and when the sensor and the magnet grow apart from each other, the sensor is in a high state and the door can be opened.

Now let’s get into building Arduino solenoid door lock.

Step 1: Gather all the components

For this project, you will need:

RFID-RC522 module
Arduino Uno
Relay module
12V Solenoid lock
A Buzzer
Hall Effect sensor
10kΩ Resistor

Step 1.1: How does a Solenoid lock work?

Before we get into building the lock, let’s look at how it works. The lock has an electronic-mechanical system for locking.

This lock comes with a slug, a mounting bracket, and a slanted cut. When the power is on, DC generates a magnetic field that pushes the slug inside to keep the door in an unlocked position. The slug will move only when the power cuts out meaning the slug goes outside making the door locked. The slug doesn’t utilize any power when the door is locked. To make the Solenoid lock work, you will require a power source of at least 12V@500mA.

Step 2: Make the connections based on the circuit diagram

See the table below for complete connections. The GND pin of the buzzer connects to the GND pin of Arduino. The digital pin 4 of Arduino connects with the buzzer’s positive pin, and the GND pin connects to the Arduino GND pin. A relay module connects the Solenoid lock to the Arduino. Also, The OUT pin and the VCC pin of the Hall Effect sensor are connected with a 10K resistor.

Arduino UnoRFID
3.3V3.3V
Digital 10SDA
Digital 11 MOSI
Digital 13 SCK
Digital 12 MISO
Stays unconnectedIRQ
GNDGND
Digital 9RST
Arduino UnoHall Effect Sensor
5V5V
GNDGND
3OUT

After you are done soldering all the components to the perfboard, your apparatus should look something like this:

Step 4: Write the code for DIY Arduino Solenoid door lock

You’ll start by adding all the libraries. Here, you only need libraries:

1) One for the RFID module
2) Second for the SPI communication between RFID and Arduino

You can download library one here, and library two here.

In the code below, the pins for the buzzer, RFID module, and Solenoid lock are defined.

int Buzzer = 4;
const int LockPin = 2;
#define SS_PIN 10
#define RST_PIN 9

Next, define the buzzer pin and the lock pin as OUTPUT, and the sensor pin as INPUT. After that enter command for SPI communication as shown below.

pinMode(LockPin, OUTPUT);
pinMode(Buzzer, OUTPUT);
pinMode(hall_sensor, INPUT);
SPI.begin();      // Initiate  SPI bus     
mfrc522.PCD_Init();   // Initiate MFRC522​

Next, inside the void loop, set up hall sensor values as such: when the value goes low, the door closes.

state = digitalRead(hall_sensor);
  Serial.print(state);
  delay(3000);
  if(state==LOW){
   digitalWrite(LockPin, LOW);
   Serial.print("Door Closed");
   digitalWrite(Buzzer, HIGH);
   delay(2000);
   digitalWrite(Buzzer, LOW);}

After that add the following code in the void loop function.

In the following, the commands will check whether a new RIFD card is there, if it is, the function will check the UID of that card. If the card is valid, the door will unlock, or else it will give an error: “you are not authorized”.

if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  //Show UID on serial monitor
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     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) == "60 4E 07 1E" ) //change here the UID of the card/cards that you want to give access
  {
    digitalWrite(LockPin, HIGH);
    Serial.print("Door Unlocked");
    digitalWrite(Buzzer, HIGH);
    delay(2000);
    digitalWrite(Buzzer, LOW);
  }
  else
  {
   Serial.println("You are not Authorised");
   digitalWrite(Buzzer, HIGH);
   delay(2000);
   digitalWrite(Buzzer, LOW);
  }
}

Step 5: Test your Solenoid lock

Mount your perfboard on the door frame as shown in the image below. The magnet should go on the door to detect movements of the door. After you are done with mounting, test your RFID card. As long as the Hall Effect sensor has a high output, the door will remain open. But when the door reaches the sensor, the status will change to low (because of the magnetic field) and the lock will close again.

The complete code for the project is given below:

#include <SPI.h>
#include <MFRC522.h>
int hall_sensor = 3;
int state,lockread;
int Buzzer = 4;
const int LockPin = 2;
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  pinMode(LockPin, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(hall_sensor, INPUT);
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  //Serial.println("Approximate your card to the reader...");
  // Serial.println();
  digitalWrite(LockPin, LOW);
}
void readsensor()
{
 lockread = digitalRead(LockPin);
 state = digitalRead(hall_sensor);
 //Serial.print(lockread);
 //Serial.print(state);
 // delay(2000); 
}
void loop() 
{
  readsensor();
  sensor(); 
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor 
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {    
     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) == "60 4E 07 1E" ) //change here the UID of the card/cards that you want to give access
  {   
    digitalWrite(LockPin, HIGH);
    Serial.print("Door Unlocked");
    digitalWrite(Buzzer, HIGH);
    delay(2000);
    digitalWrite(Buzzer, LOW);
    sensor();
    }
  else
  {
   Serial.println("You are not Authorised"); 
   digitalWrite(Buzzer, HIGH);
   delay(2000);
   digitalWrite(Buzzer, LOW);
  } 
}
void sensor()
{
 readsensor();
 if (lockread == HIGH){  
      readsensor();
      if(state==LOW){
      digitalWrite(LockPin, LOW);
      Serial.print("Door Closed");
      digitalWrite(Buzzer, HIGH);
      delay(2000);
      digitalWrite(Buzzer, LOW);
    }
  } 
 }

Get started on this project with your Arduino here.

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: