Arduino

Arduino MFRC522 tutorial – Is RFID tag present or removed?

RFID tag present or removed ? Arduino MFRC522 tutorial
Support us by sharing

Why do we need to detect the presence or removal of RFID cards?

Most of the RFID tutorials on the internet will teach you how to read RFID cards. But that will not tell you when the tag is absent. For example, in an RFID based attendance system, we do not need to know for how long the card was there. Still, for some cases, we need to know that the card is present or removed. For instance, in 6 candles games, it is necessary to put all candles at a specified location for triggering the output. For that card under candles must stay at that position. Otherwise, it will disturb the sequence. We will use Arduino and mfrc522 to learn this concept.

Detecting Removal of RFID Tags

In this tutorial, we are going to write a code that on detecting the card will turn on the Green LED. And when we remove the tag, it will turn off the Green LED. Also, the results will appear on the serial monitor. You can modify the code and use it for various purposes. Sounds good? Let’s start.

Do you want to send WhatsApp messages from Arduino?

What we will need?

We will not need a list of items for this tutorial. If you are following my videos and trying my tutorials, you will already have Arduino Uno, MFRC522 RFID reader, Jumper wires, Solderless Breadboard, and LEDs. Are you not following my words? Don’t worry; I have a picture for you.

Is RFID tag present or removed? Arduino MFRC522 tutorial
Components needed

Arduino MFRC522 Interfacing

RFID pinout
MFRC522 PINS ARDUINO UNO PINS
SDA D10
SCK D13
MOSID11
MISOD12
GNDGND
RSTD9
3.3V3.3V
Arduino MFRC522 Interfacing

After connecting MFRC522, connect LED to pin D7 and GND.

Interface LED with Arduino!

Code

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

#define RST_PIN         9           
#define SS_PIN          10 
#define Green 7 

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

void setup(){
  pinMode(Green,OUTPUT);
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init(); 
  
}
uint8_t buf[10]= {};
MFRC522::Uid id;
MFRC522::Uid id2;
bool is_card_present = false;
uint8_t control = 0x00;
void PrintHex(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
{
     char tmp[16];
       for (int i=0; i<length; i++) { 
         sprintf(tmp, "0x%.2X",data[i]); 
         Serial.print(tmp); Serial.print(" ");
       }
}

//*****************************************************************************************//

void cpid(MFRC522::Uid *id){
  memset(id, 0, sizeof(MFRC522::Uid));
  memcpy(id->uidByte, mfrc522.uid.uidByte, mfrc522.uid.size);
  id->size = mfrc522.uid.size;
  id->sak = mfrc522.uid.sak;
}

void loop(){
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  MFRC522::StatusCode status;

    // Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( !mfrc522.PICC_ReadCardSerial()) {
    return;
  }

    bool result = true;
  uint8_t buf_len=4;
  cpid(&amp;id);
  Serial.print("NewCard ");
  PrintHex(id.uidByte, id.size);
  Serial.println("");

    while(true){
digitalWrite(Green,HIGH);
    control=0;
    for(int i=0; i<3; i++){
      if(!mfrc522.PICC_IsNewCardPresent()){
        if(mfrc522.PICC_ReadCardSerial()){
          //Serial.print('a');
          control |= 0x16;
        }
        if(mfrc522.PICC_ReadCardSerial()){
          //Serial.print('b');
          control |= 0x16;
        }
        //Serial.print('c');
          control += 0x1;
      }
      //Serial.print('d');
      control += 0x4;
    }
    
    //Serial.println(control);
    if(control == 13 || control == 14){
      //card is still there
    } else {
      break;
    }
  }
  Serial.println("CardRemoved");
  digitalWrite(Green,LOW);
  delay(500); //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  
}

Code description and video tutorial

Conclusion

By following this tutorial, you will be able to know how RFID works, how to interface the RFID module with Arduino, and how to detect the removal of RFID tags.

This blog is sponsored by win-source.net

Summary
RFID tag present or removed ? Arduino MFRC522 tutorial
Article Name
RFID tag present or removed ? Arduino MFRC522 tutorial
Description
In this tutorial, I have explained how you can detect the presence or absence of RFID tags using the MFRC522 RFID card Reading module, Arduino and LEDs. This tutorial also explains the specification of the MFRC522 RFID card reader and a discussion on how to increase the range of the reader.
Author
Publisher Name
High Voltages
Publisher Logo
Support us by sharing

Comments (1)

  1. You may choose pcb manufacturing and assembly on MOKOPCB what you want. They can produce new PCB for you, or do it on your project

Leave a Reply

%d bloggers like this: