top of page

How to build an RFID door lock with Arduino

I love Arduino. Arduino successfully brought microcontrollers to the people. They make working with hardware seem fun and simple. Arduino hardware and software can be used for a wide range of projects, from classroom examples to large-scale engineering challenges. In this blog post, I go over how to make an auto-door lock using an Arduino and a couple other pieces of hardware. It's not as difficult as it sounds!


If you have any questions about Arduino or need a professional software engineer to help with a project you're working on, schedule an intro call with CodeConda for free!


Arduino Components Needed:

  1. Arduino board (e.g., Arduino Uno)

  2. RFID Reader (e.g., MFRC522)

  3. RFID Cards or Key Fobs

  4. Servo Motor or Solenoid Lock

  5. Breadboard and jumper wires

  6. Power supply (9V battery or USB power adapter)

  7. Access to a computer with the Arduino IDE installed

  8. Basic soldering tools (if required)


Step-by-Step Instructions:


1. Understand the Basics:


Before diving in, let's understand the key components:

  • Arduino: The brain of the system.

  • RFID Reader: Reads data from RFID cards.

  • RFID Cards: Carry unique identification codes.

  • Servo Motor or Solenoid Lock: Actuates the locking mechanism.

  • Breadboard and wires: Create connections.

  • Power supply: Powers the Arduino and components.


2. Set Up the Circuit:


Follow these RFID connections using jumper wires:

  • Connect VCC of the RFID module to 3.3V or 5V on Arduino.

  • Connect GND of the RFID module to GND on Arduino.

  • Connect RST of the RFID module to a digital pin (e.g., 9) on Arduino.

  • Connect SDA of the RFID module to a digital pin (e.g., 10) on Arduino.

  • Connect MOSI of the RFID module to a digital pin (e.g., 11) on Arduino.

  • Connect MISO of the RFID module to a digital pin (e.g., 12) on Arduino.

  • Connect SCK of the RFID module to a digital pin (e.g., 13) on Arduino.


Follow these servo motor or solenoid connections using jumper wires:

  • Connect the control pin of the servo or solenoid to a digital pin (e.g., 6) on Arduino.

  • Connect the power and ground pins of the servo or solenoid to appropriate power sources.


3. Install Libraries:

  • Open the Arduino IDE on your computer.

  • Go to "Sketch" > "Include Library" > "Manage Libraries."

  • Search for "MFRC522" and install the library for the RFID module.

  • If you're using a servo motor, you don't need an additional library. If you're using a solenoid lock, make sure to install the necessary libraries for controlling it.


4. Coding:

  • Open the Arduino IDE and write the code. This code reads RFID cards and controls the lock.

  • Initialize RFID reader and servo in the setup function.

  • In the loop function, check for a card.

  • If an authorized card is detected, unlock for a few seconds, then lock again.


Here's a basic example code to get you started. This code reads an RFID card and controls a servo motor to simulate locking and unlocking.


#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);

#define SERVO_PIN 6
#include <Servo.h>

Servo servo;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  servo.attach(SERVO_PIN);
}

void loop() {
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    Serial.println("Card detected!");

    if (checkCard()) {
      openDoor();
      delay(5000); // Keep the door unlocked for 5 seconds
      closeDoor();
    }
    
    mfrc522.PICC_HaltA();
  }
}

bool checkCard() {
  // Define the UID of your authorized cards or key fobs
  byte authorizedCard1[] = {0xAA, 0xBB, 0xCC, 0xDD};
  // Add more authorized cards if needed
  
  for (int i = 0; i < sizeof(authorizedCard1); i++) {
    if (mfrc522.uid.uidByte[i] != authorizedCard1[i]) {
      return false;
    }
  }
  
  return true;
}

void openDoor() {
  servo.write(90); // Assuming 90 degrees is unlocked position
}

void closeDoor() {
  servo.write(0); // Assuming 0 degrees is locked position
}

5. Upload and Test:

  • Upload the code to your Arduino.

  • Open the Serial Monitor to debug and view messages.

  • Test the system by placing an authorized card on the reader. Observe if the lock responds as intended.


6. Building the Physical Lock:

  • Mount the servo motor or solenoid lock on your door.

  • Connect the lock mechanism to the door's handle or locking system.

  • Ensure the lock can be securely attached and activated by the servo/solenoid.


7. Powering the System:

  • Power your Arduino using a 9V battery or USB adapter.

  • Ensure the power supply can handle both the Arduino and the lock mechanism.


8. Fine-Tuning and Expansion:

  • Adjust servo angles in the code to ensure proper locking and unlocking.

  • Modify delay times to suit your preferences.

  • Experiment with different RFID cards for authorization.


9. Safety and Security:

  • Ensure authorized cards are kept secure.

  • Regularly maintain and test your lock for reliability.

  • Consider adding a backup physical lock.


10. Learn and Explore:


This project is just the beginning! You can expand it by:

  • Adding more advanced security features.

  • Integrating with home automation systems.

  • Creating a user-friendly interface.


Remember, building your RFID door lock is a creative and educational journey. Don't hesitate to iterate and improve upon your design as you learn more. Have fun and happy tinkering!


Disclaimer: This project involves electronics. Be cautious, and ensure safety measures while handling tools and components.




Kommentare


bottom of page