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?
How can Rpi python read a SPI MFRC255 RFID/NFC tag?
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.
- 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.
- 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.)
/ to continue, …
- 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
- 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.
- 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, …
(20) MIFARE – Wikipedia
to continue, …
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
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
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
[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])
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
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
Discussion on question by 627117717.pr: Trying to code a RFID tag to a MFRC522 chip using raspberry pi but it is not allowing me to read it
Imported from a comment discussion on raspberrypi.stackexchange.com/questions/109773/…