In this project, you’ll connect Arduino directly to the RFID reader. This project accomplishes the same thing as the project in Chapter 2, but the reader is connected to an Arduino, not to your computer. As a starter step, you’ll see how to read in an RFID tag’s ID and send its value to a computer over the serial port. After you’ve done that, you’ll see how to read in an RFID tag’s ID and compare it to a stored tag ID: if you wave the right tag at the RFID reader, it will light an LED. In this way, the RFID tag will behave as a key.
Some RFID tags have many well-documented vulnerabilities. Certain types of RFID tags can be cloned easily, for example. So in theory, if an attacker gets close enough to your RFID tag (or you code) to determine the ID of the tag, they may be able to create a copy of it.
The Arduino Uno is a good model of Arduino to get started with for all the microcontroller-based projects in this book.
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.
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 circuit for this reader is similar to the one from Chapter 2. Connect the module to the Arduino’s 5V and ground connections, and connect the reader’s serial transmit line (labeled SOUT) to digital pin 6 on the Arduino, which we’ll enable as a secondary serial port by using Arduino’s SoftwareSerial library (the Arduino Uno has a primary serial port that we’ll use to send messages to the computer). You’ll also need to attach the enable pin to ground. Figure 3-1 shows these connections.
This sketch reads in bytes similar to the Processing sketches shown in Chapter 2. Upload it to your Arduino, launch the Arduino Serial Monitor (Tools→Serial Monitor), and make sure the Serial Monitor is configured for 9600 bps. Next, bring an RFID tag within range of the reader, and you should see the tag ID appear in the Serial Monitor window.
/* RFID Reader */ #include <SoftwareSerial.h> // Bring in the software serial libraryconst int tagLength = 10; // each tag ID contains 10 bytes
const int startByte = 0x0A; // Indicates start of a tag const int endByte = 0x0D; // Indicates end of a tag char tagID[tagLength + 1]; // array to hold the tag you read
const int rxpin = 6; // Pin for receiving data from the RFID reader const int txpin = 7; // Transmit pin; not used SoftwareSerial rfidPort(rxpin, txpin); // create a Software Serial port
void setup() { // begin serial communication with the computer Serial.begin(9600); // begin serial communication with the RFID module rfidPort.begin(2400); } void loop() { // read in and parse serial data: if (rfidPort.available() > 0) { //
if (readTag()) { //
Serial.println(tagID); } } } /* This method reads the tag, and puts its ID in the tagID */ boolean readTag() { char thisChar = rfidPort.read(); //
if (thisChar == startByte) { //
if (rfidPort.readBytesUntil(endByte, tagID, tagLength)) { //
return true; } } return false; }
Here’s how the code works:
This line imports a library called SoftwareSerial, which allows you to use any pair of digital pins as a serial port. It’s not as robust as the built-in hardware serial port (pins 0 and 1), but it is well-behaved at low transmission rates, which is perfect here, since we’re communicating with the RFID reader at 2400 bits per second.
These lines of code declare several constants used throughout
the sketch. The first, tagLength
,
is the number of characters in the RFID tags that the Parallax reader
can process. The second, startByte
,
is the value of the character that the RFID reader sends when it’s
beginning to transmit a tag ID. And the last, endByte
, is what the RFID reader uses to
signal that it’s done transmitting a tag ID.
This line declares a buffer (tagID
) that’s big enough to hold the tag ID,
along with an extra byte at the end to hold the character (zero) that
terminates a character array. This way, when you use the println
command to display the tagID
later, the zero will signify to
Arduino that it’s reached the end of the string.
This initializes the Software Serial session with pin 6 as the
receiving pin, and 7 as the transmitting pin. Since you don’t transmit
anything to the RFID reader, you don’t need to hook pin 7 up. In the
setup()
module, you’ll see that the
code calls begin()
on both the
built-in serial port (to talk to the computer and this port that you
just initialized).
This expression checks the Software Serial port to see if any messages are coming in from the RFID reader.
If the previous expression evaluated to true, this line calls
the readTag()
function. If that
returns true, the next line (println()
) displays the tag to the built-in
serial port, which makes it appear on the Serial Monitor’s display.
This line reads one character from the RFID reader, and stores
it in thisChar
.
If thisChar
is equal to the
startByte
indicator, the next line
is run.
The readBytesUntil
function
will read bytes from the RFID reader until it hits the endByte
delimiter, and it stores the result
into tagID
. If it succeeds in
reading tagLength
(10) bytes from
the RFID reader, this function returns true.
Here’s the sort of output you’ll see in the Serial Monitor as you bring different tags in range (since you have different tags than I do, you’ll see different values there). Because the Parallax reader transmits a tag ID continuously while it is in range, you’ll see the ID repeated as long as you hold it next to the reader:
04162F7CAC 04162F7CAC 0415EA09BE 0F02A684B1 0F02A684B1 0F02A684B1
With the same circuit, and just a few changes to the code, you can
make the Arduino take action only when a certain tag comes within range.
Before you try this, you’ll need to run the sketch from the previous
section, and copy the ID of the tags you want to match. For this example,
I’ll use 04162F7CAC
, but you will need
to use a tag ID from your collection of tags.
Here’s the modified sketch, which lights the Arduino’s built-in LED when the right tag is brought within range of the reader:
/* RFID Reader */ #include <SoftwareSerial.h> // Bring in the software serial library const int tagLength = 10; // each tag ID contains 10 bytes const int startByte = 0x0A; // Indicates start of a tag const int endByte = 0x0D; // Indicates end of a tag char tagID[tagLength + 1]; // array to hold the tag you read const int rxpin = 6; // Pin for receiving data from the RFID reader const int txpin = 7; // Transmit pin; not used SoftwareSerial rfidPort(rxpin, txpin); // create a Software Serial portString matchingTag = "04162F7CAC"; // The tag to match
![]()
const int ledPin = 13; // The digital pin for the built-in LED
void setup() { // begin serial communication with the computer Serial.begin(9600); // begin serial communication with the RFID module rfidPort.begin(2400);
pinMode(ledPin, OUTPUT); // enable the pin for output
} void loop() { // read in and parse serial data: if (rfidPort.available() > 0) { if (readTag()) { Serial.println(tagID);
if (matchingTag.equals(tagID)) { //
![]()
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn it off
} } } } /* This method reads the tag, and puts its ID in the tagID */ boolean readTag() { char thisChar = rfidPort.read(); if (thisChar == startByte) { if (rfidPort.readBytesUntil(endByte, tagID, tagLength)) { return true; } } return false; }
Here’s what those new lines of code do:
This is an Arduino String
variable that holds
the ID of the tag you’re trying to match.
This constant specifies which digital pin to use to light the LED. Digital pin 13 corresponds to the LED that’s built into the Arduino board.
This configures the pin so you can use it for output.
This line checks to see if the tag ID you just read from the reader matches the one you seek.
If so, it lights the LED by writing HIGH
to it.
If not, it turns it off by writing LOW
.
These last few lines of code didn’t add a lot, but they opened the door to a powerful new capability: taking action in the physical world based on the bytes you received from the RFID reader. In Chapter 4, you’ll see how to go from turning on an LED to turning on an actual light (or any other electrically-powered device).