Raspberry Pi Tutorial
Using the RFID RC522 module in Python


May 9, 2019 • 0 • 0

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.

Using the RFID RC522 module in Python - Raspberry Lab

Prerequisites

To use this RFID module with a Raspberry Pi, you need:

Once you have everything, you can proceed to wiring.

Wiring the RFID module

Connect the RC522 module to the Raspberry following the diagram below.

Wiring diagram of the RFID RC522 module on Raspberry Pi 3

Enable SPI

Before using our module, we need to enable SPI (Serial Peripheral Interface). Nothing complicated, just enter in the console:

sudo raspi-config


Information

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.

Information

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

RFID Tag and RC522 - Raspberry Pi French

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:

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:

RFID tag reading program with RC522 - Raspberry Pi French

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:

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:

RFID tag writing program with RC522 - Raspberry Pi French

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!

Read next

Article image

Choose and use a camera on Raspberry Pi


Article image

Get temperature with a DS18B20 sensor