$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS Arduino

Getting Started With RFID

DFRobot Feb 22 2016 608



Have you ever wondered how your ID tag works?

In this tutorial, we will be able to show you how to read a RFID button, or tag, if it’s held up against ID-12 RFID reader or any 125KHz module. Also this project is an easy and fun way to explain the basic concept of digital identification technique using ID-12 together with ping ultrasonic and LEDs.

 

Step 1: Materials

To follow along and create this tutorial, you will need the following materials:
  • ID-12LA RFID Reader
  • 2 - RFID buttons 125 kHz (Black)
  • ATMEGA32U2 USB Dev Board for ID-12 and ID-20
  • Ping Ultrasonic Sensor
  • 2 - LEDs (1 Red and 1 Green)
  • 2 - 220ohm Resistors
  • Jumper Wires

 

Step 2: ID-12LA RFID Kit - ID-12LA RFID reader

 


In this kit there are three things: ID-12LA RFID reader, RFID Button, and ATMEGA32U2 USB Dev Board.
Here is a little information about each part:
ID-12LA RFID reader: It uses radio frequency to identifying any object with a RFID tag wirelessly within a certain range.
  • This reader is going to send out a radio signal that is going to be picked up and answered with a unique string of data from the RFID tag or button. This module has been used in applications such as access control, load identification, alarm system, and so on.
  • The only things that make ID-12 different from the other RFID module (ID-2 and ID-20) is that it has a built-in antenna, and the size of the antenna. All these different ID readers use the same communication protocols, and works within a three data output format. This tutorial is using the ASCII (American Standard Code for Information Interchange) data format. That means out of 32 bit unique number of the RFID button, this tutorial concentrates on the 12 bit from the DATA and CHECKSUM.
  • STX stands for Start-of-Text. It indicates that communication between the RFID button and RFID reader has started while ETX is an End-of-Text communication.
  • DATA are the 10 ID tag number.
  • Check Sum is the sum of the data for the purpose of detecting error.
  • CR is the Carriage Return and LF is Linefeed.
  • See first image above.
  • It’s designed to include 11 pins, which is shown in the diagram in the second image above. In order for the reader to work, it requires a power supply to the ground and digital pin that are connected to the Arduino for serial communication. It can draw around 65mA current and works within about 100mm range.

Step 3: ID-12LA RFID Kit - RFID Button


RFID Button: Having a diameter of 30mm, this button holds a unique 32 bit ID. This ID is read by the ID-12LA RFID, or any other device using 125kHZ module. The unique serial number of each button makes them useful as a key system, or to track individual objects. Refer to the second image in the previous step for more information.

 

Step 4: ID-12LA RFID Kit - ATMEGA32U2 USB Dev Board:


ATMEGA32U2 USB Dev Board: This device is made compatible for the RFID reader (ID-12) that we are using to do this tutorial. It contains an Atmega32u2 microcontroller and an extra pin that can be used to control other sensors. Also the board has an LED and Buzzer to indicate whether the reader made a scan or not.

We only used this board for connection purpose since the ID-12 has a large pin that makes it hard to connect on to a breadboard.

Step 5: Ping Ultrasonic Sensor

 


This sensor is used to measure distance by using ultrasonic waves. Ultrasonic describes sound that has a frequency above human ear limit, which is about 20,000Hz. The device can measure a distance from about 2cm (0.8in) to 3m (3.3yd). It works by sending an ultrasonic wave, and then provides an output pulse to approximate the time it took for the echoed wave to return to the sensor. The distance is the result of the rate of speed multiplied with how long it took for the wave to hit the object and to echo back to the sensor. These kinds of sensors are widely used in robotic applications.

Distance = Speed of Light * Time


Step 6: Hardware Assembly


1. Place a black and red wire on the breadboard. Connect the red wire to 5V on the Arduino while the black wire is connected to the GND.

 

Step 7: Hardware Assembly - continued

 

Step 8: Hardware Assembly - continued



3. Attach ID-12 reader to the Atmega32U2 USB Dev board and place it on the breadboard.
Then connect from the board to the Arduino/ breadboard as follows:
  • VCC => =5V
  • GND = > GND
  • TX => Pin 3


Step 9: Hardware Assembly - continued

 

4. Place the LEDs on the breadboard and connect the resistors from the GND to the short pin of the LED.
Connect the long pin to the digital pin 7 for the Red LED and digital pin 9 for the Green LED.

 

Step 10: Software Program

In this section we are going to program the Arduino to identify the specific person assigned for each RFID button.
First, we need a program to read the RFID button. After compiling and loading, we will use the unique numbers that were read from each RFID button for identification purposes.

 
/*Using RFID ID-12 to read unique character that is assigned to each RFID button or tag*/
char val = 0; //variable to store the char read from the RFID buttonvoid setup() {Serial.begin(9600); //connect to the serial port}
void loop () {//read the serial portif(Serial.available() > 0)val = Serial.read(); //read from one char from the ID-12 and store it Serial.print(val); //display the char in the serial monitor}}

At this point, we will be able to know each RFID button’s unique identifier.

 

Step 11: Software Program - continued

Since we already have the unique identifier from the earlier code, we are going to store it in the system. Once the user comes near to the ultrasonic sensor, it will ask for an ID. After the reader scans, check to make sure it matches the one stored in the system. The output will be displayed on the serial monitor as well as the ON and OFF functions of different LEDs to indicate matching.

 
/*This program uses ID-12 reader to read any RFID button or card, and Identify if this ID is known in the system*/
// include a header file to........... 
#include  SoftwareSerial mySerial(3,2); // virtual serial port //////////////////////////////////////////////////////////////////
 int R_LED= 7; // pin attached to the red LED
 int G_LED = 9;
 int LEDpin= 13;
int Reset = 12;
int Sonic_pin = 6; // pin from the ultrasonic sensor
/////////////////////////////////////////////////////////////
char val = 0; // what if it's byte val...how many is it storing
// because I already read and stored the ID's and I assigned it to the people
char id_tag1[] ="78003BDE66FB";
char id_tag2[] = "78003BF78B3F";
char* nam_tag[] = {"Derek", "Jay"};
char IDstring[13];
int i=0;  
int p =0;
////////////////////////////////////////////////////////////
void setup()
{
Serial.begin (9600);  // begin serial communication
mySerial.begin(9600);
pinMode (Sonic_pin, INPUT);
pinMode (R_LED, OUTPUT);
pinMode (G_LED, OUTPUT);
pinMode (Reset, OUTPUT);
pinMode (LEDpin, OUTPUT);
digitalWrite(LEDpin ,LOW);
}
 
void loop()
{
 //calculate the distance measured from the ping ultrasonic sensor until the target is less than 4cm away
  float distance;
  do{
  float time;
  // send out a pulse tone by creating
  pinMode(Sonic_pin, OUTPUT);
  digitalWrite(Sonic_pin, LOW);
  delay(2);
  digitalWrite(Sonic_pin, HIGH);
  delay(5); 
  digitalWrite(Sonic_pin, LOW);
  //let the ultrasonic sensor be an input to take back the echoed wave
  pinMode(Sonic_pin, INPUT);
  time = pulseIn(Sonic_pin, HIGH); // get the echoed duration in microsecond
  distance = SecToCm(time); // call function to calculate the distance
  delay(1000);} while (distance>4.0);
  
  Serial.println("Hello, please swipe your ID");
  //Serial.println(" ");
 delay(2000);
  //Open_Door();
 
   Red_tag();
   Iden_tag();
   resetID();
  Serial.println(" ");
}
 ////////////////////////////////////////////////////////////////////////////
void Access()
{
  // turns the green LED on if the ID is known to the system
  digitalWrite(G_LED, HIGH);
  delay(2000);
  digitalWrite(G_LED, LOW);
}
///////////////////////////////////////////////////////////////////////////////
void Denied()
{ 
  // turns the red LED on if the ID is not known to the system
  for(int i=0; i<3; i++)
  {
  digitalWrite(R_LED, HIGH);
  delay(1000);
  digitalWrite(R_LED, LOW);
  delay(300);
  }
}
//////////////////////////////////////////////////////////////////////////////
float SecToCm(float time)
{
  // convert the time measured into distance in centimeter
  //the ultrasonic read 29 microseconds per centimeter
 return time/29.0/2.0 ;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void Red_tag()
 { // if there is a radio frequency available from let the ID-12 read each char and store the value
   while (mySerial.available() >0)
   {
     //for(p=0;p<13;p++)
     {
     val= mySerial.read();
     IDstring[p] = val;
     //Serial.println(val);
     p++;
     }
   }
   p=0;
   delay(500);
  Serial.print("ID Number: ");
  delay(500);
  // print out on the serial monitor the RFID unique ID stored in string
    for ( i=0; i<13;i++)
{
  Serial.print(IDstring[i]);
}
 Serial.println();
 delay(1000);
 Serial.print("Please wait while checking");
 Serial.println();
 }
 ////////////////////////////////////////////////////////////////////////////////
 void Iden_tag()
{
   boolean reading = true; //to check if the ID is known
   boolean reading1 = false;// to check if the first ID is not known
   boolean reading2 = false;// to check if the second ID is not known
  
 for (int i=0; i<12;i++)// comparing the new button read to the ID 2 stored in the system
{
 if (IDstring[i+1] != id_tag2[i])
 {
  reading2= true; //indicate that the ID is not the same
  reading = false;
   // break;
 }
 
}
 
 if (reading==true)
 {
 delay (1500);
  Serial.println("Access granted");
  Serial.print("ID belongs to ");
  Serial.println(nam_tag[1]);
   Access(); // call a function to light up the green LED
 delay(2000);
  }
 reading = true;
 for (int i=0; i<12;i++)// why 12 not 13 try to figure out
{
 if (IDstring[i+1] != id_tag1[i])
  {
    reading1= true;
    reading = false;
    //break;
  } 
}  
{
 if (reading==true)
 {
   delay (1500);
  Serial.println("Access granted");
  Serial.print("ID belongs to ");
  Serial.println(nam_tag[0]);
  Access();
 delay(2000);
 }
 if (reading1== true && reading2 ==true)
  {
   delay(1000);
    Denied();
     Serial.println("Access Denied");
     Serial.println("Please try again ");
  }
}
}
///////////////////////////////////////////////////////////
void resetID()
{
 for(int i = 0; i < 13; i++){ 
    IDstring[i] = 0;
   }
}

Step 12: Code Explanation


The first part of the code includes the serial header file that is used to implement serial communication between the Arduino and RFID reader module. We are then going to create an object, in this case “mySerial”, to assign the communication pins. Then we define the global variable pin that is used by the LED and ping ultrasonic sensor. Also, as a global variable, we have created a character array to hold the two ID numbers that we got from the previous code as well as two names that are assigned for each ID number. These variables can be used by any function declard in this program.

Inside the setup function we have defined the serial communication between the computer and the Arduino as well as ID-12 and Arduino using same frequency band (9600). Then we defined which pin is used as an output or input. When we look at the loop function, the first thing we did was use another loop known as do while loop. Inside this loop, we calculated the distance at least once and checked if the target is located less than 4cm away. In order to calculate the distance, we first have to send out a pitched tone from the ultrasonic sensor. And this is done by sending Low-High-Low sequence by triggering digital pin 6. After it hit a target and returned back to the sensor, the sensor will output echo pulse. Using pulseln(), we can measure the echoed time pulse in microsecond and then converted it into distance. According to the datasheet for ping ultrasonic sensor, the speed of the sound is 340m/s and that means there are 29 seconds per centimeter. Calling out function Red_tag, we are going to scan and store any RFID button. In order to do this, we created a new array to hold on to each character. The Iden_tag function is going to compare the button that was stored on to the new array and the Id_tag created at the beginning of the code. Then we are going to see the output on the serial monitor as well as the LEDs.
 

Step 13: Results

 


Above is a screenshot of the serial monitor displaying the output.
If the detector doesn’t work after running the code, check the connection as well as any mistyping when writing the code. As we conclude this tutorial, we can now use this project for monitoring any object with a RFID tag.

Ready to get started? DFRobot online store has all the products you need to create your own project. While you're there, check out the other tutorials we have available for you! Also, if you haven't already, explore more projects here.

If you have any questions about this tutorial, do not hesitate to post a comment, shoot us an email, or post it in our forum.
Thanks for reading!

Source: Instructables
 

REVIEW