In this lesson, we take a look at Radio Frequency Identification, RFID for short. These systems use data strings stored inside special tags (or transponders) to uniquely identify people or objects when they are scanned by an RFID reader. These types of systems are found in many applications such as library books, animal identification, inventory control systems, and secure access control systems.
[caption align="aligncenter" width="500"]
Phidgets RFID Quick Start Kit[/caption]
When the reader is powered up (plugged into a powered USB port in the case of PhidgetsRFID), it gives power to a large coil. The coil creates an external magnetic field which can then be paired with a coil inside a nearby tag. This delivers a small amount of power wirelessly to the tag. With that power, the tag is able to access a small internal memory bank and transmit a key string back to the reader via modulation on the wireless signal.
In order for an RFID reader, like the
Phidgets RFID Quick Start Kit, to communicate with an RFID tag, they must share a common protocol. This protocol acts as a set of rules for the way data is transmitted wirelessly between the reader and tag. It's common for people to assume that an RFID tag and reader need only share the same operating frequency to be compatible, but they also need to use the same communication protocol. Check the user guide for your device to determine which protocols it supports.
Now, if you have a reader and some tags, you're ready to set something up. For this tutorial, we will show you how to set up a
Phidgets RFID Quick Start Kit.
- Plug the RFID reader into a computer that has the Phidgets drivers installed (see Lesson 1 for instructions). Attach a green LED anode (long lead) to the output labelled 0 and the red LED anode to the output labelled 1. Connect both cathodes (short lead) to ground. Note: It's a good idea to put a 10-13Ω resistor between the green LED anode and output labelled 0, depending on the forward voltage of your LED. It might work without the resistor, but since you're running 5V through the LED, it will burn out pretty soon.
- Download the sample code for your programming language. For this lesson, we'll be working in C++, but the concepts are valid in any language.
- Change the tag handler functions (
TagHandler
and TagLostHandler
) to respond in the way you want. For this program, a function called Lookup(char*)
will look in a file of approved tag values and return TRUE if the tag value is found in the file, and FALSE otherwise. If the tag is approved, then a green light on output 0 comes on, otherwise output 1 (a red light) comes on. When the tag is moved away, the output changes back to 0. The functions look like this:
int CCONV TagHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto)
{
//turn on the Onboard LED
CPhidgetRFID_setLEDOn(RFID, 1);
if ( Lookup(TagVal) ) // the tag value is valid
{
CPhidgetRFID_setOutputState(RFID, 0, 1);
}
else // the tag value is invalid
{
CPhidgetRFID_setOutputState(RFID, 1, 1);
}
printf("Tag Read: %s\n", TagVal);
0;
}
int CCONV TagLostHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto)
{
//turn off the Onboard LED
CPhidgetRFID_setLEDOn(RFID, 0);
//turn off the digital outputs
CPhidgetRFID_setOutputState(RFID, 0, 0);
CPhidgetRFID_setOutputState(RFID, 1, 0);
printf("Tag Lost: %s\n", TagVal);
return 0;
}
... and
Lookup
looks like this:
int Lookup(char *value)
{
char line[128]
char check[128]
FILE * db;
strcpy(check, value);
check[strlen(value)] = '\n'; // reading the file I have set up puts a null terminoator at the end,
// where as the tag when it's read does not have the null terminator
db = fopen( "goodtags.txt", "r" );
while (fgets (line, 128, db) != NULL)
{
if ( strcmp (line, check) == 0 )
{
return 1;
}
}
fclose (db);
printf("Tag not found, sorry.\n");
return 0;
}
For a larger database of tags, you'll want to implement a more sophisticated storage system and search algorithm, but this is just meant to show you how RFID can work. One place to go from here is to connect a 5V dead bolt mechanism to output 0 to unlock a door when an approved tag is scanned. In reality, a tag can trigger any number of things.
The PhidgetRFID can also write values to RFID tags. You'll need to use T5577 type tags. You can then use the
CPhidgetRFID_write(CPhidgetRFIDHandle RFID, char* tagString, CPhidgetRFID_Protocol protocol, int lock)
function. There are three protocols supported, which are given by the following enumerations (for the protocol parameter):
- PHIDGET_RFID_PROTOCOL_EM4100 - EM4100 (EM4102) is a 40-bit protocol
- PHIDGET_RFID_PROTOCOL_ISO11785_FDX_B - ISO11785 FDX-B encoding is primarily used for animal identification
- PHIDGET_RFID_PROTOCOL_PHIDGETS - PhidgetTAG is a Phidget specific protocol and allows a 24 character ASCII value
We can then create a program and modify the
TagHandler
function to write new tags:
int CCONV TagHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto)
{
//turn on the Onboard LED
CPhidgetRFID_setLEDOn(RFID, 1);
printf("Tag Read: %s\n", TagVal);
fflush(stdout);
printf("Would you like to re-write the tag value (Y/N)?");
char ans = getchar();
fpurge(stdin);
if (ans == 'y' || ans == 'Y')
{
char newtag[24] // maximum 24 ascii characters allowed
printf("\nPlease enter a new tag value: ");
fgets(newtag, 24, stdin);
fpurge(stdin);
if (CPhidgetRFID_write(RFID, newtag, PHIDGET_RFID_PROTOCOL_PHIDGETS, PFALSE))
{
printf("\nTag written successfully.\n");
CPhidgetRFID_setOutputState(RFID, 0, 1);
}
else
{
printf("\nWrite tag failure.\n");
CPhidgetRFID_setOutputState(RFID, 1, 1);
}
}
return 0;
}
Alternatively, you can use the Phidget Control Panel (available on Mac and Windows) to write new tags:
Using the Phidget Control Panel to write an RFID tag
Now that you can read and write with the PhidgetRFID, it's good to mention a couple other characteristics of RFID:
- Tags come in two main varieties: passive and active. Active tags have their own power supply, which they use to power an antenna to broadcast their data. Passive tags derive the power they require to operate directly from the radio frequency output of the RFID reader, and no other power supply is necessary. We've been using passive tags here. These tags are cheaper to produce and much more suitable for common applications whereas active tags are used in situations where very large read distance is desirable (train cars, for example, are one of the few places active tags are used).
- Multiple RFID tags within 1 to 2 meters will interfere with each other. The best way to overcome this is in software by enabling the antennae of individual RFID readers in sequence.
- While some RFID readers offer the capability to read multiple tags at once, the majority do not. In order to read a tag, any other tags must first be removed from the reader's field.
RFID are great in object tracking applications, and are often integrated with databases. You can use the basics in this tutorial to build many RFID projects, so have fun!