Tuesday, July 7, 2015

How to use RFID-RC522 on Raspbian



Before use RFID-RC522. Enable SPI Interface (Update 2018-09-28)

Current kernel in document (Raspbian GNU/Linux 9 Kernel Version 4.14.70-v7+ #1144)

Enable SPI Interface
- Use command
$ sudo raspi-config
select menu
5 Interfacing Options -> SPI -> Yes -> OK -> Finish
Or

- Edit on /boot/config.txt.

change this line

dtparam=spi=on


Edit on /etc/modprobe.d/raspi-blacklist.conf

add # (comment) on line "blacklist spi-bcm2708"

#blacklist spi-bcm2708

Reboot Raspberry PI

sudo shutdown -r now

Re-Check SPI Interface software

$ dmesg | grep spi
[    5.408904] bcm2708_spi 20204000.spi: master is unqueued, this is deprecated
[    5.659213] bcm2708_spi 20204000.spi: SPI Controller at 0x20204000 (irq 80)

$ lsmod | grep spi
spidev                 16384  0
spi_bcm2835            16384  0


If you found, software is enabled.

Install Software for use RFID-RC522
Install python-dev

sudo apt-get install python-dev

Install SPI-Py (Hardware SPI as a C Extension for Python)

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

Install MFRC522-python

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

Install Python RC522 library

$ git clone https://github.com/ondryaso/pi-rc522.git
$ cd pi-rc522
$ sudo python setup.py install


Install Pins between Raspberry PI and RFID-RC522

RFID-RC522 Pin
 Raspberry PI Pin
Raspberry PI Pin name
SDA
  24
    GPIO8
SCK
  23
    GPIO11
MOSI
  19
    GPIO10
MISO
  21
    GPIO9
IRQ
  18
    GPIO24
GND
  Any
    Any Ground
RST
  22
    GPIO25
3.3V
  1
    3V3

You can test read card.

$ cd examples
$ python Read.py
Starting

If touch the card, it will display.

Detected: 10
Card read UID: XX,XX,XX,XX
Setting tag
Selecting UID [XX, XX, XX, XX, XX]

Authorizing
Changing used auth key to [XX, XX, XX, XX, XX, XX] using method B

Reading
Calling card_auth on UID [XX, XX, XX, XX, XX]
S1B0: [0, 0, 105, 36, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Deauthorizing
Changing auth key and method to None
^C
Ctrl+C captured, ending read.

And test write card.

$ python KeyChange.py
Starting

Detected: 10
Card read UID: XX,XX,XX,XX
Setting tag
Selecting UID [XX, XX, XX, XX, XX]

Authorizing
Changing used auth key to [255, 255, 255, 255, 255, 255] using method A

Writing modified bytes
Calling card_auth on UID [XX, XX, XX, XX, XX]
Changing pos 2 with current value 0 to 105
Changing pos 3 with current value 0 to 36
Changing pos 4 with current value 0 to 64
Writing [0, 0, 105, 36, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to S1B0
Not calling card_auth - already authed
S1B0: [0, 0, 105, 36, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Calling card_auth on UID [XX, XX, XX, XX, XX]
Changing pos 0 with current value 0 to 18
Changing pos 1 with current value 0 to 52
Changing pos 2 with current value 0 to 86
Changing pos 3 with current value 0 to 120
Changing pos 4 with current value 0 to 150
Changing pos 5 with current value 0 to 146
Changing pos 6 with current value 255 to 15
Changing pos 7 with current value 7 to 7
Changing pos 8 with current value 128 to 143
Changing pos 9 with current value 105 to 105
Changing pos 10 with current value 255 to 116
Changing pos 11 with current value 255 to 0
Changing pos 12 with current value 255 to 82
Changing pos 13 with current value 255 to 53
Changing pos 14 with current value 255 to 0
Changing pos 15 with current value 255 to 255
Writing [18, 52, 86, 120, 150, 146, 15, 7, 143, 105, 116, 0, 82, 53, 0, 255] to S1B3
Changing auth key and method to None
Stopping crypto1

You can learning python code for use RFID read and write data.



Python RC522 libraryhttps://github.com/ondryaso/pi-rc522



Example Project for use NFC and Raspberry PI


  • Attendance system using Raspberry Pi and NFC Tag reader by Yim

http://www.instructables.com/id/Attendance-system-using-Raspberry-Pi-and-NFC-Tag-r/


Reference : 

Device Tree overlays of Raspberry PI Firmware
MIFARE (MFRC522) datasheet

36 comments:

  1. Hello, I get "AUTH ERROR(status2reg & 0x08) != 0" errors when I write data on the tags with my smartphone before I read them with the rc522(Read.py).
    (It works like in your example if a remove the Data again)

    I also get this error when I use other Tags like the NTAG213! Any Ideas what causes my problem ?

    ReplyDelete
  2. Merci beaucoup pour ces configurations qui ont fait refonctionner mon rc522 avec mon pi :)
    Il avait toujours fonctionner puis du jour au lendemain plus rien. Les lignes rajoutées dans le /boot/config.txt ont résolu le problème

    ReplyDelete
  3. It is a good Quality RFID Post thanks for sharing .

    ReplyDelete
  4. Sorry ge3ko1. My work is very busy.

    SPI-Py and MFRC522-python are example code.

    Maybe data structure (Data format) of card (write with smartphone) difference from example code for read it.
    It display error message.

    I should find more information and modify example code for use it. Or find new library for read data from card (write with smartphone).


    Good luck ;)

    ReplyDelete
  5. very nice for get information use rfid-RC522 on Raspbian .

    ReplyDelete
  6. Hi,I following you tutorial..but when i touch the card..it dooesnt have any information from my card and it without any error massage..how can i solve it?

    ReplyDelete
    Replies
    1. I think you use new raspbian image or new kernel. (4.4.x)

      spi-bcm2708 is missing on new kernel.
      and Device Tree overlays parameter changed.

      change file /boot/config.txt with below line.

      dtparam=spi=on

      or use sudo raspi-config command to enable SPI

      see more information
      https://github.com/raspberrypi/firmware/tree/master/boot/overlays

      If I have free time, I will check it again.


      See on post by xvolte
      https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=106313&sid=c2f08cd89fee0405fa1422b70fdfce72&start=25

      Maybe it can help use.

      Change the RST PIN to PIN 18.
      Change on MFRC22.py file. (Change spidev and pin)

      https://github.com/mxgxw/MFRC522-python/blob/master/MFRC522.py

      Good Luck ;)

      Delete
  7. Any idea as to the instructions for doing this on a raspberry pi 3?

    ReplyDelete
  8. Thanks for sharing informative post with us.
    Daccess security services provider in pune.
    vehicle tracking system suppliers in mumbai

    ReplyDelete
  9. Works exactly the same on PI 3. Don't forget to enable SPI

    ReplyDelete
  10. does not work the same on pi 3 been looking for a solution for hours

    ReplyDelete
  11. Dear ,
    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.

    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
    Replies
    1. RFID-RC522 is SPI Interface. It cannot connect with I2C.

      I don't know driver of 7" inch LCD. It will have some problem
      with SPI.???

      Maybe you should recheck about SPI config. It should enabled.

      Delete
  12. I wanted a c program for rc522 rfid sensor. Where will I find one? The python one runs perfectly, but I need a c program for my project

    ReplyDelete
    Replies
    1. Try to see.

      https://github.com/paguz/RPi-RFID
      https://github.com/paulvha/rfid-rc522
      https://dev.hawry.net/rc522-on-the-raspberry-pi-without-python/

      Delete
  13. Not sure if my comment went through...
    I was asking if I can connect my RFID reader/writer to these pins: https://ibb.co/fNxuH6 since I am using a touchsreen. There is also a conflict with SPI since both use it so should I change the RFID to CE2? If so where in the code should all this be initialized?

    ReplyDelete
    Replies
    1. Found this tutorial on how to enable these pins: https://tutorials.technology/tutorials/69-Enable-additonal-spi-ports-on-the-raspberrypi.html

      Now need to figure out how to modify the RFID code so that it matches these new pins setup.

      Delete
    2. I think.
      If you use spi1-3cs overlay.
      https://tutorials.technology/tutorials/69-Enable-additonal-spi-ports-on-the-raspberrypi.html
      SPI1
      GPIO20 (Pin Header Number 38) – MOSI
      GPIO19 (Pin Header Number 35) – MISO
      GPIO21 (Pin Header Number 40) – SCLK
      GPIO18 (Pin Header Number 12) – CE0

      See more detail of overlay and parameter
      https://github.com/raspberrypi/firmware/tree/master/boot/overlays

      check about correct pin on raspberry pi.

      and change the code from /dev/spidev0.x to /dev/spidev1.x for use spi1


      *** I never use or test spi1

      Have a good luck

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

    ReplyDelete
  15. Hello

    i need help. i want to use this with a web interface where you can see the checks in a web page. i hope someone could help me

    thanks a lot

    ReplyDelete
  16. howdy was quite recently checking whether you disapproved of a remark.
    https://www.starnfc.com/

    ReplyDelete
  17. Thank you very much for this post. I didn't notice the IRQ pin to PIN 18 in other posts and because of that I have been looking for a way to fix it for a long day.

    ReplyDelete
    Replies
    1. Welcome. Good news for you.

      I updated this post for fix RFID-RC522 not work with new kernel version of Raspbian.
      The new kernel use spi_bcm2835 instead spi-bcm2708.
      Include IRQ pin to PIN 18 for pi-rc522 library.

      Hope you enjoy with it.

      Delete
  18. Hi, Its not working for new kernel version. I did all your steps. How do I fix this?

    ReplyDelete
    Replies
    1. What's about you problem? Please explain.

      Maybe you try to check your connection pin. Is it all correct?


      Delete

  19. Thanks a lot for the good News, your Post was very helpfull. Could you please give a wiring-example how to connect two rc522 to Rpi3 using irq-pin and spi0/ce0/ce1 ? Thank you in advance for ervery hint.

    ReplyDelete
    Replies
    1. GPIO - Raspberry PI Document
      https://www.raspberrypi.org/documentation/usage/gpio/

      SPI PIN
      - SPI0: MOSI (GPIO10); MISO (GPIO9); SCLK (GPIO11); CE0 (GPIO8), CE1 (GPIO7)
      - SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK (GPIO21); CE0 (GPIO18); CE1 (GPIO17); CE2 (GPIO16)

      You can connect use SPI0 for first and SPI1 for second of RFID-RC522 and select IRQ-Pin other pin for second of RFID-RC522.

      Then change spidev device to 1 for spi1 and pins in code file for support them.

      https://github.com/ondryaso/pi-rc522/blob/master/pirc522/rfid.py

      *** I don't test it.

      Delete
  20. Hello,

    i want to override the keys i have. Will change the ID from Number xxxxxxxxx to a name. What i have to do with the KeyChange.py??

    thx

    ReplyDelete
    Replies
    1. https://blog.atlasrfidstore.com/rfid-basics-read-write-tags

      https://www.getkisi.com/blog/how-to-calculate-facility-code-using-card-bit-calculators

      https://blog.atlasrfidstore.com/encoding-rfid-tags-3-things-to-know

      Delete
  21. Man, thanks for updating this for the new kernel version, love you

    FZP

    ReplyDelete
  22. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.

    RFID key cards
    RFID key cards hotel
    Custom hotel key cards

    ReplyDelete
  23. hi i am using raspberry pi 3 B + with latest raspbian stretch.. i followed your steps..all my pins are correctly connected but when i start the read.py code and touch the rfid tag to the reader, nothing happens..any help please???

    ReplyDelete
  24. Thanks for sharing your valuable information and time. Please keep updating.
    RFID Singapore

    ReplyDelete
  25. Relevant post! Keep sharing such kind of post. Explore DsaTech for CCTV Installation Services and all your security needs.

    ReplyDelete