Chapter 2. Reading RFID Tags in Processing

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.

RFID reader

Parallax’s RFID Reader Module, available from Maker Shed as part of a starter pack or by itself.

RFID tags

The starter pack includes several tags, and you can buy them separately.

USB-to-TTL serial adaptor

The FTDI Friend can do the job.

Breadboard

You can use a half-size or mini breadboard to make connections between the reader and the USB-to-TTL serial adaptor.

Jumper wire

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.

Note

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 library 1
import 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 2
  println(Serial.list());

  // based on the list of serial ports printed from the
  // previous command, change the 0 to your port's number 3
  String portnum = Serial.list()[0];

  // initialize the serial port 4
  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 5
  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) { // 6
  tagID = trim(rfidPort.readString());
}

Here’s an explanation of the key parts of the code:

1

This line imports the serial library that comes with Processing. With it, you’ll be able to use serial functions later in this sketch.

2

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.

3

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.

4

This line begins serial communications at 2400 bits per second.

5

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.

6

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.