RFID Reader

RFID stands for Radio Frequency Identification.  The Parallax RFID Reader Serial + Tag Sampler includes several unique RFID tags. This tutorial will show you how connect the RFID Card Reader to the Propeller microcontroller.  Then, it will show you how to read a tag’s ID number and use that data in a program.

 

Circuit

In addition to 5V and ground, the RFID Card Reader – Serial requires two Propeller I/O pin connections.  P2 will be set to output, to set the RFID Reader’s Enable pin.  P1 will be set to input, to read the serial data coming from the RFID Reader. A 2.2 k-ohm resistor is used for this connection since the reader outputs a 5 V signal and the Propeller chip is a 3.3 V device.

Parts

  • Build the RFID Reader circuit following the wiring diagram and schematic below. 

This circuit and test code will work with any Propeller development board that has a prototyping area.

 

Test Code

Library Alert! This example code and library requires the Learn folder released 5/15/2014 or later.  Update your Learn Library.

  • Click SimpleIDE’s Open Project button.
  • Open RFID Read.side from ...Documents\SimpleIDE\Learn\Examples\Devices\Sensor\RFID Serial 
  • Click the Run with Terminal button.
  • Hold each RFID tag near the RFID Reader.

Each tag’s ID number should appear in the SimpleIDE Terminal. If no tag is detected within one second, you will see “timed out” until a tag is detected.

 

How it Works

First, the program includes the simpletools and rfidser libraries.  The rfidser library gives us access to the rfid_open and rfid_get functions, along with other conveniences for communicating with the RFID reader.

#include "simpletools.h"                      // Include simpletools
#include "rfidser.h"                          // Include rfidser

Next, the program declares two int values, giving names to the two Propeller I/O pins that are connected to the RFID Reader.  Later, these names will be passed to parameters in a function call.

int rfidEn = 2;                               // Reader /ENABLE pin to P2
int rfidSout = 1;                             // Reader SOUT pin to P1

The next line creates a device identifier for the RFID reader; we named it rfid.    The device identifier is a nickname for a memory structure being set aside to store information about this particular RFID reader connection.  If you are using more than one RFID reader in your project, you would give each one its own identifier using rfider *nickname.

rfidser *rfid;                                // Set up device ID

Inside the main routine, the rfid_open function call starts the RFID card reading process in another cog.  Notice that the I/O pin names declared above are passed to the rfid_open function’s parameters.  The function returns information that gets stored in the memory structure we nicknamed rfid.  Now, rfid is ready to use with other function calls. 

int main()                                    // Main function
{
  rfid = rfid_open(rfidSout, rfidEn);         // Open reader, start reading

The last two lines are inside an infinite while loop. The first line gets the tag ID from the reader (if one is detected) and the second line prints the tag ID in the SimpleIDE Terminal.

  while(1)                                    // Main loop
  {
    char *str = rfid_get(rfid, 1000);         // Wait up to 1 s for card
    print("id = %s.\n", str);                 // Print ID.
  } 
}

Let’s look closer at those two lines.  The function call rfid_get(rfid, 1000) is listening to the device we named rfid for 1000 ms.   This function returns the tag’s ID number if one is present, or the phrase “timed out” if no ID number is received within 1000 ms.    On the other side of the equal sign, char *str names a pointer to a place in Propeller memory to start storing the characters returned by the function call.

The print function call uses the %s formatter to display str.  This formatter will print a character string stored in memory, starting at the address named str.  The character string will display as a tag ID or the phrase “timed out” on a new line each time through the loop.

 


Did You Know?

Passive Tags — These tags are passive, meaning they do not have a power source of their own inside. Instead, an embedded antenna circuit inside the tag gathers a tiny bit of power from a magnetic field generated by the Reader.  Holding the Reader close to the tag powers it just enough to send its ID number back to the Reader. 

Keep it In the Family — The Parallax RFID Card Reader works with the EM4100 family of read-only tags.  This reader will not work with tags from other families, nor can it write data to RFID tags.  


 

Try This

When RFID tags are used in access systems, the usual goal is to find out if a tag’s ID is known to the system, so the appropriate action can be taken.  The next example program uses if...then...else to take a different actions based on a tag’s ID.  First, you will need to write down your tags’ ID numbers.  

  • Label your tags “A” and “B.”
  • Click SimpleIDE’s Open Project button.
  • Open RFID Read and Compare.side from ...Documents\SimpleIDE\Learn\Examples\Devices\Sensor\RFID Serial.
  • Click Run with Terminal, and then hold a tag up to the reader.  You should see the message “Unknown ID” followed by the tag’s number.
  • Click the Disable button in the SimpleIDE Terminal, then write down the tag’s ID number.

  • Now, update the lines shown below with the ID numbers from your own tags.

  • Click Run with Terminal again. The Serial Terminal should now recognize each tag.  If it doesn’t, re-check your typing and try again!

 

Your Turn

Now that the program recognizes both tag IDs and takes separate actions for each, you can modify those actions to make the application more interesting.  The rfidser library has an rfid_disable function that will make the RFID reader ignore tags until you either re-run the program or call the rfid_enable function. Let’s modify the program to go into lock-down if Tag B is detected.

  • After the second else...if statement, replace the print statement with break;
  • Below the closing bracket of the while(1) block, add these two lines:
Rfid_disable;
Print(“Banned user detected – system in lockdown.\n”);
  • Now re-run the program. Try Tag A to make sure the reader is still reading. Then try Tag B. Does it stop the whole show?

Disable vs Close, Enable vs Open.  rfid_disable does not shut down the Propeller core running the RFID reading process that was launched with rfid_open.  Reading can resume with rfid_enable.  If you want your application to stop reading tags and also free up the Propeller core and memory that it is using, shut down the process entirely with rfid_close.   If your project needed to read tags again after that, your program would need to call rfid_open to re-start the RFID reading process again in another core.