Thursday, 30 January 2014

RFID and Raspberry PI

SainSmart RFID-RC522 & Pi

My first blog about some hardware hacking I am looking at, this article describes connecting the SainSmart RFID-RC522 module with the Raspberry PI. It refers to work that others have done, please see the references at the end of the blog for the sources of information I have used. However by collecting together these sources and my own additions, this will help others.

The SainSmart RFID-RC522 module works with the Mifare RFID tags and uses the RC522 chip. SainSmart have provided a module that can be used as a RFID Reader Card Proximity Module. The module uses the SPI bus to communicate with a controller. For those using a Raspberry PI it is imoprtant to note module uses 3.3v and is compatible with the voltage inputs on the Raspberry PI.

The Serial Peripheral Interface bus (SPI) bus is a synchronous serial data link which the Raspberry PI supports through its GPIO, the PI supports two slave devices using the CE (Chip enable) pins.

Enable the SPI on the Raspberry PI

As the SPI is not enabled by default you will need to edit the raspi-blacklsit.conf in order to enable the SPI interface; this has been blacklisted as most users are not interested in it according to the comment in the file. There are only two devices in the file, the SPI and I2C.

sudo vi /etc/modprobe.d/raspi-blacklist.conf

Add '#' in front of the line spi-bcm2708 to comment it out of the blacklist. Save the file, and you will need to reboot the Raspberry PI, after which the lsmod command should show the spi device (spi_bcm2708) enabled.

Connection Diagram

Connecting the module to the PI is reasonably straight forward, as the wiring diagram shows, my breadboard set is also pictured.

SPI Code

To use the module from Python, need to load a SPI wrapper, however we need to install ‘python-dev’ to enable us to install the SPI wrapper.

To install ‘python-dev’ :

sudo apt-get install python-dev

In order to read data from the SPI bus in Python we need a set of routines, a suitable set is SPI-Py, available form github.

To do the install, clone the SPI-Py git repository. This is the source code for the SPI python library we’ll be using.

git clone

Install the SPI-Py module by typing

cd SPI-Py
sudo python install

Sample Program

In order to test the module out they is a sample code which is a Python port of the example code for the NFC module MF522-AN and provides a small class object to interface with Moduleon the Raspberry Pi.

This is a Python port of the example code for the NFC module MF522-AN

sudo python

If everything I working you should be able to read the tags that came with the SainSmart module.



  1. hello nice tutorial, I follow every step without problem, except the last one:
    sudo python I got this result:

    Card read UID: 131,80,231,164,144
    Size: 8
    AUTH ERROR(status2reg & 0x08) != 0

    I don't understand why I can Authenticate.

    One thing, I have also install he bcm2835 library.

    1. Hi, It appears to a problem with the ported Python code, I am working on some corrections to the code which I will publish, however from the snippet you pasted I can see it read the Card UID

    2. I too am facing the same problem. Is a fix available?


  2. Please help. I have followed many tutorials on the internet but can't seem to find out how to make this work. I followed all the instructions but when I get to it never seems to recognise any RFID cards I put next to it. I have tried re doing this many times. Could it be to do with a dodgy RFID reader?

    1. Oh it turned out it was a dodgy reader so they sent me another and now it works. :)

  3. I'm working on a new python code which will include some diagnostic information which may help. You need to make sure it is wired up correctly, also are there any error messages when you run the python script?

  4. Hello GeraintW!

    Nice Tutorial! It helps me alot! I had a few Problems connecting the pins but with your Graphic and the Graphic of a Spanish Guy ( i solved it.

    Now i can read the Card UID, but i get now the AUTH Error like the Previous.
    When do you think is your PAtch ready for solving this Problem ?
    Maybe you can explain how the Authentication Works?

    Thank you!

  5. Working on redoing the code, however away from home for work a lot at the moment and it may take away before I can publish. Will also try to explain how authentication works. Will try and post chunks of work as I develop it, hopeful it will help.

  6. I found your website perfect for my needs. Thanks for sharing the great ideas. Whole article is too good and well written.

  7. Hi,
    thanks for this, I also had the authentication errors.

    at lines 305 and 400 my program sees a status of 2 when it is expecting a status of 0,

    at line 307 there seems to be a double negative, the error goes away if I change the preceding "if" statement
    from: if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:
    to: if not (self.Read_MFRC522(self.Status2Reg) & 0x08) == 0:

  8. Hello! That’s a great RFID tutorial. I have actually been able to follow each and every step mentioned in the post, and make it work for my business.

  9. This comment has been removed by the author.

  10. Hi.. I did everything according to your post... However.. After the last step i.e. running the python program... There was not output to be seen.. No output no error

    1. This comment has been removed by the author.

  11. Hello, I used your tutorial to use my rc522-Modul but cannot get it to work. This is my equipment;

    [*] Raspberry b+
    [*] rc522 ( this: )
    [*] I see spi_bcm2708 when typing lsmod
    [*] from MFRC522-python tells me "Welcome", but does not to anything when bringing a card nearby

    I tried:
    [*] FOUR different rc522-devices from two different shops
    [*] another Raspberry Pi ( model B )
    [*] 2A Power Adapter
    [*] fresh install of raspbian on a fresh SD-card

    I don´t think it is hardware-problem but I cannot find anything wrong...

    Thanks for your help and greetings from germany.

    Here ou can find some photos to show my problem:



    1. Steffen,

      It seems you do not have SPI enabled. Go thro' raspi-config>Advance options and Enable the SPI.
      Reboot your pi and it should work.

      - Rahul Patil.

  12. This is very interesting rfid information. buy RFID Inventory Management system at wholesale price in india

  13. Can you explain me how to write something in rfid, i run the program but nothing output... But thanks for your post, it's great...

  14. import spi
    Traceback (most recent call last):
    File "", line 1, in
    ImportError: No module named spi

  15. i need to connect five rfid(rc-522) in raspberry pi b+ . i am trying lot of things but it cannot be able to work..plse i need help

  16. This is very useful information this post RFID Software

  17. Very nice and helpful information has been given in this article. I like the way you explain the things. Keep posting. Thanks..

    RFID Access Control System

  18. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing. rfid tags

  19. i changed default key to my own key , but can't read blocks , getting "authentication error" ?

  20. hi, i am using the RIFD MC522, i have the problem when i using it with arduino. i would like to know the code number of the RIFD MC522.

  21. Hi,
    1.I connected the 7" inch LCD(800*400 pixel) which is connected to the Raspberry pi through HDMI and USB. The setup is working properly.

    2.The second link is to connect the RFID-RC522 to the Pi. This independently works without any issues.

    >>The issue is when i install both, the RFID-RC522 reader doesn't work. It aborts the connection.
    >>how we have to solve other wise how we connect the RFID-RC522 with I2C for read Tag ID
    Can you please help me out with this issue?


  22. whenever we not place the Tag correctly at time only showing the error

  23. hi,
    my one is working buI#m trying to extend the range. I want to be able to read passive tags thorugh clothes. What would you suggest? Is there an antenna for it or another type of reader?

  24. Hi
    I'm getting following error.

    can't open device: No such file or directory

  25. That's great post. I really appreciate this. This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. If your Print Spooler keeps stopping windows 10, contact professionals instantly. We are a leading destination for getting best help regarding this concern. We are available 24/7.

  26. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks for sharing! Roku provides you with the free channels and some paid as well. Further in this blog, you will be provided with the steps to activate your Roku account and tips for quick Roku account sign up.