RFID circuits have been everywhere for some time and come in various forms. The most well-known use is the entry pass for a building, but you also find them in anti-theft devices, ski passes, and bank cards (equipped with contactless). More recently, some Decathlon stores replaced barcodes with RFID chips, so you just put items in a bin to scan them!
But what is it really? RFID (Radio Frequency Identification) is a technology that allows very fast identification of an item.
The RFID technology uses radio tags (commonly called RFID tags) that come in different forms (cards, chips, stickers). These tags are composed of an antenna and an electronic chip that allow them to receive and respond to radio requests from the transceiver. These chips do not need batteries because they are powered wirelessly by the reader, via induction.
Prerequisites
To use this RFID module with a Raspberry Pi, you need:
- A Raspberry Pi
- An RFID RC522 kit
- Cables
- A soldering iron (unless the module pins are already soldered)
Once you have everything, you can proceed to wiring.
Wiring the RFID module
Connect the RC522 module to the Raspberry following the diagram below.
Enable SPI
Before using our module, we need to enable SPI (Serial Peripheral Interface). Nothing complicated, just enter in the console:
sudo raspi-config
Navigating raspi-config
To navigate raspi-config menus, use the keyboard arrow keys. Press the Enter key to validate.
To quit, press Esc.
Once in the menu, go to 5. Interfacing Options, then to P4. SPI and answer Yes to the question Would you like the SPI interface to be enabled?.
Restart the Raspberry.
sudo reboot
To check that the SPI module has been enabled, enter the following command:
lsmod | grep spi
If the spi_bcm2835 module is listed, then SPI is enabled.
Module not listed?
It may happen that the spi_bcm2835 line is not in the list. To fix this problem, enter the following command:
sudo nano /boot/config.txt
In the opened file, look for the line dtparam=spi=on. If this line exists, remove the opening "#". Otherwise, if the line does not exist, add it at the end of the file.
Save and exit (Ctrl+X > "Y" > Enter).
Restart the Raspberry.
Installing the MFRC522 library
Now it's time to install the library we will use to read and write to our RFID tags. First, let's update our Raspberry Pi.
sudo apt-get update
sudo apt-get upgrade
Now install Python 2.7. It may already be installed; if so, the Raspberry will not reinstall it.
sudo apt-get install python2.7-dev
We can now install SPI-Py which is used by the MFRC522 library:
cd ~
git clone https://github.com/lthiery/SPI-Py.git
cd ~/SPI-Py
git checkout 8cce26b9ee6e69eb041e9d5665944b88688fca68
sudo python setup.py install
Finally, we can install the MFRC522 library:
cd ~
git clone https://github.com/Espace
RaspberryFrancais/RFID-RC522.git
Now that the library is properly installed, we can start using the RFID RC522 module.
Read an RFID tag
Let's start with reading RFID tags. The library I provide includes a Lecture.py program that retrieves two pieces of information from the RFID tags placed on the reader:
- UID (User Identifier). This is a (theoretically unique) number stored in a non-rewritable area. However, some newer special tags have this storage area rewritable, which means you can have two tags with the same UID.
- Data sector #8: An RFID tag has different data sectors. The RC522 allows us to read and write sector 8 (so it is rewritable). Its size is 16 bytes. We can therefore store 16 characters in basic ASCII on it.
The Lecture.py program is located in the ~/RFID-RC522/Lecture.py directory. Just enter these two commands to run it.
cd ~/RFID-RC522
sudo python Lecture.py
Once launched, you get something like this:
Write to an RFID tag
Now that we know how to read the information on an RFID tag, let's see how to write to sector 8. The Ecriture.py program performs the following actions:
- It asks you to enter the information you want to store on the tag. Enter a character string (it is automatically cut at the 16th character) and it will be converted to hexadecimal, then stored on the card as integers.
- The program then waits for you to pass the card over the reader, then writes to it and displays the new content of the card.
The Ecriture.py program is located in the ~/RFID-RC522/Ecriture.py directory. Just enter these two commands to run it.
cd ~/RFID-RC522
sudo python Ecriture.py
Once launched, you get something like this:
This tutorial is now complete. Modify the reading and writing files as you like to build your project. If you have any questions, feel free to post them in the comments.
Have fun!

