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
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13.
RFID-RC522
- Description: An RFID reader/writer module.
- Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA.
Servo
- Description: A rotary actuator or linear actuator that allows for precise control of angular or linear position.
- Pins: GND, VCC, PWM.
Wiring Details
Arduino UNO
- 3.3V connected to RFID-RC522 VCC (3.3V)
- 5V connected to Servo VCC
- GND connected to Servo GND and RFID-RC522 GND
- D13 (SCK) connected to RFID-RC522 SCK
- D12 (MISO) connected to RFID-RC522 MISO
- D11 (MOSI) connected to RFID-RC522 MOSI
- D10 (SS_PIN) connected to RFID-RC522 SDA
- D9 (RST_PIN) connected to RFID-RC522 RST
- D8 (SERVO_PIN) connected to Servo PWM
RFID-RC522
- VCC (3.3V) connected to Arduino UNO 3.3V
- RST connected to Arduino UNO D9
- GND connected to Arduino UNO GND
- SCK connected to Arduino UNO D13
- MISO connected to Arduino UNO D12
- MOSI connected to Arduino UNO D11
- SDA connected to Arduino UNO D10
Servo
- VCC connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
- PWM connected to Arduino UNO D8
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.
