Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO RFID Door Lock with Servo Motor

View and Edit Project In Cirkit DesignerImage of Arduino UNO RFID Door Lock with Servo Motor

Circuit Documentation

Summary

This circuit integrates an Arduino UNO microcontroller with an RFID-RC522 reader and a servo motor. The purpose of the circuit is to read RFID tags and control the position of the servo motor based on the tag's data. When an authorized RFID tag is detected, the servo motor unlocks (rotates to a specific angle) for a short period before returning to the locked position.

Component List

Arduino UNO

RFID-RC522

Servo

Wiring Details

Arduino UNO

RFID-RC522

Servo

Documented Code

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

#define SS_PIN 10
#define RST_PIN 9
#define SERVO_PIN 8

MFRC522 rfid(SS_PIN, RST_PIN);
Servo myServo;

// Define authorized RFID card UID
byte authorizedUID[] = {0xDE, 0xAD, 0xBE, 0xEF}; // Example UID (Replace with your own UID)

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  myServo.attach(SERVO_PIN);
  myServo.write(0); // Start with the lock closed
  Serial.println("Place your card near the reader...");
}

void loop() {
  // Check for new RFID cards
  if (!rfid.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if (!rfid.PICC_ReadCardSerial()) {
    return;
  }

  // Print UID to Serial Monitor
  Serial.print("UID tag : ");
  for (byte i = 0; i < rfid.uid.size; i++) {
    Serial.print(rfid.uid.uidByte[i], HEX);
    Serial.print(" ");
  }
  Serial.println();

  // Check if the scanned UID matches the authorized UID
  if (compareUID(rfid.uid.uidByte, authorizedUID, rfid.uid.size)) {
    Serial.println("Access Granted!");
    myServo.write(90); // Unlock the door
    delay(3000); // Keep it unlocked for 3 seconds
    myServo.write(0); // Lock the door again
  } else {
    Serial.println("Access Denied!");
  }

  rfid.PICC_HaltA(); // Stop reading
}

bool compareUID(byte *uid, byte *authorizedUID, byte uidSize) {
  for (byte i = 0; i < uidSize; i++) {
    if (uid[i] != authorizedUID[i]) {
      return false; // UID does not match
    }
  }
  return true; // UID matches
}

The code initializes the RFID reader and the servo motor. It continuously checks for new RFID cards and compares the UID of the scanned card with a predefined authorized UID. If the UIDs match, the servo motor is activated to unlock. After a delay, the servo motor returns to the locked position. If the UIDs do not match, access is denied.