Tutorial – RFID

Tutorial – RFID

RFID (Radio Frequency IDentification) is a non-contact technology used in many applications. Maybe you already know what is NFC because it’s used in many smartphones, NFC is just like a subcategory of RFID.

This technology is used for access control, books tracking…and so on. That’s because it is simple and efficient. It works like a radio and it consists of two main components:

  • a tag, located on the object that we want to identify.
  • a tag reader.

How it works?

The RFID reader has a radio frequency module and an antenna coil inside. So it generates a high frequency electromagnetic field. The tag is usually a passive component, which consists of just an antenna and a microchip. When the tag gets near the reader’s electromagnetic field, a voltage is generated in the simple tag’s circuit due to induction. So the tag can be read.

So we can make cool projects with this module!!

But before making a new project, we have to learn how to control all the modules that we are going to use!

Now we will explain how to use RFID, without using other modules. Then we will make a project more complex that will include also other modules.

Let’s start!

Schematic

This is the first thing to learn.

We tested our sketch with an Arduino nano. The schematic depends on the Arduino board that you are using. Anyway we will say you how to do the wirings also with other boards.

Schematic tested by us.

Then we have to upload a sketch…

pin layout for other boards.

Sketch

#include <SPI.h>        //include this two libraries
#include <MFRC522.h>

//RFID pin
#define N 1 //max number of tags that can be used
#define RST_PIN   9
#define SS_PIN    10

MFRC522 mfrc522(SS_PIN, RST_PIN);   //MRRC522 object

byte readCard[4];
char* myTags[N] = {};               //here we save our tag

String tagID = "";

int state;                          //this variable is used for the switch(case), I called it 'state' to remember a finite-state machine.
bool done = false;                  //this boolean variable is used in the while in the setup. To be sure you select a tag first of all.

void setup() 
{  
  SPI.begin();                      //Initialize the SPI bus 
  Serial.begin(9600);               //Initialize the serial communication
  
  mfrc522.PCD_Init();               //Initialize MFRC522

// Now Arduino waits until a tag is scanned
  
  while (!done)
  {
    if (getID())
    {
      myTags[0] = strdup(tagID.c_str()); //Sets the tag into position 0 in the array. For our tutorial this will be the only position available.
                                         //This means that you can save only one tag
      Serial.println("You have choosen");
      state=0;                           //go to state 0
      done = true;                       //exit from the while
    }
  }
}

void loop()
{
  switch(state)
  {      
    case 0:
      Serial.println("Waiting for tag!");
      if(getID())   
      {
        if(!correctTag())
        {
          Serial.println("Wrong tag!");
          state=0;
        }
        else if(correctTag())
        {
          Serial.println("Correct tag!");
          state=1;   //if the target is correct go to state 1
        }   
      }
      else
      {
        state=0;
      }
      
      break;

    case 1: 
        Serial.println("Door unlocked!");       //just for the tutorial, you will only have to wait the message that the door is locked. In other projects
                                                //we will do it more complex
        delay(3000);
        Serial.println("Door Locked!");
        state = 0;    //go to state 0
  }
}

bool correctTag()       //this function checks if the tag is correct(the first we saved) or not
{
  if(tagID==myTags[0])
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

bool getID() 
{
  // Getting ready to read PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) //If a new PICC is placed to RFID reader continue
  {
    return 0;
  }
  
  if ( ! mfrc522.PICC_ReadCardSerial()) //Since a PICC is placed get Serial and continue
  {
    return 0;
  }
  
  tagID = "";
  for ( uint8_t i = 0; i < 4; i++) 
  {  //The MIFARE PICCs that we use have 4 byte UID
    readCard[i] = mfrc522.uid.uidByte[i];
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); //Adds the 4 bytes in a single String variable
  }
  tagID.toUpperCase();
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;   //return 1 only if we put a tag on the reader
}

From this sketch you can also create your own project!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: