Raspberry Pi RFID tag reader

This article explains how to add an RFID tag reader to your Raspberry Pi. With a tag reader you can read the unique ID numbers from RFID cards and even your Oyster card, and make things happen on your Raspberry Pi depending on which card is wanded over it.

What is RFID?

RFID stands for “Radio Frequency IDentification”. It is a near-field communications standard that has been used for many years on Oyster cards used with the London Underground, and many other identification based systems. You hold a tag or card in close proximity to a reader, and the reader reads a unique ID number stored in that tag to open a door or perform some action for you.

There are many different types and standards of RFID, and the one used here is the Mifare standard. This is the standard that was adopted by London Underground for the Oyster card, and is also used on various contactless payment systems.

The development of the RFID standard lead on to the original idea of “The Internet of Things”. If “Things” all have a unique ID, you can track those things as they move, and computers can make their own decisions about the world, rather than relying on humans to tell them what is happening. Read the fascinating article in ref[8] to find out more about the British inventor Kevin Ashton who was instrumental in the development of RFID, and who is attributed with coining the phrase “The Internet of Things”.

Reading Tags and Cards

To build this project you need an SL030 RFID tag reader, and a connecting lead from skpang, product numbers RFID-SL030 and RSP-SL030-CAB. You will also need some tags, you can get these from skpang and I like to use the MICARD-1K. Links to these and other products are at the end of this article.

Download the “example code” zip file from the links below. You can either unzip this from the file manager by right-clicking on the icon and choosing “unzip here”, or you can use the unzip command from an LXTerminal prompt.

Once the files are unzipped, plug the connecting cable between the raspberry pi and the SL030, making sure that you put both connectors the right way round. The Raspberry pi end must be plugged in so that the red wire lines up with the P1 corner pin marked on the raspberry pi circuit board. At the SL030 end, the red pin has to line up with the pin labelled as VCC.

Now open LXTerminal and use the cd command to change into the directory that has the rfid_example.py program. You can run this example either with Python version 2, or Python version 3. I prefer to use Python 2, but I have tested this program with both versions. The program uses the GPIO pins of the Raspberry Pi to communicate with the reader, so you have to use the sudo command – (if you don’t understand why, there is a link to an article about using sudo in the links at the end).

Now wand your tag over the reader and it should say “card present” and then show you the number of that card.

How Does it Work?

RFID cards don’t have any power source inside them, but they have a small silicon chip and a fairly large aerial. When the card is brought near to the reader, the reader is transmitting a radio signal, and this induces a small current into the aerial in the tag or card. There is just enough power induced in this aerial to power up the chip inside the tag or card.

At time of manufacture, each tag or card is programmed with a unique number that identifies it. Every card in the world has a different number. Now, the reader transmits a known waveform through it’s aerial, and this is picked up by the card. The ID number programmed into it’s chip is used to interfere with the signal transmitted by the reader device, and the reader device can then detect these disturbances in the signal and decode the ID number as a result. All of this happens in just a few milliseconds.

Recognising Specific Tags

The sample program rfid_example.py has a small table of tag ID numbers in it, with names against each card. If you have bought yourself a number of different cards, you can put the ID numbers of each card in this table (and add more entries if you like), and on the right hand side put the name of the person that the card belongs to. Now, when you wave each card over the reader, it will say who the card belongs to.

Something Special About The Code

The code that I have provided here communicates with the RFID reader using a wire protocol called I2C, and it is the files inside the rfid folder that handle the communications with the SL030 reader. This is version 2 of the code, version 1 was quite complicated to set up with many steps and changes to Raspberry Pi configuration files, and lots of people had told me that this was putting them off trying it out, or they failed completely to follow all the setup steps.

I hope that version 2 of the code is much easier for people to use. It does not use the on-board I2C peripheral of the raspberry pi, but it “waggles” the pins very fast itself, which achieves the same goal, but creates a zero-install package – hopefully you have managed to just copy these files to any of your Raspberry Pi computers and run it and it has worked.

If for any reason you have tried this example and it did not work out of the box for you, please do report your experiences on the skpang blog website, which I monitor regularly and will aim to reply to all queries and fix any problems that people find.

Further Ideas

I’m hoping that this will be come one of many simple building-blocks of hardware and software that schools and other experimentors can build on top of to build amazing project ideas. Here are a few that you might want to consider:

1) Use my “playing sounds on the Raspberry Pi” article to assign a different sound to each tag, and print pictures of animals and other items on different cards. Give the cards to young children, and when they wand the card of a dog over the reader, it says the word “dog” and makes a barking noise. With this you can build a language teaching tool for young children.

2) Buy a card for each member of your Raspberry Pi club, and program the tags into your Python program as explained above. Every time a member of the club attends, get them to wand their card over the reader, and store the date and time and the tag-id number in a CSV file on the raspberry pi. This is a class-register system. Get your attendees to wand-out again at the end, and you can then see how long each of them stay at the club and perhaps offer them points for the number of hours they spend working on projects.

3) Attach RFID tags (keyring type ones) to different tools or items in a school classroom, and challenge people to use every item for something within the class to achieve some goal, such as to build something in tech classes using as many different tools and processes as possible. At the end of the lesson analyse the data stored in your data files collected on the Raspberry Pi and announce (with evidence!) who gets the prize for the most tools and processes used in the lesson.

Another source of great ideas for RFID projects, is Charlotte Godley’s Farnell Element 14 blog in ref[10], where she develops a sort of loyalty card system for Raspberry Jam events, using the NXP RFID Reader on a Raspberry Pi.

If you try this code and build something amazing with it, please let me know. You can tweet me on twitter as @whaleygeek, and I’ll publish links to the best projects here on this page to give inspiration to others.

Note, various things about GPIO changed on the RPi2, there is a modified driver in [1b] below that supports the RPi2. I have tested this on an RPi2, but not yet on old V1 RPi (although it should work).

Links and Resources

[1] rfid_SL030 - RFID SL030 sample Python program (for RPi V1 only)

[1b] rfid_SL030_rpi2 – RFID SL030 sample Python program (with RPi2 support as well)

[2] skpang – SL030 product

[3] skpang – Raspberry Pi lead

[4] skpang – Mifare cards and tags

[5] Understanding the sudo command on the Raspberry Pi

[6] Playing sounds with the Raspberry Pi in Python

[7] skpang blog about SL030

Please note that the skpang blog post at time of writing still talks about the old (v1) method of communicating with the device. The instructions are soon to be updated.

[8] Article by Kevin Ashton on how RFID lead to him coining the term “The Internet of Things”

[10] Charlotte Godley’s RFID project on Farnell Element 14 – Pi Passport.

A Note About Security

The details in this article explain only how to read the unique number off of an ID card. This is a read-only operation that makes no change to the card. Many Mifare cards include a region of writeable memory, and the SL030 does have the capability to change the values in this writeable memory, although I have not provided any examples.

Please keep in mind that systems such as the Oyster card have multiple layers of additional security and keys associated with the security of the transport system, and using an off the shelf RFID reader like this on it’s own is not sufficient to “hack” these cards. I don’t know how to hack these cards, I don’t wish to know, and if anyone asks me any questions about how to do it, I will politely ignore you.

The information in this article is based entirely on publicly available information, with a view to making it simpler for schools and hobbyists to build interesting access-tag based projects based around RFID technology. The responsibility of using technology for positive and beneficial purposes lies with you, the user.

If you choose to not use the memory facility of these cards but to just use the ID reading facility I have described in this article, you can still design some interesting systems. These are usually supported by some form of data file or database to store information about the ID numbers and the usage patterns of those ID numbers.

This entry was posted in Drivers, How to, Python, Raspberry Jam. Bookmark the permalink.

Comments are closed.