Reading Out Raspberry Pi RFID RC522 Tags (NFC)

RFID is a technology whereby data is transmitted without touch, which is used in chip cards. Access cards can be read out with a Raspberry Pi RFID module (RC522) and thus e.g. access to doors or locks can be given. Current smartphones have similar ones.

In this tutorial, I will show how to read RFID tags with the RC522 and the Raspberry Pi and also write chip cards. The code presented can also be used for other projects (door opener, access control).

NFC is a related technology, the differences of which can be found here. Both RFID and NFC transmit on a frequency of 13.56 MHz, which is why the modules are compatible with each other.

 

 

Hardware Parts Used

I have used the following (building) parts for this tutorial:

There are also RFID USB readers, but I have not tried them.

If you want to use the card reader as an entrance control, etc., it makes sense to give every user a card. You can also buy these chip cards in smaller and larger quantities for small money and then individually write RC522 in on each card (instructions below)

 

 

Setup

The pin strip of my module was not yet soldered on, so I had to solder it first before I could connect the pins.
The wiring is as follows:

RF522 Module Raspberry Pi
SDA Pin 24 / GPIO8 (CE0)
SCK Pin 23 / GPIO11 (SCKL)
MOSI Pin 19 / GPIO10 (MOSI)
MISO Pin 21 / GPIO9 (MISO)
IRQ
GND Pin6 (GND)
RST Pin22 / GPIO25
3.3V Pin 1 (3V3)

 

The whole thing looks like this schematically:

Raspberry Pi RFID RC522 NFC Breadboard

Raspberry Pi RC522 Connection

 

 

Activating SPI and Software Installation

In order to use the RFID RC522 Shield, we need the SPI bus. So that the Kernel is loaded at startup, we edit the config file:

sudo nano /boot/config.txt

The following content is added to the end of the file:

device_tree_param=spi=on
dtoverlay=spi-bcm2708

You save and exit with CTRL + O, CTRL + X. Then we activate SPI:

sudo raspi-config

Activate under “Advanced Options”> “SPI” and confirm the restart (alternatively usesudo reboot now).

Then you can use dmesg | grep spi to checked whether the module has been loaded. The output should look like this:

pi@raspberrypi:~ $ dmesg | grep spi
[   10.784368] bcm2708_spi 20204000.spi: master is unqueued, this is deprecated
[   10.813403] bcm2708_spi 20204000.spi: SPI Controller at 0x20204000 (irq 80)

 

Now the packages have to be installed so that we can access the SPI bus and load a corresponding library from GitHub.

sudo apt-get install git python-dev --yes

Firstly we install the Python SPI module

git clone https://github.com/lthiery/SPI-Py.git
cd SPI-Py
sudo python setup.py install
cd ..

and then the Raspberry Pi RFID RC522 library:

git clone https://github.com/mxgxw/MFRC522-python.git && cd MFRC522-python

 

 

Testing the Raspberry Pi RFID Reader/Writer

In addition to the RC522 module, a white card and an NFC-compatible key fob are usually supplied. These parts can be used as authentication because they are writable and readable. An NFC-enabled (Android/iOS) smartphone could also be used (which most newer cell phones are).

To run the first test of the card/key fob, we run the script:

sudo python Read.py

As soon as the chip card is held to it and recognized, you will see an output like this:

pi@raspberrypi:~/MFRC522-python $ sudo python Read.py
Welcome to the MFRC522 data read example
Press Ctrl-C to stop.
Card detected
Card read UID: 69,245,238,117
Size: 8
Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

In order to change the stored data (numbers) on the chip, we edit the “Write.py” file (sudo nano Write.py). To do this, edit the code from line 55 as follows (you can freely choose the 16 numbers from data between 0 and 255.  I represented a word with ASCII characters)

 

 

Using NFC/RFID reader in Raspberry Pi projects (door lock, etc.)

The two Python files “Read.py” and “Write.py” contain some sample code for reading and writing a chip which can be used in other projects. The most important file is “MFRC522.py”, which can be copied into another project.

The following excerpt can be used in other projects, e.g. as a check of a code lock or door lock. I use one authentication level (you could also set several) with a certain initial code. The last digits provide information about the holder of the card (if that data is stored somewhere). You could only identify the user by the UID, but I assume that several cards can belong to one user. If you don’t like this solution, you can, of course, change it.

I have made a small change in the “MFRC522.py” file so that the MIFAREReader.MFRC522_Read function has a return value:

 

The sample code then looked like this (the previous changes are important, otherwise no comparison can take place):

As you can see, the code is very simple and, as I said, should only serve as an impetus to start your own, more sophisticated, applications. The code in line must, of course, be adjusted.

 

What projects are you planning to do with it? I am considering building a door lock or a drawer lock/small safe with the Raspberry Pi RFID reader.

Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!