Rohan Barnwal
Published © GPL3+

RFID-Based Smart Servo Lock System using Arduino R4 Wi-Fi

Smart RFID Lock using Arduino UNO R4 - keyless, portable, and automatic servo lock any space. Secure simple and smart!

IntermediateFull instructions provided1 hour3
RFID-Based Smart Servo Lock System using Arduino R4 Wi-Fi

Things used in this project

Hardware components

UNO R4 WiFi
Arduino UNO R4 WiFi
×1
Jumper wires (generic)
Jumper wires (generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
MFRC522 RFID Module
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connections

Code

Code

Arduino
// RFID-triggered servo: initial 150deg -> on scan move to 0deg, hold 5s, return to 150deg
// Requires MFRC522 (Miguel Balboa) and Servo libraries
// Servo signal on pin D6. RC522 wiring per comments below.

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

#define RST_PIN    9    // RC522 RST
#define SS_PIN     10   // RC522 SDA/SS
#define SERVO_PIN  6    // servo signal pin

// Authorized UID bytes (your card)
const byte AUTH_UID[4] = { 0x2A, 0x7E, 0x17, 0xB1 };

MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;

const int initialPos = 150;              // starting position (deg)
const int triggeredPos = 0;              // position to move to when RFID read (deg)
const unsigned long holdTimeMs = 5000UL; // hold time at triggeredPos (ms)
const int stepDelay = 8;                 // ms delay between degree steps for smooth movement

void setup() {
  Serial.begin(115200);
  while (!Serial) { } // wait for Serial on some boards (harmless on others)

  SPI.begin();
  mfrc522.PCD_Init();

  myServo.attach(SERVO_PIN);
  myServo.write(initialPos); // place servo at initial position
  Serial.println();
  Serial.println("RFID -> Servo ready.");
  Serial.print("Initial angle: ");
  Serial.print(initialPos);
  Serial.println(" deg");
  Serial.println("Scan authorized card to trigger movement.");
}

void loop() {
  // look for a new card
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  // Print scanned UID
  Serial.print("Scanned UID: ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    if (mfrc522.uid.uidByte[i] < 0x10) Serial.print("0");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    if (i < mfrc522.uid.size - 1) Serial.print(":");
  }
  Serial.println();

  // Only react if UID size is 4 and matches AUTH_UID
  if (mfrc522.uid.size == 4) {
    bool match = true;
    for (byte i = 0; i < 4; i++) {
      if (mfrc522.uid.uidByte[i] != AUTH_UID[i]) {
        match = false;
        break;
      }
    }

    if (match) {
      Serial.println("Authorized card detected. Moving servo 150 -> 0, hold 5s, return to 150.");
      moveServoSmooth(initialPos, triggeredPos, stepDelay);
      delay(holdTimeMs);
      moveServoSmooth(triggeredPos, initialPos, stepDelay);
      Serial.println("Movement complete. Waiting for card removal before next trigger.");

      // Halt card & stop crypto
      mfrc522.PICC_HaltA();
      mfrc522.PCD_StopCrypto1();

      // Wait until the card is removed to avoid immediate retrigger
      while (mfrc522.PICC_IsNewCardPresent() || mfrc522.PICC_ReadCardSerial()) {
        delay(100);
        // clear any repeated read
        if (mfrc522.PICC_IsNewCardPresent()) {
          mfrc522.PICC_HaltA();
          mfrc522.PCD_StopCrypto1();
        }
      }
      Serial.println("Card removed. Ready.");
    } else {
      Serial.println("UID NOT authorized. Ignored.");
    }
  } else {
    Serial.println("UID length not 4 bytes — ignored.");
  }

  // final cleanup and small debounce
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  delay(150);
}

// Smooth servo movement helper: moves degree-by-degree and prints each angle to Serial
void moveServoSmooth(int fromDeg, int toDeg, int stepDelayMs) {
  if (fromDeg == toDeg) {
    myServo.write(fromDeg);
    Serial.print("Angle: ");
    Serial.print(fromDeg);
    Serial.println(" deg");
    return;
  }

  int step = (toDeg > fromDeg) ? 1 : -1;
  for (int deg = fromDeg; deg != toDeg; deg += step) {
    myServo.write(deg);
    Serial.print("Angle: ");
    Serial.print(deg);
    Serial.println(" deg");
    delay(stepDelayMs);
  }
  // final position
  myServo.write(toDeg);
  Serial.print("Angle: ");
  Serial.print(toDeg);
  Serial.println(" deg");
}

Credits

Rohan Barnwal
35 projects • 35 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments