I'm pretty new to Arduino and RFID, and I just finished up with my code and once I tested it, it accepted all the cards rather then just the one I authorized
#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.
byte firmwareVersion = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print("Firmware Version: 0x");
Serial.println(firmwareVersion, HEX);
if (firmwareVersion == 0x92) {
Serial.println("RC522 v2.0 detected.");
} else if (firmwareVersion == 0x91 || firmwareVersion == 0x90) {
Serial.println("RC522 v1.0 detected.");
} else {
Serial.println("Unknown firmware version. Check connections.");
}
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(6, OUTPUT);
pinMode(7, 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 = "662F3A 03") {
// Unlock the door and indicate with green LED
digitalWrite(7, 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(7, LOW);
Serial.println("Access granted. Welcome!");
Serial.println();
delay(3000); // Wait for a few seconds
} else {
// Unauthorized card, indicate with red LED
digitalWrite(6, HIGH);
delay(1000); // Show red LED for 1 second
digitalWrite(6, LOW);
Serial.println("Access denied. Unauthorized card.");
delay(3000); // Wait for a few seconds
}
mfrc522.PICC_HaltA(); // Halt the card
}
here's the updated code, also that's the UID that used to pop up but now I'm using a different card and the UID is "B3 42 32 0E" now.
#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.
byte firmwareVersion = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print("Firmware Version: 0x");
Serial.println(firmwareVersion, HEX);
if (firmwareVersion == 0x92) {
Serial.println("RC522 v2.0 detected.");
} else if (firmwareVersion == 0x91 || firmwareVersion == 0x90) {
Serial.println("RC522 v1.0 detected.");
} else {
Serial.println("Unknown firmware version. Check connections.");
}
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(6, OUTPUT);
pinMode(7, 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 == "B3 42 32 0E") {
// Unlock the door and indicate with green LED
digitalWrite(7, 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(7, LOW);
Serial.println("Access granted. Welcome!");
Serial.println();
delay(3000); // Wait for a few seconds
} else {
// Unauthorized card, indicate with red LED
digitalWrite(6, HIGH);
delay(1000); // Show red LED for 1 second
digitalWrite(6, LOW);
Serial.println("Access denied. Unauthorized card.");
delay(3000); // Wait for a few seconds
}
mfrc522.PICC_HaltA(); // Halt the card
}
}
all of the cards are getting declined now and the UID is getting doubled
#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.
byte firmwareVersion = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print("Firmware Version: 0x");
Serial.println(firmwareVersion, HEX);
if (firmwareVersion == 0x92) {
Serial.println("RC522 v2.0 detected.");
} else if (firmwareVersion == 0x91 || firmwareVersion == 0x90) {
Serial.println("RC522 v1.0 detected.");
} else {
Serial.println("Unknown firmware version. Check connections.");
}
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(6, OUTPUT);
pinMode(7, 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
Serial.println(cardUID);
Serial.println(cardUID == "B34232 0EB342320E");
// Check if the card UID matches the authorized card
if (cardUID == "") {
// Unlock the door and indicate with green LED
digitalWrite(7, 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(7, LOW);
Serial.println("Access granted. Welcome!");
Serial.println();
delay(3000); // Wait for a few seconds
} else {
// Unauthorized card, indicate with red LED
digitalWrite(6, HIGH);
delay(1000); // Show red LED for 1 second
digitalWrite(6, LOW);
Serial.println("Access denied. Unauthorized card.");
delay(3000); // Wait for a few seconds
}
mfrc522.PICC_HaltA(); // Halt the card
}
}
I ended up fixing it but the servo is still not turning
#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.
byte firmwareVersion = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print("Firmware Version: 0x");
Serial.println(firmwareVersion, HEX);
if (firmwareVersion == 0x92) {
Serial.println("RC522 v2.0 detected.");
} else if (firmwareVersion == 0x91 || firmwareVersion == 0x90) {
Serial.println("RC522 v1.0 detected.");
} else {
Serial.println("Unknown firmware version. Check connections.");
}
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(6, OUTPUT);
pinMode(7, 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
Serial.println(cardUID);
Serial.println(cardUID == "B3 42 32 0E");
// Check if the card UID matches the authorized card
if (cardUID == "B3 42 32 0E") {
// Unlock the door and indicate with green LED
digitalWrite(7, 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(7, LOW);
Serial.println("Access granted. Welcome!");
Serial.println();
delay(3000); // Wait for a few seconds
} else {
// Unauthorized card, indicate with red LED
digitalWrite(6, HIGH);
delay(1000); // Show red LED for 1 second
digitalWrite(6, LOW);
Serial.println("Access denied. Unauthorized card.");
delay(3000); // Wait for a few seconds
}
mfrc522.PICC_HaltA(); // Halt the card
}
}
I would copy and paste the portion of the output that clearly illustrates why your program is not working, but you chose to use a screenshot instead of including it as text.
There were four things I suggested that you do to fix your program. You seem to have stopped after doing the first. So be it.
I messed with the code a bit and the serial monitor is now saying this
18:50:39.664 -> Firmware Version: 0xFF = (unknown)
18:50:39.696 -> WARNING: Communication failure, is the MFRC522 properly connected?
18:50:39.763 -> Scan PICC to see UID, SAK, type, and data blocks...
19:15:29.009 -> Firmware Version: 0xB2 = (unknown)
19:15:29.009 -> Scan PICC to see UID, SAK, type, and data blocks...
19:29:11.358 -> Firmware Version: 0xB2
19:29:11.358 -> Unknown firmware version. Check connections.
19:29:11.395 -> Hold your card close to the reader...
19:29:11.435 ->
19:29:13.779 -> Card ID: B34232 0EB342320E
19:29:13.779 -> 0
19:29:14.788 -> Access denied. Unauthorized card.
the updated code is
#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.
byte firmwareVersion = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print("Firmware Version: 0x");
Serial.println(firmwareVersion, HEX);
if (firmwareVersion == 0x92) {
Serial.println("RC522 v2.0 detected.");
} else if (firmwareVersion == 0x91 || firmwareVersion == 0x90) {
Serial.println("RC522 v1.0 detected.");
} else {
Serial.println("Unknown firmware version. Check connections.");
}
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(6, OUTPUT);
pinMode(7, 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
Serial.println(cardUID);
Serial.println(cardUID == "B3 42 32 0E");
// Check if the card UID matches the authorized card
if (cardUID == "B3 42 32 0E") {
// Unlock the door and indicate with green LED
digitalWrite(7, 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(7, LOW);
Serial.println("Access granted. Welcome!");
Serial.println();
delay(3000); // Wait for a few seconds
} else {
// Unauthorized card, indicate with red LED
digitalWrite(6, HIGH);
delay(1000); // Show red LED for 1 second
digitalWrite(6, LOW);
Serial.println("Access denied. Unauthorized card.");
delay(3000); // Wait for a few seconds
}
mfrc522.PICC_HaltA(); // Halt the card
}
}
There is a difference between the the first number (read from the card in the for-loop) and the second number. That is strange and I have no idea why that is happening.
Before you enter the for-loop to read the uid bytes, I would add the below
Serial,print(F("There are "));
Serial.print(mfrc522.uid.size);
Serial.println(F(" uid bytes");)
This should print the number of uid bytes and might explain why you only have 3 bytes in the the first number.
That should be without the spaces between bytes in the String representation.
Another approach could be to directly compare the uid bytes against a valid card.
void loop()
{
// Check for RFID card
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
{
// Read the RFID card's UID
Serial.print("Card ID: ");
byte validCard[] = { 0xB3, 0x42, 0x32, 0x0E };
// if there is a mismatch in the number of bytes
if (mfrc522.uid.size != sizeof(validCard))
{
Serial.println("OOPS");
}
else
{
// if the uid bytes match the card
if (memcmp(mfrc522.uid.uidByte, validCard, sizeof(validCard)) == 0)
{
Serial.println("match");
}
// if the uid bytes do not match the card
else
{
Serial.println("no match");
}
}
mfrc522.PICC_HaltA(); // Halt the card
}
}
I can not test as I don't have a reader/cards but it does compile.
#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
int attemptCount = 0; // Variable to track failed attempts
const int maxAttempts = 3; // Max number of attempts before lockout
const int lockoutTime = 30000; // Time in milliseconds to lock out the system
void setup() {
Serial.begin(9600); // Start the communication with your computer
SPI.begin(); // Initialize the SPI communication.
mfrc522.PCD_Init(); // Initialize the RFID reader.
byte firmwareVersion = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print("Firmware Version: 0x");
Serial.println(firmwareVersion, HEX);
if (firmwareVersion == 0x92) {
Serial.println("RC522 v2.0 detected.");
} else if (firmwareVersion == 0x91 || firmwareVersion == 0x90) {
Serial.println("RC522 v1.0 detected.");
} else {
Serial.println("Unknown firmware version. Check connections.");
}
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(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
// Check if the system is in lockout mode
if (attemptCount >= maxAttempts) {
Serial.println("Max attempts reached. Locking out for 30 seconds.");
digitalWrite(redLedPin, HIGH); // Show red LED to indicate lockout
delay(lockoutTime); // Wait for lockout time
digitalWrite(redLedPin, LOW); // Turn off red LED after lockout time
attemptCount = 0; // Reset attempt counter
return; // Skip the rest of the loop until lockout is over
}
// Check for RFID card
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
{
// Read the RFID card's UID
Serial.print("Card ID: ");
// Print the number of UID bytes
Serial.print(F("There are "));
Serial.print(mfrc522.uid.size);
Serial.println(F(" uid bytes"));
byte validCard[] = { 0xB3, 0x42, 0x32, 0x0E };
// if there is a mismatch in the number of bytes
if (mfrc522.uid.size != sizeof(validCard))
{
Serial.println("OOPS");
}
else
{
// if the uid bytes match the card
if (memcmp(mfrc522.uid.uidByte, validCard, sizeof(validCard))== 0)
{
Serial.println("match");
}
// if the uid bytes do not match the card
else
{
Serial.println("no match");
}
}
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
Serial.println(cardUID);
Serial.println(cardUID == "B342320E");
// Check if the card UID matches the authorized card
if (cardUID == "B342320E") {
// Unlock the door and indicate with green LED
digitalWrite(7, HIGH);
myservo.write(110); // Open the door (servo at 90 degrees)
delay(2000); // Keep the door open for 2 seconds
myservo.write(15); // Close the door (servo at 0 degrees)
digitalWrite(7, LOW);
Serial.println("Access granted. Welcome!");
Serial.println();
attemptCount = 0; // Reset the attempt counter after successful access
delay(3000); // Wait for a few seconds
} else {
// Unauthorized card, indicate with red LED
digitalWrite(6, HIGH);
delay(1000); // Show red LED for 1 second
digitalWrite(6, LOW);
Serial.println("Access denied. Unauthorized card.");
attemptCount++; // Increment failed attempt counter
delay(3000); // Wait for a few seconds
}
mfrc522.PICC_HaltA(); // Halt the card
}
}
}
and I think the solution was
byte validCard[] = { 0xB3, 0x42, 0x32, 0x0E };
// if there is a mismatch in the number of bytes
if (mfrc522.uid.size != sizeof(validCard))
{
Serial.println("OOPS");
}
else
{
// if the uid bytes match the card
if (memcmp(mfrc522.uid.uidByte, validCard, sizeof(validCard)) == 0)
{
Serial.println("match");
}
// if the uid bytes do not match the card
else
{
Serial.println("no match");
}
}