In this project, you’ll read some RFID tags and get a sense of how the readers behave. You’ll see how far away from your reader a tag can be read. This is a handy test program for use any time you’re adding RFID to a project.
Parallax’s RFID Reader Module, available from Maker Shed as part of a starter pack or by itself.
The starter pack includes several tags, and you can buy them separately.
The FTDI Friend can do the job.
You can use a half-size or mini breadboard to make connections between the reader and the USB-to-TTL serial adaptor.
You’ll need a set of jumper wire to make your connections.
The Parallax reader is one the simplest readers available. It communicates serially at 2400 bps. When the Enable pin is held low (connected to ground), it sends a reading whenever a tag is in range. The tag ID is a 12-byte string starting with a carriage return (ASCII 13) and finishing with a newline (ASCII 10). The ten digits in the middle are the unique tag ID. The EM4001 tags format their tag IDs as ASCII-encoded hexadecimal values, so the string will never contain anything but the ASCII digits 0 through 9 and the letters A through F.
The circuit for this reader is very simple. Connect the module to 5V and ground, and connect the reader’s serial transmit line (labeled SOUT) to the serial adaptor’s serial receive line (labeled RX). You’ll also need to attach the enable pin to ground. Figure 2-1 shows these connections.
The following Processing sketch waits for twelve serial bytes, strips out the carriage return and the newline, and prints the rest to the screen. Before you run this sketch, plug the FTDI Friend into your computer with a USB Mini cable.
You will probably need to look at the output of
Serial.list()
and change the number on the next line
of code match the serial port that corresponds to your
microcontroller.
/* Parallax RFID Reader language: Processing */ // import the serial libraryimport processing.serial.*; Serial rfidPort; // the serial port you're using String tagID = ""; // the string for the tag ID void setup() { size(600, 200); // list all the serial ports
println(Serial.list()); // based on the list of serial ports printed from the // previous command, change the 0 to your port's number
String portnum = Serial.list()[0]; // initialize the serial port
rfidPort = new Serial(this, portnum, 2400); // incoming string from reader will have 12 bytes: rfidPort.buffer(12); // create a font with the third font available to the system: PFont myFont = createFont(PFont.list()[2], 24); textFont(myFont); } void draw() { // clear the screen: background(0); // print the string to the screen
text(tagID, width/4, height/2 - 24); } /* this method reads bytes from the serial port and puts them into the tag string. It trims off the \r and \n */ void serialEvent(Serial rfidPort) { //
tagID = trim(rfidPort.readString()); }
Here’s an explanation of the key parts of the code:
This line imports the serial library that comes with Processing. With it, you’ll be able to use serial functions later in this sketch.
This line will print all the available serial ports to the Processing console. You should examine the output of this command and identify which of your serial ports corresponds to the FTDI Friend that’s plugged into your computer.
If the output of the previous line of code was anything other than the first serial port (index 0), change the 0 in this line to the index of the correct serial port.
This line begins serial communications at 2400 bits per second.
Processing’s draw()
function
runs continuously as long as the sketch is running. This line will
display whatever’s in the tagID
variable to the screen, even if it’s still blank.
This function is invoked any time there’s some incoming activity
on the serial port. When the Parallax reader sends something to the
Processing sketch, it will be a tag id. This line puts that tag’s ID
into the tagID
variable.
Figure 2-2 shows the results of holding a tag up to the reader while this Processing sketch is running.