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 https://github.com/lthiery/SPI-Py.git

Install the SPI-Py module by typing

cd SPI-Py
sudo python setup.py 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 MFRC522.py

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

Resources


http://www.sainsmart.com/sainsmart-mifare-rc522-card-read-antenna-rf-rfid-reader-ic-card-proximity-module.html
https://github.com/lthiery/SPI-Py
https://github.com/mxgxw/MFRC522-python



31 comments:

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

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

    I don't understand why I can Authenticate.

    One thing, I have also install he bcm2835 library.

    ReplyDelete
    Replies
    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

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

      Thanks

      Delete
  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 MFRC522.py 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?

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

      Delete
  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?

    ReplyDelete
  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 (http://fuenteabierta.teubi.co/2013/07/utilizando-el-lector-nfc-rc522-en-la.html) 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!

    ReplyDelete
  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.

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

    ReplyDelete
  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:

    ReplyDelete
  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.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  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

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  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: http://www.ebay.de/itm/301466966558 )
    [*] I see spi_bcm2708 when typing lsmod
    [*] Read.py 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:

    http://i.imgur.com/ZaCu4Jc.jpg

    http://i.imgur.com/b7cmwj1.jpg

    http://i.imgur.com/TMchGNg.jpg

    http://i.imgur.com/cpnROPR.jpg


    Steffen


    .

    ReplyDelete
    Replies
    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.

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

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

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

    ReplyDelete
  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

    ReplyDelete
  16. This is very useful information this post RFID Software

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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.

    ReplyDelete
  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?

    1.https://alselectro.wordpress.com/2016/08/20/7-touch-screen-lcd-for-raspberry-pi-driver-installation-with-virtual-keyboard/
    2.http://raspmer.blogspot.in/2015/07/how-to-use-rfid-rc522-on-raspbian.html

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

    ReplyDelete
  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?

    ReplyDelete