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?
Rpi3 NOOBS python 2.7 SimpleMFRC522 cannot read Mifare tag problem.
Step 1 – Using Rpi4B Python 3 and Mfrc522 library to read Maire Blue 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.
Step 2 – 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.
Step 3 – Use Rpi4B buster python 3 and Mario Gomez’s read.py and write.py code to read and write the blue tag.
- Mario Gomez’s read.py and write.py code is very well commented and therefore most newbie friendly. (See Ref 27)
/ 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 MFRC522 Setup 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
End of Answer