MFRC522 Help with "Any" scan

Hey there, fairly simple issue! I am trying to just check that an RFID badge was scanned. I dont care about the value or anything just that it was scanned. Here is my current code just turning a light green or red.


#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

const int PIN_RED   = 7;
const int PIN_GREEN = 6;
const int PIN_BLUE  = 5;

void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();

pinMode(PIN_RED,   OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE,  OUTPUT);
}
void loop() 
{
  
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  
  if (content.substring(1) == "04 8C 5F 3A 7A 60 80") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    analogWrite(PIN_GREEN, 255);
    Serial.println();
    delay(1000);
    analogWrite(PIN_RED,   0);
    analogWrite(PIN_GREEN, 0);
    analogWrite(PIN_BLUE,  0);
  }
 
 else   {
    Serial.println(" Access denied");
    analogWrite(PIN_RED, 255);
    delay(1000);
    analogWrite(PIN_RED,   0);
    analogWrite(PIN_GREEN, 0);
    analogWrite(PIN_BLUE,  0);
  }
} 

Bonus if we could only accept scans from a device in a certain format?
Its fine if we just accept all RFID scans but would be cool if we only accepted them with a value format of
"04 8C 5F 3A 7A 60 80" or
" 00 00 00 00 00 00 00"
all my devices are in this format.

Thanks for any help you guys

I don't have time to look at the code in detail at the moment but I found the use of English amusing :grinning:

  Serial.println("Approximate your card to the reader...");

Why did you convert the UID to String and then compare it with other string?
Your UID is only seven bytes, why not compare it with saved one also in bytes?

So what exactly is your issue? Your code runs just fine.

I assuming you are using Arduino uno ,your code working perfectly it gives card number in hex fine, check your connections . if you have any doubts about the pins check this https://github.com/miguelbalboa/rfid

Yes. I don’t want to assign a certain UID. I just want to check that a rfid device was scanned. I’m using the rfid as a trigger for another device. I want to accept any RFID device

And again, what is your issue?
If you don't need to check a certain UID - the only condition that you should to test is Reading UID from card:

I think you asked for assistance with the code . You want to give access by reading any rfid card. I hope this will help..

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

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.

const int PIN_RED = 7;
const int PIN_GREEN = 6;
const int PIN_BLUE = 5;
uint8_t readsuccess;
void setup() {

  Serial.begin(9600);  // Initiate a serial communication
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE, OUTPUT);
  SPI.begin();  // Initiate  SPI bus
    // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();
  mfrc522.PCD_Init();
}
void loop() {
  readsuccess = getid();
  if (readsuccess) {
    Serial.println("Authorized access");
    analogWrite(PIN_GREEN, 255);
    Serial.println();
    delay(1000);
    analogWrite(PIN_RED, 0);
    analogWrite(PIN_GREEN, 0);
    analogWrite(PIN_BLUE, 0);
  }
}

bool getid() {
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return 0;
  }
  if (!mfrc522.PICC_ReadCardSerial()) {
    return 0;
  }
  Serial.print("UID tag :");
  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  mfrc522.PICC_HaltA();
  return true;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.