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 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.
Arduino MFRC522 Interfacing
MFRC522 PINS | ARDUINO UNO PINS |
---|---|
SDA | D10 |
SCK | D13 |
MOSI | D11 |
MISO | D12 |
GND | GND |
RST | D9 |
3.3V | 3.3V |
After connecting MFRC522, connect LED to pin D7 and GND.
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(&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
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
There seems to be a mistake in your drawing: the RF board is connected to the 5V pin on your drawing… Guess that’s not supposed to be done.
What changes need to be made to run on nodemcu esp8266