How to Set Up a Raspberry Pi RFID RC522 Chip

In this Raspberry Pi RFID RC522 tutorial, I will be walking you through the steps on how to set up and wire the RFID RC522 chip with your Raspberry Pi.

raspberry pi RFID Reader

This project is a cool circuit to play around with and opens you up to quite a wide variety of different projects from using it as an attendance system to using it to open a lock.

The RFID RC522 is a very low-cost RFID (Radio-frequency identification) reader and writer that is based on the MFRC522 microcontroller.

This microcontroller provides its data through the SPI protocol and works by creating a 13.56MHz electromagnetic field that it uses to communicate with the RFID tags.

Make sure that the tags you purchase for your RFID RC522 operate on the 13.56MHz frequency otherwise we will fail to read them.

We will be showing you how to wire up the RC522 as well as showing you how to write Python scripts to interact with the chip so you can both read and write your RFID Tags.

You can extend this tutorial to use something like a 16×2 LCD for the Raspberry Pi, handy if you want to show some information or display a visual prompt to the end user.

Equipment List

Below are all the bits and pieces that I used for this Raspberry Pi RFID RC522 tutorial.

Recommended

Optional

Video

Below we have a video showing you the process of setting up the RC522 on your Raspberry Pi including setting up all the circuitry.

If you would prefer a more thorough explanation on how to do everything, then you can check out our written guide on setting up the RFID RC522 with your Raspberry Pi below.

Adblock removing the video? Subscribe to premium for no-ads.

Assembling the RFID RC522

One thing you will notice when purchasing an RFID RC522 Reader is that 90% of them don’t come with the header pins already soldered in. The missing pins mean you will have to do it yourself, luckily soldering header pins is a rather simple task, even for beginners.

1. First off, if the header pins you received with your RC522 isn’t the correct size, then snap them down, so you only have a single row of eight pins.

2. Place the header pins up through the holes of your RC522. One handy trick is to put the long side of the header pins into a breadboard and then putting the circuit over the top of the header pins. The breadboard will hold the pins tightly making it easier to solder them to the RFID RC522 circuit.

3. Now using a hot soldering iron and some solder, slowly solder each of the pins. Remember it is best to heat the joint slightly before applying solder to it, this will ensure that the solder will adhere more to the joint and reduce the chances of creating a cold joint. We also recommend being careful with the amount of solder you apply.

4. With the header pins now soldered to your RFID circuit, it is now ready to use, and you can continue with the tutorial.

Wiring the RFID RC522

On your RFID RC522 you will notice that there are 8 possible connections on it, these being SDA (Serial Data Signal), SCK (Serial Clock), MOSI (Master Out Slave In), MISO (Master In Slave Out), IRQ (Interrupt Request), GND (Ground Power), RST (Reset-Circuit) and 3.3v (3.3v Power In). We will need to wire all of these but the IRQ to our Raspberry Pi’s GPIO pins.

You can either wire these directly to the GPIO Pins or like we did in this tutorial, plug the RFID RC522 into our Breadboard then wire from there to our Raspberry Pi’s GPIO Pins.

Wiring your RFID RC522 to your Raspberry Pi is fairly simple, with it requiring you to connect just 7 of the GPIO Pins directly to the RFID reader. Follow the table below, and check out our GPIO guide to see the positions of the GPIO pins that you need to connect your RC522 to.

  • SDA connects to Pin 24.
  • SCK connects to Pin 23.
  • MOSI connects to Pin 19.
  • MISO connects to Pin 21.
  • GND connects to Pin 6.
  • RST connects to Pin 22.
  • 3.3v connects to Pin 1.
Raspberry Pi RFID Wiring Schematic
Raspberry Pi RFID RC522 GPIO Connection

Setting up Raspberry Pi OS for the RFID RC522

Before we begin the process of utilizing the RFID RC522 on our Raspberry Pi, we will first have to make changes to its configuration. By default, the Raspberry Pi has the SPI (Serial Peripheral Interface) disabled, which is a bit of a problem as that is what our RFID reader circuit runs through.

Don’t worry though as it is fairly simple to re-enable this interface, just follow our steps below to configure your Raspberry Pi and Raspberry Pi OS to utilize the SPI interface.

1. Let’s begin by first opening the raspi-config tool, and we can do this by opening the terminal and running the following command.

sudo raspi-config

2. This tool will load up a screen showing a variety of different options. If you want a more in-depth look into these options, you can check out our raspi-config guide.

On here use the arrow keys to select “5 Interfacing Options“. Once you have this option selected, press Enter.

3. Now on this next screen, you want to use your arrow keys to select “P4 SPI“, again press Enter to select the option once it is highlighted.

4. You will now be asked if you want to enable the SPI Interface, select Yes with your arrow keys and press Enter to proceed. You will need to wait a little bit while the raspi-config tool does its thing in enabling SPI.

5. Once the SPI interface has been successfully enabled by the raspi-config tool you should see the following text appear on the screen, “The SPI interface is enabled“.

Before the SPI Interface is fully enabled we will first have to restart the Raspberry Pi. To do this first get back to the terminal by pressing Enter and then ESC.

Type the following Linux command into the terminal on your Raspberry Pi to restart your Raspberry Pi.

sudo reboot

6. Once your Raspberry Pi has finished rebooting, we can now check to make sure that it has in fact been enabled. The easiest way to do this is to run the following command to see if spi_bcm2835 is listed.

lsmod | grep spi

If you see spi_bcm2835, then you can proceed on with this tutorial and skip on to the next section. If for some reason it had not appeared when you entered the previous command, try following the next three steps.

7. If for some reason the SPI module has not activated, we can edit the boot configuration file manually by running the following command on our Raspberry Pi.

sudo nano /boot/config.txt

8. Within the configuration file, use CTRL + W to find “dtparam=spi=on“.

If you have found it, check to see if there is a “#” in front of it. If there is, remove it as this is commenting out the activation line. If you can’t find the line at all, add “dtparam=spi=on” to the bottom of the file.

Once you have made the changes, you can press CTRL + X then pressing Y and then Enter to save the changes.

You can now proceed from Step 5 again, rebooting your Raspberry Pi then checking to see if the module has been enabled.

Getting Python ready for the RFID RC522

Now that we have wired up our RFID RC522 circuit to the Raspberry Pi we can now power it on and begin the process of programming simple scripts in Python to interact with the chip.

The scripts that we will be showing you how to write will show you how to read data from the RFID chips and how to write to them. These will give you the basic idea of how data is dealt with and will be the basis of further RFID RC522 tutorials.

Installing the Required Packages for RFID RC522 On the Raspberry Pi

1. Before we start programming, we first need to update our Raspberry Pi to ensure it’s running the latest version of all the software. Run the following two commands on your Raspberry Pi to update it.

sudo apt update
sudo apt upgrade

2. Now the final thing we need before we can proceed is to install python3-dev, python3-pip and python3-venv packages.

Simply run the following command on your Raspberry Pi to install all of the required packages for this guide on setting up your RFID reader.

sudo apt install python3-dev python3-pip python3-venv

Setting up a Directory for the Python Scripts and Environment

3. Our next step is to create a directory where we will be storing the scripts we are writing as well as the Python virtual environment.

Recent versions of Raspberry Pi OS require packages installed using pip to be stored within a virtual environment.

You can create this directory by using the following command within the terminal.

mkdir ~/pi-rfid

4. After creating the directory, change to it by using the following command.

cd ~/pi-rfid

Creating a Python Virtual Environment

5. Now, we must use Python to generate a virtual environment within a directory called “env“.

We must use this virtual environment whenever we want to run our scripts and install any other Python packages.

python3 -m venv env

6. After Python finishes generating the virtual environment, use the command below to start utilizing it.

Once we use the virtual environment, the Python packages we install using pip will be kept within this environment, not the global one.

source env/bin/activate

After running the above command, you should see your terminal line starts with “(env)“.

Installing the Python Packages to Use RFID RC522 on the Raspberry Pi

7. Next, we must install the Python Library spidev to our Raspberry Pi using the python “pip” tool that we downloaded in the previous step.

The spidev library helps handle interactions with the SPI and is a key component to this tutorial as we need it for the Raspberry Pi to interact with the RFID RC522.

Run the following command  on your Raspberry Pi to install spidev to your Raspberry Pi through pip.

Please note that we use sudo here to ensure that the package is installed so that all users can utilize it and not just the current user.

python3 -m pip install spidev

8. Now that we have installed the spidev library to our Raspberry Pi we can now now proceed to installing the MFRC522 library using pip as well.

You can check out the RFID MFRC522 Python code from the PiMyLifeUp Github if you are interested in seeing how the library works or improving its behaviour.

There are two files that are included within our MFRC522 library that we make use of:

MFRC522.py” which is an implementation of the RFID RC522 interface, this library handles all the heavy lifting for talking with the RFID over the Pi’s SPI Interface.

SimpleMFRC522.py” that takes the “MFRC522.py” file and greatly simplifies it by making you only have to deal with a couple of functions instead of several.

To install the MFRC522 library to your Raspberry Pi using pip go ahead and run the following command.

python3 -m pip install mfrc522

9. With the library now saved to our Raspberry Pi, we can begin programming for our RFID RC522.

To start off with we will be showing you how to write data to your RFID cards by using the RC522. Simply go onto our next section to begin programming our first Python script.

Writing with the RFID RC522

For our first Python script, we will be showing you how to write data from the RC522 to your RFID tags. Thanks to the SimpleMFRC522 script this will be relatively simple, but we will still go into how each part of the code works.

1. Before we begin writing this script, let us ensure we are in the directory we created earlier, and we are using the virtual environment stored within it.

cd ~/pi-rfid
source env/bin/activate

2. Begin by changing directory into our newly cloned folder, and begin writing our “Write.py” Python script.

nano Write.py

3. Within this file, write the following lines of code. This code will basically ask you for text to input and then write that text to the RFID Tag.

a. The first line of this segment of code helps tell the terminal how to interpret the file, and it lets it know that it should use Python when executing it and not something else such as Bash.

Our first import, “RPi.GPIO” has all the functions needed to interact with the GPIO Pins, and we need this to make sure they are cleared when the script finishes running.

The second import, imports in our “SimpleMFRC522” library, this is what we will use actually to talk with the RFID RC522, it greatly simplifies dealing with the chip compared to the base MFRC522 library.

#!/usr/bin/env python

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

b. This line creates a copy of the SimpleMFRC522 as an object, runs its setup function then stores it all in our reader variable.

reader = SimpleMFRC522()

c. Our next block of code we keep within a try statement, this is so we can catch any exceptions and clean up properly. Make sure that you retain the ‘tabs‘ after “try:” as Python is whitespace sensitive, and it is how it differs between blocks of code.

The second line here reads in an input from the command line, and we use “input” in Python 3 to read in all input and store it in our text variable.

With the third line, we utilize “print()” to notify the user that they can now place their RFID tag down onto the reader for writing.

Afterward, on our fourth line of code we use our reader object to write the values we stored in the text variable to the RFID tag, this will tell the RFID RC522 Circuit to write the text values to a certain sector.

Finally, on the 5th line of code, we use “print()” again to notify the user that we have successfully written to the RFID tag.

try:
        text = input('New data:')
        print("Now place your tag to write")
        reader.write(text)
        print("Written")

d. Our final two lines of code handle exiting of the script. Finally, always occurs after the try statement, meaning no matter what we run the “GPIO.cleanup()” function.

These lines are crucial as failing to clean up can prevent other scripts from working correctly.

finally:
        GPIO.cleanup()

4. Once you have finished writing in your script, it should look something like below.

#!/usr/bin/env python

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        text = input('New data:')
        print("Now place your tag to write")
        reader.write(text)
        print("Written")
finally:
        GPIO.cleanup()

Once you are happy that the code looks correct, you can save the file by pressing CTRL + X then pressing Y and then finally hitting ENTER.

5. Now that we have written our script, we will want to test it out. Before testing out the script make sure that you have an RFID tag handy. Once ready, type the following command into your Raspberry Pi’s terminal.

python3 Write.py

6. You will be asked to write in the new data, in our case we are going to just type in “pimylifeup” as its short and simple. Press ENTER when you are happy with what you have written.

7. With that done, simply place your RFID Tag on top of your RFID RC522 circuit.

As soon as it detects it, it will immediately write the new data to the tag. You should see “Written” appear in your command line if it was successful.

You can look at our example output below to see what a successful run looks like.

pi@raspberrypi:~/pi-rfid $ sudo python3 Write.py
New data:pimylifeup
Now place your tag to write
Written

8. You have now successfully written your “Write.py” script, and we can now proceed to show you how to read data from the RFID RC522 in the next segment of this tutorial.

Reading with the RFID RC522

Now that we have written our script to write to RFID tags using our RC522 we can now write a script that will read this data back off the tag.

1. Let’s start off by changing the directory to make sure we are in the right place, we will also use “source” to switch into the virtual environment we creatd

cd ~/pi-rfid
source env/bin/activate

2. Once we are in the right place, we can run nano to begin writing our “Read.py” script.

nano Read.py

3. Within this file, write the following lines of code. This script will basically sit and wait till you put your RFID tag on the RFID RC522 reader, it will then output the data it reads off the tag.

a. The first line of code tells the operating system how to handle the file when a user executes it. Otherwise, it will try and just run it as a regular script file and not a Python file.

The first import is, “RPi.GPIO“. This library contains all the functions to deal with the Raspberry Pi’s GPIO pins, and we mainly import this to ensure that we clean up when the script finishes executing.

The second import is, “SimpleMFRC522“. This script contains a few helper functions to make it an awful lot easier to deal with writing and reading from the RFID RC522, without it the scripts would become quite long.

#!/usr/bin/env python

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

b. This line is quite important as it calls SimpleMFRC522’s creation function and then stores that into our reader variable as an object so we can interact with it later.

reader = SimpleMFRC522()

c. This next block of code is contained within a “try” statement, and we use this so we can catch any exceptions that might occur and deal with them nicely. You need to ensure that you use the ‘tabs‘ as shown after “try:” as Python is whitespace sensitive.

The second line in this block of code makes a call to our reader object, in this case, it tells the circuit to begin reading any RFID tag that is placed on top of the RC522 reader.

With the third and fourth lines we utilize “print()” to print out the information that we received from reading the RFID Chip, this includes the ID associated with the RFID tag and the text that is stored on the tag.

try:
        id, text = reader.read()
        print(id)
        print(text)

d. The two last lines of code handle the termination of the script. The “finally” statement always triggers after the try statement even if we get an exception.

This try statement ensures that no matter what we run the “GPIO.cleanup()” function. It is quite crucial as failing to clean up the GPIO can prevent other scripts from working correctly.

finally:
        GPIO.cleanup()

4. Now that you have finished writing your “Read.py” script for your RFID RC522 it should look something like what is shown below.

#!/usr/bin/env python

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        id, text = reader.read()
        print(id)
        print(text)
finally:
        GPIO.cleanup()

Once you are sure you have entered the code correctly, you can save the file by pressing Ctrl + X then pressing Y and then finally hitting ENTER.

5. Now that we have finally finished our Read.py script we need to test it out. Before we test out the script, grab one of the RFID tags that you want to read.

Once that you are ready, type the following command into your Raspberry Pi’s terminal.

python3 Read.py

6. With the script now running, all you need to do is place your RFID Tag on top of your RFID RC522 circuit.

As soon as the Python script detects the RFID tag being placed on top, it will immediately read the data and print it back out to you.

An example of what a successful output would look like is displayed below.

pi@raspberrypi:~/pi-rfid $ python3 Read.py
827843705425
pimylifeup

7. If you successfully receive data back from your Read.py script with the text that you pushed to the card using your Write.py script then you have successfully set up your Raspberry Pi to connect with your RFID RC522 Circuit.

You can learn how to setup your RFID RC522 Reader/Writer as a way of checking attendance by following our Raspberry Pi powered RFID attendance system guide.

We will be going into more depth with these scripts and the RFID chip in later tutorials. It will include exploring how to set up a door security system among other cool DIY Pi projects.

If you have enjoyed this Raspberry RFID RC522 tutorial or have any feedback, then feel free to drop a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

91 Comments

  1. Avatar for NotJim
    NotJim on

    Day 1 new guy here. When connecting the GPIO pins to the bread board you reference the pins by number. When you say, for example pin 24, are you referencing the physical 24th pin on the board? Alternatively, are you referencing the physical 18th pin labeled GPIO 24?

    I’m learning on a pi5 for reference..

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi NotJim,

      In this particular guide we are referencing the physical pin numbering. I typically try to keep to the physical numbering in most of the guides I write.

      Kind regards,
      Emmet

  2. Avatar for Philipp
    Philipp on

    Thanks for the great tutorial! Worked on my first try on my Raspi Pi 4.

  3. Avatar for Brian Rogers
    Brian Rogers on

    Hi,
    This was a great tutorial, It got me going great,
    until ‘Bookworm’.
    With the removal of pip, it sadly lies in tatters.
    some libraries (eg spidev) that were installed:
    sudo pip3 install spidev
    can now be installed using apt:
    sudo apt install python3-spidev

    but not mfrc522
    ๐Ÿ™

    I’m thinking I’ll need to copy the files from the lib of an old install I still have, but so inelegant.
    I hope you have better news ๐Ÿ™‚

    ttfn
    Brian

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Brian,

      Sorry to hear that you have ran into issues with our guide due to the changes introduced in Bookworm.

      With the changes they made in Bookworm they now expect you to work within virtual environments when installing Python packages. I have quickly re-adjusted this tutorial so the scripts and the Python packages it needs are installed to a local Python virtual environment.

      Please let me know if this resolves the issues you are facing.

      Kind regards,
      Emmet

  4. Avatar for Pyro
    Pyro on

    Greetings,
    Thank you for the write up on this RFID module. I am using it for a Linux class project at the College of Southern Nevada. I am a Electronics Technician major, so this was right up my alley to combine the two. I am looking forward to expanding on this with a LCD, num pad, garage door, etc. This was awsome to learn. Thanks Again!
    I was wondering if there was a way to set up a database using only linux and python3 rather than the SQL set-up in the Attendance System RFID module.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Pyro,

      I am glad to hear that this tutorial has been helpful to you and I hoping that your studies are going well!

      When it comes to your question are you asking whether you can avoid using MySQL/MariaDB completely and handle everything with just Python? or are you asking whether it is possible to have the script self install itself to a specified database?

      Both options are doable. The first you could very easily move to using SQLite. This stops you from having to run a separate database server but allows you to still utilise SQL to query and add data to the database. SQLite operates by saving and working with a local file rather than a database server such as MySQL or MariaDB.

      The second option would just require adding additional logic to see whether the database has the tables we need, and if not add them if they don’t exist.

      Please let me know if you need me to try and explain things a bit better or whether I have misunderstood your question.

      Cheers,
      Emmet