How RFID Work on Your Raspberry Pi
Your Raspberry Pi uses RFID. There are basically three different types of RFID systems available on the market, mainly distinguished by which frequency range they use. All systems consist of two parts: a tag or card and a reader. The reader extracts binary bits from a tag or card using radio waves, so no wires are needed between the reader and the tag or card.
These tags normally known as passive tags — tags that apparently require no source of power — although a small number are active tags that require fitting with a small watch battery. These active tags are used when you need a much longer read range.
The reader sends out a radio signal, and the tag picks it up and uses the power in that radio signal to activate a microchip. That microchip then sends back a number of pulses to the reader, which is interpreted as a number. The way the tag sends the data back is different on different types of tag, but the main way is transmitting the data back to the reader on a different frequency.
Here’s a list of available tags, with their frequency ranges:
- 125–135 KHz tags: These tags are the ones used in chipping pets and also in a lot of access control systems. They have a limited storage capacity and are normally restricted to holding a serial number only of 64 or 128 bits. The tags and cards used most widely today conform to the EM4100 / EM4200 standard. Most of the tags are read-only, but a few (the Hitag tags, for example) allow you to store data using a special programmer.
- 13.56 MHz tags: These are the so-called smart cards, capable of storing not only a serial number but also some data that can be read or written. There is a measure of security built into the cards so that the data is not accessible to those who don’t know the encryption key. There are many different types of cards, but by far the most common is the one known as a MIFARE classic card.
- UHF (860–960MHz): Unlike the other two RFID types, UHF readers are capable of reading more than one tag at the same time. They are also quite long-range, typically 10 to 30 feet. They’re designed for bulk inventory taking. Each item on a palette can be recorded and counted at the same time. The readers have a very high-power transmitter in them, so much so that they’re a health hazard, requiring strict time limits for workers operating them in order to avoid long-term radiation exposure. There are other, higher-frequency systems in this class as well.
Many Raspberry Pi owners use the MIFARE classic card — to give it its Sunday name, the ISO/IEC 14443 A/MIFARE mode protocol. The readers are cheap, and the tags come in various shapes ranging from key fobs to cards. It’s the cards that are most convenient for these projects, but it’s not essential to use these. Here’s the block diagram of what an RFID system looks like electronically.
The antenna used are formed from coils of very thin wire or even metal foil. Readers for these cards come in all price ranges and interfaces. You can use one of the cheapest — the RFID-RC522 — which can be had for less than $13 for three on popular electronics and auction sites. They’re based on the MFRC522 chip from NXP Semiconductors (formerly Phillips), and though this chip is capable of being connected to a computer in a number of different ways, the way these low-cost boards are designed, they’re restricted to an SPI interface only.
When you get these RFID readers, they come with a choice of two types of header pins. You need to solder on the right-angled pins. Then you can either make up a lead or mount it vertically on a breadboard.
It’s much better to mount it vertically because it’s away from the metal forming the internal clips of the breadboard, and metal affects the resonant frequency of the antenna coil — and thus the tag read range. Here, you see both the schematic and layout diagram of how to wire up the reader to the Raspberry Pi.
For a really neat job, mount the reader in a wooden or plastic box, preferably using nylon screws because close metal reduces the reading range.
After you attach the reader to the Pi, it’s time to get the software you need to read it. First off, you have to install the
python-dev system by typing sudo apt-get install python-dev into a terminal window of a Raspberry Pi connected to the Internet.
There is a fair chance that you have the latest version already installed, but it’s best to check. Next, you need to install the
SPI-Py library, which allows you to use the hardware SPI as a C extension for Python. Again in a terminal window, you enter the following:
git clone https://github.com/lthiery/SPI-Py.git
sudo python setup.py install
sudo python3 setup.py install
This installs the code that allows you to use the SPI hardware from both Python 2 and Python 3. Finally, go to the Desktop menu and choose Preferences, then select the Raspberry Pi Configuration application. When the application opens, click on the Interfaces tab and make sure that the SPI interface is enabled. If it isn’t, click to enable it and reboot your Pi.