วันอังคารที่ 7 กรกฎาคม พ.ศ. 2558

How to use RFID-RC522 on Raspbian



Before use RFID-RC522. Enable SPI Interface (Device Tree Overlays)

On new kernel use Device Tree Overlays.

Edit on /boot/config.txt.

add new line

device_tree_param=spi=on
dtoverlay=spi-bcm2708


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
spi_bcm2708             6010  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 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
  None
    None
GND
  Any
    Any Ground
RST
  22
    GPIO25
3.3V
  1
    3V3

You can test read card.
sudo python Read.py
Welcome to the MFRC522 data read example
Press Ctrl-C to stop.
If touch the card, it will display.

Welcome to the MFRC522 data read example
Press Ctrl-C to stop.
Card detected
Card read UID: XX,XX,XX,XX
Size: 8
Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Ctrl+C for Stop.

And test write card.

$ sudo python Write.py
Card detected
Card read UID: XX,XX,XX,XX
Size: 8

Sector 8 looked like this:
Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Sector 8 will now be filled with 0xFF:
4 backdata &0x0F == 0x0A 10
Data written

It now looks like this:
Sector 8 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]

Now we fill it with 0x00:
4 backdata &0x0F == 0x0A 10
Data written

It is now empty:
Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

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



and other python library : https://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
SPI-PY
MFRC522-python
MIFARE (MFRC522) datasheet
PI-RC522

15 ความคิดเห็น:

  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 ?

    ตอบลบ
  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

    ตอบลบ
  3. 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 ;)

    ตอบลบ
  4. very nice for get information use rfid-RC522 on Raspbian .

    ตอบลบ
  5. 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?

    ตอบลบ
    คำตอบ
    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 ;)

      ลบ
  6. Any idea as to the instructions for doing this on a raspberry pi 3?

    ตอบลบ
  7. Thanks for sharing informative post with us.
    Daccess security services provider in pune.
    vehicle tracking system suppliers in mumbai

    ตอบลบ
  8. Works exactly the same on PI 3. Don't forget to enable SPI

    ตอบลบ
  9. does not work the same on pi 3 been looking for a solution for hours

    ตอบลบ
  10. 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

    ตอบลบ
    คำตอบ
    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.

      ลบ
  11. 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

    ตอบลบ
    คำตอบ
    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/

      ลบ