Thursday, August 11, 2016

RFID Arduino Program


INTRODUCTION:

RFID – radio frequency identification. Some of us have already used these things, and they have become part of everyday life. For example, with electronic vehicle tolling, door access control, public transport fare systems and so on. It sounds complex – but isn’t.

To explain RFID for the layperson, we can use a key and lock analogy. Instead of the key having a unique pattern, RFID keys hold a series of unique numbers which are read by the lock. It is up to our Arduino sketch to determine what happens when the number is read by the lock. The key is the tag, card or other small device we carry around or have in our vehicles. We will be using a passive key, which is an integrated circuit and a small aerial. This uses power from a magnetic field associated with the lock. Here are some key or tag examples:







In this experiment we’ll be using 125 kHz tags – for example. To continue with the analogy our lock is a small circuit board and a loop aerial. This has the capability to read the data on the IC of our key, and some locks can even write data to keys. Here is our reader (lock) example:







MATERIALS NEEDED:

  • 1 PC 
  • 1 Keyboard and 1 Mouse 
  • 1 Wireduino Board 
  • 1 USB cable 
  • 1 Breadboard 
  • 1 RFID Reader Package 
  • 16 Some RFID tags

PROCEDURE:

Simply insert the RFID reader main board into a solderless breadboard as shown below. Then use jumper wires to connect the second and third pins at the top-left of the RFID board to Wireduino 5V and GND respectively. The RFID coil connects to the two pins on the top-right (they can go either way). Finally, connect a jumper wire from the bottom-left pin of the RFID board to Wireduino digital pin 2:



Next, upload the following sketch to your Arduino and open the serial monitor window in the IDE:

#include <SoftwareSerial.h>

SoftwareSerial RFID(2, 3); // RX and TX

int i;
void setup()
{

RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
}
void loop()
{
if (RFID.available() > 0)
{
i = RFID.read();
Serial.print(i, DEC);
Serial.print(",");

}
}

As you can see from the experiment, the reader returns the card’s unique ID number which starts with a 2 and ends with a 3. While you have the sketch operating, read the numbers from your RFID tags and note them down, you will need them for future sketches.

To do anything with the card data, we need to create some functions to retrieve the card number when it is read and place in an array for comparison against existing card data (e.g. a list of accepted cards) so your systems will know who to accept and who to deny. Using those functions, you can then make your own access system, time-logging device and so on.

Let’s demonstrate an example of this. It will check if a card presented to the reader is on an “accepted” list, and if so light a green LED, otherwise light a red LED. Use the hardware from the previous sketch, but add a typical green and red LED with 560 ohm resistor to digital pins 13 and 12 respectively. Then upload the following sketch:


#include <SoftwareSerial.h>

SoftwareSerial RFID(2, 3); // RX and TX


int data1 = 0;
int ok = -1;
int yes = 13;
int no = 12;


// use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers

int tag1[14] = {2,52,48,48,48,56,54,66,49,52,70,51,56,3};
int tag2[14] = {2,52,48,48,48,56,54,67,54,54,66,54,66,3};
int newtag[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons

void setup()

{

RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
pinMode(yes, OUTPUT); // for status LEDs
pinMode(no, OUTPUT);

}
boolean comparetag(int aa[14], int bb[14])
{

boolean ff = false;
int fg = 0;
for (int cc = 0 ; cc < 14 ; cc++)

{
if (aa[cc] == bb[cc])
{

fg++;

}

}

if (fg == 14)

{

ff = true;

}

return ff;

}


void checkmytags() // compares each tag against the tag just read

{

ok = 0; // this variable helps decision-making,
// if it is 1 we have a match, zero is a read but no match,
// -1 is no read attempt made
if (comparetag(newtag, tag1) == true)
{

ok++;

}

if (comparetag(newtag, tag2) == true)
{
ok++;
}

}

void readTags()
{

ok = -1;
if (RFID.available() > 0)
{

// read tag numbers
delay(100); // needed to allow time for the data to come in from the serial buffer.

for (int z = 0 ; z < 14 ; z++) // read the rest of the tag

{
data1 = RFID.read();
newtag[z] = data1;
}

RFID.flush(); // stops multiple reads
// do the tags match up?
checkmytags();

}

// now do something based on tag type
if (ok > 0) // if we had a match
{

Serial.println("Accepted");
digitalWrite(yes, HIGH);
delay(1000);
digitalWrite(yes, LOW);
ok = -1;

}

else if (ok == 0) // if we didn't have a match

{

Serial.println("Rejected");
digitalWrite(no, HIGH);
delay(1000);
digitalWrite(no, LOW);

ok = -1;

}
}

void loop()

{

readTags();

}



No comments:

Post a Comment