Trying to read my RFID MFRC522 using a Mifare blue tag and it is not working. Wondering if i could get any help on this as when i put the tag to read it doesn't show up as quick?
using:
Trying to read my RFID MFRC522 using a Mifare blue tag and it is not working. Wondering if i could get any help on this as when i put the tag to read it doesn't show up as quick?
using:
Question
How can Rpi python read a SPI MFRC255 RFID/NFC tag?
Short Answer
Part 1 - Introduction to I2C and SPI NFC/RFID Chips and Modules
Part 2 - Setting up SPI MFRC522 Module
Part 3 - Troubleshooting SPI Software and Hardware using Loopback
Part 4 - Using Rpi4B buster Python 3 and Mfrc522 library to read NFC Tag
I have successfully installed mfrc522 using pip3. I created a reader, wrote something to a tag, and then read back, without any problem.
Notes
I am using Rpi4B buster release 2020feb13, preinstalled python 3.7.3.
pip3 installs mfrc522 0.0.7 in /usr/local/lib/python3.7/dist-packages ...
pip3 installs spidev 3.4 and GPIO 0.7 in /usr/lib/python3/dist-packages ...
If the reader freezes/hangs when try to read or write, then it is necessary to do the SPI loopback test, to make sure the SPI software setup and hardware wiring is correct (See Appendix E below for details on using the SPI loop back tools.
Part 5 - Using Rpi4B Python 2 and Mfrc522 library to read Maire Blue Tag
The python3 mfrc522 library installed by pip3 is backward compatible to python2. So the path is set so that either python3 or python2 would import the same library installed by pip3. The following screen shows that python2 can also use the same library to read/write a tag.
Part 6 - Studying Mario Gomez's read.py and write.py code
I found Mario Gomez' library has a function for MIFAIRE Classic 1K tag. I know very little about it. So I am googling to understand it better. I found Ref 43 by the Last Minute Enginners good. I found there are many more things I don't know about, eg UID, PICC, SAK etc.
Notes
Mario Gomez's mfrc522.py read.py and write.py code is very well commented and therefore most newbie friendly. (See Ref 27)
Mario Gomez's code, as he said, is thoroughly commented, but I still found it difficult to understand or guess what this and that means. Then I realized that I actually do not have the prerequisite knowledge to understand event the basic idea of NFC. So I googled for a NFC glossary to clear my mind (Ref 43). Skimming the glossary, I now know precisely what is NFC, PCD, PICC, and most importantly what is MIFAIRE (A product of NXP Semiconductors. It is a smart card based on ISO14443A providing up to 4KB of memory on the card.)
Part 7 - Testing nRF24L01 SPI, Reset, and Interrupt Loopback
/ to continue, ...
Long Answer
The OP used Rpi3 NOOB python 2.7, and installed himself SpiPy, SpiDev for testing.
I think python 2.7 is a bit out of date. So I am repeating the OP's situation but instead using Rpi4B buster 2020feb13 (full version image), with the following buster preinstalled software:
(a) python 3.7.3
(b) thonny IDE
(c) spiDev
(d) pip3
I am using pip3 to install the MFRC522 python library, which includes the following two python3 programs:
(a) mfrc522.py (about 400 lines)
(b) simpleMFRC.py (about 100 lines)
I am using the RFID/NFC module already tested OK using libnfc-1.7.1 in I2C configuration (Appendix A).
I am using pip3 to install mrfc522, RPi.GPIO, and spidev in the following directory (Appendix B). I am not sure if the above GPIO and spidev modules are the same or different from the buster's corresponding preinstalled programs.
/usr/local/lib/python3.7/dist-packages
I am using the built-in SPI interface /dev/spidev0.0 and /dev/spidev0.1 (Appendix C)
I used python3 shell to import SimpleMFRC522 from mfrc522, and found creating a reader object OK. (Appendix D).
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
I read MFRC522-python/mfrc522/SimpleMFRC522.py/ - GitHu 2019mar26 saying the following:
@death-droid Improve compatibility with Python 3 - 2019mar26
So I guess the earlier versions of SimpleMFRC522 were not very compatible to python3. I was wondering if my pip3 installed stuff is more or less updated than the OP's corresponding software using Git Clone. I guess I better download the most updated versions of mfrc522, SimpleMFRC522, and the demo/test red/write tag programs and freeze them for later testing.
Now have tidied up the different version of the four main programs mfrc522.py, samplemfrc522py, read.py, and write.py, and put them in a penzu reading log file. Next step is to skim the two big files to get a rough picture of what is going on.
Now I have skimmed the two main programs mrfc522.py and simpleMFRC522.py. I surprisingly found the program structure is very simple. So it should not not that difficult to debug and expand. The penzu reading log is here:
Now I have tried the SPI loopback test and found it OK. (Appendix E)
I tried to repeat the OP is problem, ie, raed a tag. Still no luck, the program hanged. Because I already double checked that the NFC module can read OK the same tag using libbnfc-1.7.1 I2C mode, and SPI loopback at 50kHz is OK. So the problem is likely at the SimpleMFRC522 side. Next step is to debug Read.py, SimpleMFRC522.py and the MFRC522.py library. (Appendix J)
Now I am checking the schematic of [another similar] nfc module to make sure that my guess of the IRQ and RST wiring is correct, ie, no need to connect these two pins to Rpi. This is verified by the success of libnfc-1.7.1 I2C read card without connect the RST and IRQ pins. Perhaps I can ping the module to make sure SPI Clk, Mosi, and Miso are working OK (the previous SPI loopback only tests 50kHz and only MOSI and Miso, CS is not tested. (Appendix J)
I read the pn532 datasheet that the max SPI speed is 5MHz, so it should be OK to set SPI speed to 1MHz, 500kHz, or 100kHz. (Appendix J)
/ to continue, ...
References
(1) PN532 NFC/RFID Catalog - AdaFruit US$40
(2) PN532 NFC/RFID controller breakout board v1.6 - AdaFruit US$40
(3) RFID/NFC Guides - AdaFruit
(4) Build a Babel Fish Language Toy - AdaFruit
(7) Using with LibNFC - AdaFruit (Mainly for Arduino, a bit out of date)
(8) libfnc Main Page (Last modified 2013) - nfc-tools.org
(9) RFID and NFC Tutorial - SparkFun
(10) SparkFun RFID Starter Kit Hookup Guide
(11) PN532 NFC Module for Raspberry Pi - SunFounder US$28
(12) PN532 NFC Module for Raspberry Pi Tutorial (libnfc, C language) - SunFounder
(13) AliExpress PN532 NFC Module Catalog US$3~5
(14) How to setup a Raspberry Pi RFID RC522 Chip (SPI, python) - PiMyLifeUp
(15) pimylifeup/MFRC522 - SPI python - PiMyLifeUp
(16) Grove NFC Module (With Arduino UART, I2C Demo Programs) V1.1 - Seeed Studio 2016aug31
(17) Setting up a PN532 NFC module (V3) on a Raspberry Pi using I2C - blog of stigok 2017oct12
(18)PN532/C1 Near Field Communication (NFC) controller Rev3.6 - NXP 2017nov28
(19) PN532 User Manual (UM0701-02 Rev02) - NXP2007
(20) MIFARE - Wikipedia
(21) Amazon AZDelivery RC522 RFID Kit x 3 for Arduino and Rpi - £9.5
(22) AZDelivery RFID Kit RC522 Reviews
(23) TaoBao Risym MFRC-522 RC522 RFID Reader - ¥12
(24) MFRC522 MIFARE NTAG FrontEnd R3.9 Datasheet — NXP 2016apr27
(25) Mario Gómez MFRC522-python 2018mar26 Main Page
(26) Mario Gómez MFRC522-python 2018mar26 Read Me
(27) Mario Gómez MFRC522-python 2018mar26 Zip Download
(28) Spidev 3.4 User Guide - PyPi 2020feb19
(29) RPi.GPIO 0.7.0 pip install RPi.GPIO
(30) Pat-odoo TwoRC522_RPi2-3 - GitHub
(31) Pat-odoo TwoRC522_RPi2-3 - PDF
(32) SPI-Py GitHGub - Lousi Thiery
(33) MFRC522-python GitHub - Mario Gomez
(34) How to use three RC522 RFID readers at the same time? - Rpi Forum Q&A 2019jun08
(35) MFRC522 Library and External GPIO Program Conflicting Problem, Rpi Forum Q&A
(36) RFID Tags for Library System - Rpi Forum Q&A
(38) The Beginner's Guide to RFID Systems - AtlasRFID
(39) A practical guide to writing technical specs
(41) Mario Gomez's read.py, write.py, and mfrc522.py listing
(43) How RFID Works (IFARE Classic 1K) - Last Minute Engineers
(44) Control an I2C HD44780 20x4 LCD display with Rpi
(45) How to setup an I2C 16 x 2 LCD on Rpi - Circuit Basics 2016Apr
(46) Rpi SPI and I2C Tutorial - SparkFun
(47) Near Field Communication Glossary (MIFARE, PICC, PCD etc) - NFCResearch Lab Hagenberg
(49) Ondryaso rc-522 library listing
(50) MIFARE Classic 1K Smart card IC MF1S50YYX_V1 Datasheet R3.2 — NXP 2018may23
(51) MFRC522 Antenna Design Application Note144512 - NXP
(52) Mario Gomez MFRC522 Lirary python3 Incompatibility Problem Forum Discussion
(53) barni2000/MFRC522-python3 Module MFRC522 modified for python 3, (seems not complete)
(54) About NFC - Lady ada 2012
(55) MiFare Cards & Tags - Lady ada 2012
(56) Using with LibNFC - Lady ada 2012
to continue, ...
Appendices
Appendix A - The PN532 NFC/RFID Module V3 being tested
Reference: nfclib v1.1.7 PN532 NFC Module Testing
Appendix B - Mfrc522 software (including SPIdev and GPIO) setup record
Appendix C - Minimal configuration of SPI and I2C channels for testing the PN532 module
Appendix D - PiMyLifeUp Gus SimpleMFRC522 Library
Appendix E - SPI Loopback Test
Update 2020apr30hkt1826
Many thank for the OP pointing out a typo. If MOSI is not connected to MISO, then the out would be all zeros!
Appendix F - MFRC522 Directory Listing
Appendix G - AZDelivery RFID Kit Reviews
AZDelivery 3 x RFID Kit RC522 Reviews
Erich Eichinger - Reviewed 12 September 2019
German Quality with significantly wider sensor range than Chinese clones I had some cheap Chinese RC522 clones with a very limited range (only 1-2 mm). Thought I'd try German Quality and was not disappointed. Those RC522 are still cheap enough but detect a tag up to 1.5cm distance which was enough for my purpose.
Jürgen L. Universal and affordable 23 February 2020
With the software you have to trick a little, the instructions available on the net are somewhat outdated or refer to a particular Raspi. But if you have a little programming knowledge, you can quickly customize the Python program yourself.
Rene Winkler - Works fine, but you should use SPI 3 March 2020
The module supports SPI (preconfigured), UART and I2C. The interface must be selected via configuration pins. However, there is no pull up/down for this module. According to the data sheet you would have to pull from high to low for the UART Pin EA. In this case, however, it would mean separating a trace through and pulling a wire bridge to GND.
I'm using the module via SPI on a Raspberry Pi Zero with Python for a kids music box.
The Python library, which I first found for the module, was unfortunately out of date and did not fit the SPI library. Since something had changed in the parameters for SPI read and Write. But the module can't do anything for that.
Appendix F - PiMyLifeUp SimpleMFRC522.py and Read.py by Simon Monk
# PiMyLifeUp MFRC522 Python Library, Setup, and Example
# https://github.com/pimylifeup/MFRC522-python
# pimylifeup/MFRC522-python
# https://github.com/pimylifeup/MFRC522-
python/blob/master/mfrc522/SimpleMFRC522.py
# Code by Simon Monk https://github.com/simonmonk/
from . import MFRC522
import RPi.GPIO as GPIO
class SimpleMFRC522:
READER = None
KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
BLOCK_ADDRS = [8, 9, 10]
def __init__(self):
self.READER = MFRC522()
def read(self):
id, text = self.read_no_block()
while not id:
id, text = self.read_no_block()
return id, text
def read_id(self):
id = self.read_id_no_block()
while not id:
id = self.read_id_no_block()
return id
def read_id_no_block(self):
(status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
if status != self.READER.MI_OK:
return None
(status, uid) = self.READER.MFRC522_Anticoll()
if status != self.READER.MI_OK:
return None
return self.uid_to_num(uid)
def read_no_block(self):
(status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
if status != self.READER.MI_OK:
return None, None
(status, uid) = self.READER.MFRC522_Anticoll()
if status != self.READER.MI_OK:
return None, None
id = self.uid_to_num(uid)
self.READER.MFRC522_SelectTag(uid)
status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid)
data = []
text_read = ''
if status == self.READER.MI_OK:
for block_num in self.BLOCK_ADDRS:
block = self.READER.MFRC522_Read(block_num)
if block:
data += block
if data:
text_read = ''.join(chr(i) for i in data)
self.READER.MFRC522_StopCrypto1()
return id, text_read
def write(self, text):
id, text_in = self.write_no_block(text)
while not id:
id, text_in = self.write_no_block(text)
return id, text_in
def write_no_block(self, text):
(status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
if status != self.READER.MI_OK:
return None, None
(status, uid) = self.READER.MFRC522_Anticoll()
if status != self.READER.MI_OK:
return None, None
id = self.uid_to_num(uid)
self.READER.MFRC522_SelectTag(uid)
status = self.READER.MFRC522_Auth(self.READER.PICC_AUTHENT1A, 11, self.KEY, uid)
self.READER.MFRC522_Read(11)
if status == self.READER.MI_OK:
data = bytearray()
data.extend(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii')))
i = 0
for block_num in self.BLOCK_ADDRS:
self.READER.MFRC522_Write(block_num, data[(i*16):(i+1)*16])
i += 1
self.READER.MFRC522_StopCrypto1()
return id, text[0:(len(self.BLOCK_ADDRS) * 16)]
def uid_to_num(self, uid):
n = 0
for i in range(0, 5):
n = n * 256 + uid[i]
return n
# *** mfrc522 Installation and Example Code ****************************************************
# https://github.com/pimylifeup/MFRC522-python
A python library to read/write RFID tags via the budget MFRC522 RFID module.
This code was published in relation to a blog post and you can find out more about how to hook up your MFRC reader to a Raspberry Pi there.
Installation
Until the package is on PyPi, clone this repository and run python setup.py install in the top level directory.
Example Code
The following code will read a tag from the MFRC522
from time import sleep
import sys
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
while True:
print("Hold a tag near the reader")
id, text = reader.read()
print("ID: %s\nText: %s" % (id,text))
sleep(5)
except KeyboardInterrupt:
GPIO.cleanup()
raise
.END
Appendix G - Spidev 3.4 User Guide - PyPi
Spidev 3.4 pip install spidev - Python bindings for Linux SPI access through spidev - PyPi 2020feb19
[a] xfer(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be released and reactivated between blocks. Delay specifies the delay in usec between blocks.
[b] xfer2(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be held active between blocks.
[c] xfer3(list of values[, speed_hz, delay_usec, bits_per_word])
Similar to xfer2
but accepts arbitrary large lists.
If list size exceeds buffer size (which is read from /sys/module/spidev/parameters/bufsiz
),
data will be split into smaller chunks and sent in multiple operations.
Appendix H - MRFC532 SpiDev LoopBack Test and Wiring Length Limit
Appendix I - Mario Gomez MRFC522 Wiring Diagram and Software Requirements 2014
Appendix J -Long Answer Points 12, 13, 14
Appendix K - Single MFRC522 Mdoule Wiring Diagram
Appendix L - Multiple MFRC522 Module Wiring Diagram
Appendix M - Dual MFRC522 Module (One for SPI 0.0, another for SPI 1.0)
Appendix N - Python program to ping two modules
So I have written a python program to ping two modules. There are two versions. The "read-only" version is to read the software version register at 0x37 whose contents is 0x92 or 0x12. The "read-after-write" version is to first write the interrupt config register at 0x02 and then read it back. The ping program is scalable, from one module to some 16 or more modules. The main test function is sample output is listed below:
def main():
initSpiBusNameListMode00Speed100kHz('SpiFiveBusNameList')
testReadMfrc522RegByName('SpiBus10', 'VersionReg')
testReadMfrc522RegByName('SpiBus11', 'VersionReg')
return
Sample Output
Run fnfc310.py
Begin Execute Function testReadMfrc522RegByName 2020-04-07 21:08
Function Name = testReadMfrc522RegByName
Spi Bus Name = SpiBus10
Dev Reg Name = VersionReg
Dev Reg Addr = 0x37
Dev Reg Contents = 0x92
End Execute Function testReadMfrc522RegByName 2020-04-07 21:08
Begin Execute Function testReadMfrc522RegByName 2020-04-07 21:08
Function Name = testReadMfrc522RegByName
Spi Bus Name = SpiBus11
Dev Reg Name = VersionReg
Dev Reg Addr = 0x37
Dev Reg Contents = 0x12
End Execute Function testReadMfrc522RegByName 2020-04-07 21:08
Appendix O - Tri MFRC522 Module Board
A practical guide to writing technical specs
Appendix P - Multiple MFRC522 Board For Pair/Swap Troubleshooting
Appendix Q - Reviews on the Mario Gomez' MFRC522-Python Library
(1) (a) Important notice: This library has not being actively updated in almost four years. It might not work as intended on more recent Raspberry Pi devices. You might want to take a look to the open pull-requests and forks to see other implementations and bug-fixes. (b) Requirements: This code requires you to have SPI-Py installed from the following repository: https://github.com/lthiery/SPI-Py - Mario Gomez, README.MD, MFRC522-python, GitHub, 2014.
(2) With the software you have to trick a little, the instructions available on the net are somewhat outdated or refer to a particular Raspi. But if you have a little programming knowledge, you can quickly customize the Python program yourself. - Jürgen L 2020feb23 Appendix G - AZDelivery RFID Kit Reviews
(3) (a) There is no pull up/down for this module. According to the data sheet you would have to pull from high to low, ... it would mean separating a trace through and pulling a wire bridge to GND. (b) The Python library, which I first found for the module, was unfortunately out of date and did not fit the SPI library. Since something had changed in the parameters for SPI read and Write. But the module can't do anything for that. - Rene Winkler 2020mar03 Appendix G - AZDelivery RFID Kit Reviews
Appendix H - Mfrc522 Self Test Function
Now I am write test functions to make sure my module is more or test working OK.
Appendix I - Writing MFRC522 Commands in python3
Now I am reading the datasheet to learn how to write MFRC522 commands in python 3. I have written a python execMfrc522Command function and can now do 'SoftReset" and 'Idle' with the two statements below:
execMfrc522Command('CommandReg', 'Reset')
execMfrc522Command('CommandReg', 'Idle')
Appendix J - Adding Python 3 Timer and Interrupt functions to Mario Gomez's MFRC522 Python 2 Library
End of Answer