I only started coding on Arduino and RC522 like 2 weeks ago and I've been having this problem recently, I use the MFRC522 and Easy MFRC522, also Adafruit BusIO Libraries
Here's my code
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
// Define RFID RC522 pins
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Define servo motor and LED pins
Servo myservo;
const int redLedPin = 6;
const int greenLedPin = 7;
// Define variables
boolean doorLocked = true; // Initially, the door is locked
void setup() {
Serial.begin(9600); // Start the communication with your computer
SPI.begin(); // Initialize the SPI communication.
mfrc522.PCD_Init(); // Initialize the RFID reader.
delay(5);
Serial.println("Hold your card close to the reader...");
Serial.println();
myservo.attach(3); // Attach servo to pin 3
myservo.write(0); // Close the door (servo at 0 degrees)
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
}
void loop() {
// Check for RFID card
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
// Read the RFID card's UID
Serial.print("Card ID: ");
String cardUID = ""; // Store the card's ID
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : ""); // Format the ID with leading zeros
Serial.print(mfrc522.uid.uidByte[i], HEX); // Print the ID in hexadecimal
cardUID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "") + String(mfrc522.uid.uidByte[i], HEX); // Concatenate the ID in hexadecimal
}
cardUID.toUpperCase(); // Convert to uppercase
// Check if the card UID matches the authorized card
if (cardUID == "90 95 4C 58") {
// Unlock the door and indicate with green LED
digitalWrite(greenLedPin, HIGH);
myservo.write(90); // Open the door (servo at 90 degrees)
delay(2000); // Keep the door open for 2 seconds
myservo.write(0); // Close the door (servo at 0 degrees)
digitalWrite(greenLedPin, LOW);
Serial.println("Access granted. Welcome!");
Serial.println();
delay(3000); // Wait for a few seconds
} else {
// Unauthorized card, indicate with red LED
digitalWrite(redLedPin, HIGH);
delay(1000); // Show red LED for 1 second
digitalWrite(redLedPin, LOW);
Serial.println("Access denied. Unauthorized card.");
delay(3000); // Wait for a few seconds
}
mfrc522.PICC_HaltA(); // Halt the card
}
}
here's the wiring diagram (I got it from youtube)
Thanks!
