Rfid Detector
Rfid Detector
com/microcontroller-projects/interfacing-rfid-reader-module-with-arduino
SS SCK MOSI MISO SPI communication pins. Slave Select, Clock, MOSI, and
MISO.
IRQ Interrupt signal from the module to indicate RFID tag detection.
GND Ground pin that needs to be connected to the GND pin on the Arduino.
VCC Supply pin for the module. The supply voltage can be anywhere from 2.5V to
3.3V and must be connected to the 3.3V pin on the Arduino.
The RFID tag can be either passive or active. Active tags are powered by batteries
while the passive RFID tags are powered by energy from the reader’s interrogating
EM waves. The tags are available in different forms or shapes like cards, tags, key
forbs, or stickers. Whatever the shape, the RFID tag will consist of an antenna and the
RFID chip, which will store all the data. When triggered by an electromagnetic
interrogation pulse from a nearby RFID reader, the tag will transmit data back to the
reader. The reader will then analyze this data to identify the tag. Unlike a barcode or a
QR code, the tag does not need to be within the reader’s line of sight. This makes it
easier to process and can be used for tracking objects in closed space.
As the connections are made, let’s look at the coding part. For that, we are going to
use the MFRC522 Arduino Library by Miguel André Balboa. Since the library is
not available in the Arduino library manager, download it from the MFRC522
GitHub repository and install it in the Arduino library folder. You can install it either
through the Arduino IDE, by going to Sketch -> Include Library -> Add ZIP
Library and selecting the downloaded .ZIP file, or by just simply extracting the Zip
file into the Arduino library folder.
Once the library is installed, we can test our setup with an example code. For that,
open the DumpInfo example from the MFRC522 library. Here is the example code.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(115200); // Initialize serial communications
with the PC
while (!Serial); // Do nothing if no serial port is opened
(added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
delay(4); // Optional
delay. Some board do need more time after init to be ready, see Readme
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD -
MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data
blocks..."));
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This
saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically
called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
WRITING DATA
#include <SPI.h> //include the SPI library
#include <MFRC522.h> //include the MFRC522 RFID reader library
#define RST_PIN 9 //reset pin, which can be changed to another digital pin if
needed.
#define SS_PIN 10 //SS or the slave select pin, which can be changed to another
digital pin if needed.
MFRC522 mfrc522(SS_PIN, RST_PIN); // create a MFRC522 instant.
MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key' to hold
the card information
byte data1[14] = {"Circuit-Digest"}; //The first data that needs to be written to
the tag.
byte data2[12] = {"Jobit-Joseph"}; //The second data that needs to be written to
the tag.
byte readbackblock[18]; //Array for reading out a block.
void setup()
{
Serial.begin(115200); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD
means: proximity coupling device)
Serial.println("Scan a MIFARE Classic card");
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF; // Prepare the security key for the read and write
operations.
}
}
void loop()
{
// Look for new cards if not found rerun the loop function
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// read from the card if not found rerun the loop function
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.println("card detected. Writing data");
writeBlock(1, data1); //write data1 to the block 1 of the tag
writeBlock(2, data2); //write data2 to the block 2 of the tag
Serial.println("reading data from the tag");
readBlock(1, readbackblock); //read block 1
//print data
Serial.print("read block 1: ");
for (int j = 0 ; j < 14 ; j++)
{
Serial.write (readbackblock[j]);
}
Serial.println("");
readBlock(2, readbackblock); //read block 2
//print data
Serial.print("read block 2: ");
for (int j = 0 ; j < 12 ; j++)
{
Serial.write (readbackblock[j]);
}
Serial.println("");
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));//uncomment below line if want to
see the entire memory dump.
}
//Write specific block
int writeBlock(int blockNumber, byte arrayAddress[])
{
//check if the block number corresponds to data block or triler block, rtuen
with error if it's trailer block.
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; //determine trailer block for the
sector
if (blockNumber > 2 && (blockNumber + 1) % 4 == 0) {
Serial.print(blockNumber);
Serial.println(" is a trailer block: Error");
return 2;
}
//authentication
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3;//return "3" as error message
}
//writing data to the block
status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
//status = mfrc522.MIFARE_Write(9, value1Block, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("Data write failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4;//return "4" as error message
}
Serial.print("Data written to block ");
Serial.println(blockNumber);
}
//Read specific block
int readBlock(int blockNumber, byte arrayAddress[])
{
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; //determine trailer block for the
sector
//authentication of the desired block for access
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed : ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3;//return "3" as error message
}
//reading data from the block
byte buffersize = 18;
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress,
&buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read
requires a pointer instead of just a number
if (status != MFRC522::STATUS_OK) {
Serial.print("Data read failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4;//return "4" as error message
}
Serial.println("Data read successfully");
}
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
#define LED 8
byte readCard[4];
String tag_UID = "39C3BB99"; // Replace this with the UID of your tag!!!
String tagID = "";
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup()
{
pinMode(LED, OUTPUT);// initialize digital pin LED_BUILTIN as an output.
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
Serial.begin(115200); // Initialize serial communications with the PC
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // Initialise MFRC522
}
void loop()
{
//Wait until new tag is available
while (readID()
{
if (tagID == tag_UID)
{
digitalWrite(LED, !digitalRead(LED)); // Turn on or off the onboard led
}
}
}
//Read new tag if available
boolean readID()
{
//Check if a new tag is detected or not. If not return.
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return false;
}
//Check if a new tag is readable or not. If not return.
if ( ! mfrc522.PICC_ReadCardSerial())
{
return false;
}
tagID = "";
// Read the 4 byte UID
for ( uint8_t i = 0; i < 4; i++)
{
//readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Convert the UID to a
single String
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return true;
}
LED CONTROL
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
#define LED 8
byte readCard[4];
String tag_UID = "39C3BB99"; // Replace this with the UID of your tag!!!
String tagID = "";
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup()
{
pinMode(LED, OUTPUT);// initialize digital pin LED_BUILTIN as an output.
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // Initialise MFRC522
}
void loop()
{
//Wait until new tag is available
while (readID())
{
if (tagID == tag_UID)
{
digitalWrite(LED, !digitalRead(LED)); // Turn on or off the onboard led
}
}
}
//Read new tag if available
boolean readID()
{
//Check if a new tag is detected or not. If not return.
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return false;
}
//Check if a new tag is readable or not. If not return.
if ( ! mfrc522.PICC_ReadCardSerial())
{
return false;
}
tagID = "";
// Read the 4 byte UID
for ( uint8_t i = 0; i < 4; i++)
{
//readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Convert the UID to a
single String
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return true;
}
Write:
#include <SPI.h> //include the SPI library
#include <MFRC522.h> //include the MFRC522 RFID reader library
#define RST_PIN 9 //reset pin, which can be changed to another digital pin if
needed.
#define SS_PIN 10 //SS or the slave select pin, which can be changed to another
digital pin if needed.
MFRC522 mfrc522(SS_PIN, RST_PIN); // create a MFRC522 instant.
MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key' to hold
the card information
byte data1[14] = {"Circuit-Digest"}; //The first data that needs to be written to
the tag.
byte data2[12] = {"Jobit-Joseph"}; //The second data that needs to be written to
the tag.
byte readbackblock[18]; //Array for reading out a block.
void setup()
{
Serial.begin(115200); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD
means: proximity coupling device)
Serial.println("Scan a MIFARE Classic card");
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF; // Prepare the security key for the read and write
operations.
}
}
void loop()
{
// Look for new cards if not found rerun the loop function
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// read from the card if not found rerun the loop function
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.println("card detected. Writing data");
writeBlock(1, data1); //write data1 to the block 1 of the tag
writeBlock(2, data2); //write data2 to the block 2 of the tag
Serial.println("reading data from the tag");
readBlock(1, readbackblock); //read block 1
//print data
Serial.print("read block 1: ");
for (int j = 0 ; j < 14 ; j++)
{
Serial.write (readbackblock[j]);
}
Serial.println("");
readBlock(2, readbackblock); //read block 2
//print data
Serial.print("read block 2: ");
for (int j = 0 ; j < 12 ; j++)
{
Serial.write (readbackblock[j]);
}
Serial.println("");
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed : ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3;//return "3" as error message
}