This blog is by the Little Bird Electronics team.

Moving Forward with Arduino – Chapter 15 – RFID Introduction

by John Boxall

 

This is part of a series originally titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Welcome back fellow arduidans!

In this instalment we will start to examine RFID systems

Please note that we will not be discussing the use of 13.56 MHz RFID systems such as NXP “Mifare Classic”. I’m not going to teach you how to clone stored-value public transport fare cards.

First of all, RFID – radio frequency identification. Some of us have already used these things, and they have become part of everyday life. For example, with electronic vehicle tolling, door access control, public transport fare systems and so on. Personally I thought the electronics behind RFID systems was quite complex, and had even procrastinated with examining them myself… however finally I ordered some parts in and had them working almost no time. (There’s a good lesson in that for all of us).

To explain RFID for the layperson, we can use a key and lock analogy. Instead of the key having a unique pattern, RFID keys hold a series of unique numbers which are read by the lock. It is up to our software (sketch) to determine what happens when the number is read by the lock.  The key is the tag, card or other small device we carry around or have in our vehicles. We will be using a passive key, which is an integrate circuit and a small aerial. This uses power from a magnetic field associated with the lock. Here are some key or tag examples:

Our lock is a small circuit board and a loop aerial. This has the capability to read the data on the IC of our key, and some locks can even write data to keys. Here is our reader (lock) example:

As you can see from the 5mm graph paper, the circuitry is quite small, and the loop is somewhat fragile. For installation and use, it would be wise to mount the loop aerial inside something strong and protective. But more about that later. The RFID system we will examine works on the 125 kHz frequency, but that is not so relevant at this stage.

Our use for the RFID equipment is to have our sketch make a decision based on the unique tag number. For example, it could be used as a switch to turn on and off something, perhaps an alarm system or a computer. It could control an electric door strike (lock), or activate a series of lights to one’s personal preference. The possibilities are only limited by your imagination. I hope that with your existing knowledge you can implement this RFID equipment into your next prototype or product.

Example 15.1

First of all, let’s do a basic test – what happens when we read a tag?  To do this we need to connect our reader to the Arduino or compatible board, and see what comes out when we read a card. The connections are quite simple:


From the reader data sheet.pdf (our version is the TTL model), the reader sends out serial data from it’s TX pin at 9600 bps. We will read that data using the serial input (digital pin zero on the duemilanove compatible board) and display it on the serial monitor box to see what it looks like. The LED activates when reading is taking place. Here is the sketch to use: example 15.1.pde.

Once the sketch has been uploaded, open your serial monitor box, and wave a tag over the antenna. You should have a reading similar to the video below, however your tag number will be different.


Excellent – simple numbers that we can work with. For example, one of my tags returns: 2,51,69,48,48,49,65,51,53,70,50,69,51,3 and another returns 2,51,67,48,48,67,69,49,48,68,53,51,55,3. Note that both start with 2 and end with 3, so the unique tag details are the 12 integers between the 2 and 3. One could read the data as characters or hexadecimal numbers by altering the data type in the sketch from int to byte, but for simplicity I am working in integers.

Now all we need to do is fashion sketches to recognise the tag number(s) we want to use, and perform an action based on which tag number is used (or do something when a tag is read, but not the tag you want).

Example 15.2

In the following example, (example 15.2.pde) the sketch reads the 14 integers returned from the card reader when a tag is swiped. These integers are placed into a fourteen element array, which is then compared against arrays holding the numbers my “allowed” tags. If an allowed tag is read, the green LED comes on, if a disallowed tag is read, the red LED comes on. Of course you could have the digital outputs controlling other things using a switching transistor or a relay. Below is the schematic:

The hardware laid out:

And a short video in action:


Excellent – now we are getting close to something useful. The example above could make a simple door control, or an over-engineered cookie jar.

Speaking of cookies, time for a cup of tea…

Now for some more practical uses of RFID and Arduino. In the past we have worked with real time in may chapters, and it is very simple to store data using amicroSD card shield. So:

Example 15.3

We will build on our previous example by adding time and date logging for all accesses to the system, successful or not. This could be used again for door access, payroll calculations as a modern-day punch-clock, or even a simple logging device to see what time the children arrive home when you aren’t around to check. So we will need a microSD shield, and some sort of DS1307 breakout board or shield.

When using more than one shield together, be mindful of the pins you will need to use. For example, my DS1307 shield uses analogue 4 and 5 (for I2C interface), and the microSD shield uses digital 10 to 13.

The sketch for this example is quite simple – the good thing about programming for Arduino is that just like the hardware shields, sketch procedures and functions can be very modular and reused quite easily. If you are unsure about the microSD shield, please review my review. Most of the code can be copied from that microSD shield article’s demonstration sketch, which I have done for this example. The sketch writes the time, date, tag number, and read status (accepted/rejected).

However there is one caveat when using the microSD shield – the file needs to be closed before the card can be removed for examination. In practical use, our RFID system would be usually on most of the time, so a method will needed to activate the card write function. This has been implemented with a functionbossMode() that is called when a certain tag is read – one may call this the supervisor’s card. Once this particular tag is read, the file is annotated as closed, reading stops, and the LEDs blink alternately when it is safe to remove the card. A simple reset after the card is reinserted will start the reading again. Here is the sketch,example 15.3.pdf – and a hardware layout.

The schematic is the same as example 15.2, with a few simple additions – the use of the microSD card shield, and the DS1307 real time clock shield. If you are using a DS1307 breakout board wired in separately, please use the following schematic as a guide:

Now here is a short video clip, with the addition of the ‘boss mode’ shutdown sequence:


And finally, here is a copy of the text file that was produced from a recent test run:

As you can see, it is easy to reproduce expensive time-keeping systems with our own equipment and some time. In the near future we will return to RFID and build some self-contained units with an LCD display (I need to get some more LCD units!)

Thus concludes our introduction to RFID systems. However, stay tuned for more about this and other interesting things! If you have any requests, don’t hesitate to ask.

Please subscribe (see the top right of this page) to receive notifications of new articles. High resolution photos are available from flickr.

If you have any questions at all please leave a comment (below). We also have a Google Group dedicated to the projects and related items on the website – please sign up, it’s free and we can all learn something. If you would like to showcase your work from this article, email a picture or a link to john at tronixstuff dot com. You might even win a prize!

Don’t forget to check out the range of gear at Little Bird Electronics!

So have fun, stay safe and see you soon for our next instalment, hopefully by the 4th of September 2010.