Facebook Twitter Instagram Pinterest YouTube
    Trending
    • Elecrow Meteor IPS Touchscreen with RGB LEDs
    • Pi Pico Pinout Display on the Command Line
    • How to Add a Raspberry Pi Pico Reset Button
    • Pi Pico Onboard LED
    • Pi Pico W Pinout and Power Pins
    • CrowPi L Raspberry Pi Laptop and Learning Platform
    • Pi Pico W Launched
    • Add Kodi to RetroPie Menu
    Facebook Twitter Instagram Pinterest YouTube RSS
    Raspberry Pi SpyRaspberry Pi Spy
    • Home
    • Categories
      • General
      • Hardware
      • Programming
      • Python
      • Software
      • Tutorials & Help
    • BerryClip
      • BerryClip Instructions
      • BerryClip Plus Instructions
      • Videos & Reviews
    • Buy
      • Buy Pi
      • Buy Pi Accessories
      • Buy Books
    • Tools
      • Ultimate Raspberry Pi Alexa Skill
      • Pi Power Estimator App
      • Pi-Lite 14×9 LED Matrix Sprite Editor
      • RPiREF Pin-out Reference App
      • Simple Ohm’s Law Calculator
      • Web Sites & Links
    • Tutorials & Help
        Featured
        November 9, 20200

        Raspberry Pi Temperature Monitoring

        Recent
        December 23, 2022

        How to Add a Raspberry Pi Pico Reset Button

        November 20, 2022

        Pi Pico Onboard LED

        May 6, 2022

        Add Kodi to RetroPie Menu

      1. Contact Us
      2. Site Map
      Raspberry Pi SpyRaspberry Pi Spy
      You are at:Home»Hardware»Sensors»RC522 RFID Tag Reading with the Raspberry Pi
      RFID RC522 Module

      RC522 RFID Tag Reading with the Raspberry Pi

      2
      By Matt on February 25, 2018 Sensors, SPI, Tutorials & Help

      RC522 RFID modules are a simple add-on you can connect to a Raspberry Pi to read MIFARE tags and cards. This is potentially a great feature to include in a security system or any application where you need to identify an object or person without them pressing buttons, operating switches or other sensors. The contactless tags can be carried on a key-ring and the cards fit nicely in a wallet. Both of them can be hidden inside other objects to give them a unique ID that can be read by the Pi.


      The obvious application is a security system where you use a tag to activate or deactivate an alarm but other applications include time and attendance systems and games.

      Supported Protocols

      Modules that use the RC522 chip should support all tags that use the following standards :

      • MIFARE Mini
      • MIFARE 1K
      • MIFARE 4K,
      • MIFARE Ultralight
      • MIFARE DESFire EV1
      • MIFARE Plus RF

      It is possible to buy additional cards and tags but you should make sure they support one of those standards.

      These RFID modules can not read contactless bank cards. They can read an ID from a smart phone but the ID is different every time so not very useful in most circumstances.

      The Module

      There are different modules available from the usual sources but mine is a blue PCB with eight connections. The antenna track can be seen on the PCB and it is this that communicates with a nearby tag.

      RFID RC522 reader and MIFARE tags

      My module came with two different styles of header pins, one of which needed to be soldered onto the PCB. I soldered on the right-angle header so that the cable would be inline with the PCB. One keyring style tag and one plain white “credit-card” style card were also included.

      RC522 MIFARE Tag and RFID Module

      Each of these devices has a unique code (UID) which it is possible to read using a Python script.

      The modules do not normally come supplied with jumper cables so you will need seven female-female jumper cables to connect it directly to the Pi.

      Hardware Setup

      The RC522 module has eight pins but we only need to connect seven of then to the Raspberry Pi’s GPIO header. The pins are labelled on the PCB but the markings are quite squashed.

      RC522 RFID module header

      I used a 7-way female-female jumper cable to connect the module to the Pi.

      RC522 and Raspberry Pi GPIO wiring

      The wiring details are also shown in the table below :

      RC522 HeaderDiagram ColourPi Header Notes
      3.3VGrey13.3V
      RSTWhite22GPIO25
      GNDBlack 6Ground
      IRQ––Not connected
      MISOPurple21GPIO9
      MOSIBlue19GPIO10
      SCKGreen23GPIO11
      SDAYellow24GPIO8

      Please refer to the Raspberry Pi GPIO Header page for details of the Pi’s header pin layout.

      Enable SPI

      The RC522 module uses the SPI interface to communicate with the Pi. The SPI interface has to be enabled as it is disabled by default. This is explained in more detail in my Enable SPI Interface on the Raspberry Pi tutorial.

      If you don’t want to read that then simply run :

      sudo raspi-config

      from the terminal or “Raspberry Pi Configuration” from the desktop and enable SPI under the “Interfacing Options” section.

      Install SPI Supporting Libraries

      Next install the spidev library using :

      sudo apt-get install python-spidev python3-spidev

      This installs it for both Python 2 and Python 3.

      Although I tend to use “py-spidev” to drive the SPI interface in Python for this application I used “SPI-Py”. This can be installed using the following sequence of commands :

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

      This installs it for both Python 2 and Python 3.

      Download RC522 Python Library

      Finally download a library that helps talk to the RC522 module over the SPI interface. It relies on the SPI-Py library installed in the previous step.

      The following commands will download the required library :

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

      Example Python Script

      In the “MFRC522-python” directory there are some example scripts. You can navigate to these using :

      cd MFRC522-python

      Run the “Read.py” script using the following command :

      python Read.py

      The script waits for a tag to be detected by the RFID module. When it finds a tag it reads the UID and displays it on the screen. The script runs in a loop and will keep waiting and displaying any detected UIDs.

      RC522 Module Read Example

      Running this script allows you to determine the UID of the tag or card that was supplied with the reader.

      Python 2 or Python 3

      By default the MFRC522 library only works with Python 2. However it is easy to modify to work with Python 3. Simply edit the “MFRC522.py” file in a text editor and convert all nine of the print statements to Python 3 friendly syntax. For example,

      print "Authentication Error"

      becomes :

      print("Authentication Error")

      Everything after the print statement should be wrapped in ( ) brackets.

      The example script will now run under Python 3 without any errors :

      python3 Read.py

      My Example Python Script

      In order to simplify the output I created a modified example script. It reads a card, prints out the ID and waits for 2 seconds so you don’t get the same ID printed twice unless you hold the card in place for more than 2 seconds.

      import time
      import RPi.GPIO as GPIO
      import MFRC522
      
      # Create an object of the class MFRC522
      MIFAREReader = MFRC522.MFRC522()
      
      # Welcome message
      print("Looking for cards")
      print("Press Ctrl-C to stop.")
      
      # This loop checks for chips. If one is near it will get the UID
      try:
        
        while True:
      
          # Scan for cards
          (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
      
          # Get the UID of the card
          (status,uid) = MIFAREReader.MFRC522_Anticoll()
      
          # If we have the UID, continue
          if status == MIFAREReader.MI_OK:
      
            # Print UID
            print("UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
      
            time.sleep(2)
      
      except KeyboardInterrupt:
        GPIO.cleanup()
      

      You can download this script directly to your Pi using :

      wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/rc522_read.py

      and run it using :

      python rc522_read.py

      It will run under Python 3 if you have modified the MFRC522.py print statements as mentioned previously.

      The output looks something like this :

      RC522 Module Simple Read Example

      This script is a bit closer to the code I would actually use if I wanted to read a card and take action if it had an ID stored in a list. For example, to open an electronic door lock.

      The UID is a four element array named “uid” and the script combines each element into a comma separated string. You don’t need to use the commas and the 4 elements of the array could be combined to give a plain number. For example my tag UID would become “1483565119” rather than “148,35,65,119”.

      Final Thoughts & Security Considerations

      Hopefully this will get you to the point that you can read the UID from your MIFARE tags. If you are using this sort of mechanism in a security system then be aware that it is possible to clone these cards and give them a new UID. So your system would only be secure if you prevented an attacker from discovering your UID or having physical access to your tags.

      The NXP Semiconductors Datasheet provides all the detailed technical information about the RC522 chip and the protocols it supports.

      Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
      Previous ArticleEnable 1-Wire Interface on the Raspberry Pi
      Next Article Change Raspberry Pi I2C Bus Speed

      Related Posts

      How to Add a Raspberry Pi Pico Reset Button

      Pi Pico Onboard LED

      Add Kodi to RetroPie Menu

      2 Comments

      1. John on June 10, 2018 7:57 pm

        Hello
        Thank you for your tuto,
        works great, if I can ask you, how can modifies the code to add different tags(10). do you have any site or idea?
        Thank you in advance.

        Reply
      2. Tony on November 18, 2021 12:01 am

        The following may be useful to people coming across this page.

        This page is a great starting point, but unfortunately SPI-Py has been rewritten and has a new interface which is incompatible with MFRC522-python.

        There’s another library based on MFRC522-python which is independent of SPI-Py which you can download from https://github.com/ondryaso/pi-rc522 and install. It works with the RFID module described on this page.

        Reply

      Leave A Reply Cancel Reply

      This site uses Akismet to reduce spam. Learn how your comment data is processed.

      Recent Posts
      March 13, 2023

      Elecrow Meteor IPS Touchscreen with RGB LEDs

      December 26, 2022

      Pi Pico Pinout Display on the Command Line

      December 23, 2022

      How to Add a Raspberry Pi Pico Reset Button

      November 20, 2022

      Pi Pico Onboard LED

      November 14, 2022

      Pi Pico W Pinout and Power Pins

      Categories
      • 1-wire
      • 3D Printing
      • Add-ons
      • BBC Micro:bit
      • BerryClip
      • Books
      • Camera Module
      • Cases
      • Events
      • General
      • Hardware
      • I2C
      • Infographics
      • Interfaces
      • Minecraft
      • Model A+
      • Model B+
      • News
      • Pi Models
      • Pi Pico
      • Pi Zero
      • Power
      • Programming
      • Python
      • Raspberry Pi OS
      • Raspbian
      • RetroGaming
      • Robotics
      • Sensors
      • Software
      • SPI
      • Tutorials & Help
      Tags
      Arduino audio battery berryclip Birthday bluetooth cambridge camera CamJam DigiMakers display games GPIO I2C interface Kickstarter Kodi LCD LED Linux media Minecraft Model A motionEyeOS PCB photography photos Pi-Lite Pi Pico power python Raspberry Jam Raspberry Pi Bootcamp raspbian Retrogaming retroPie screen SD card security sensor SPI SSH temperature ultrasonic video
      Raspberry PI Related
      • Adafruit Blog
      • Average Maker
      • Official RaspBerry Pi Site
      • Raspberry Pi Pod
      • RasPi.tv
      • RaspTut
      • Stuff About Code
      Tech Resources
      • MattsBits – Pi Resources
      • Microbit Spy
      • Technology Spy
      Archives
      About

      Unofficial site devoted to the Raspberry Pi credit card sized computer offering tutorials, guides, resources,scripts and downloads. We hope to help everyone get the most out of their Pi by providing clear, simple articles on configuring, programming and operating it.

      Popular Posts
      September 19, 2014

      Top 5 Reasons The Raspberry Pi Sucks

      July 27, 2012

      16×2 LCD Module Control Using Python

      October 20, 2013

      Analogue Sensors On The Raspberry Pi Using An MCP3008

      Recent Posts
      March 13, 2023

      Elecrow Meteor IPS Touchscreen with RGB LEDs

      December 26, 2022

      Pi Pico Pinout Display on the Command Line

      December 23, 2022

      How to Add a Raspberry Pi Pico Reset Button

      Facebook Twitter Instagram Pinterest YouTube RSS

      Entries RSS | Comments RSS

      This site is not associated with the official Raspberrypi.org site or the Raspberry Pi Foundation. Raspberry Pi is a trademark of the Raspberry Pi Foundation.

      Copyright © 2022 - All Rights Reserved - Matt Hawkins

      mastodon.social@RPiSpy

      Type above and press Enter to search. Press Esc to cancel.