Uncategorized

MFRC532 NFC notes

627117717.pr
627117717.pr
Sun 22:47
SO did each step by step the link you said i should try still NO LUCK still can not read or write the tags
tlfong01
tlfong01
Thank you for your clarification. So perhaps I can try your first tutorial. If no luck, then I can try the second tutorial. Wish me luck. Cheers.
627117717.pr
627117717.pr
Ok that sounds good if your ok with that and if the tutorials work for you then must be something software or maybe hardware that is going wrong on my side!
tlfong01

tlfong01
2878
Well, I just finished reading the tutorial by Gus of PiMyLifeup, also skimmed the 70+ comments. I understand 90% what is going on, except the SPI library which seems not exactly the same library I am using. This might be one thing I might get stuck. Anyway, I am now starting to follow the tutorial, not step by step, but skipping steps and using not exactly the same tools. Eg, I will use Rpi4B buster Thonny IDE to edit and run the programs. (One reader commented using IDLE python 3, but IDLE is not longer available), / to continue, …
627117717.pr
627117717.pr
Thats fine i was using the Terminal to do everything in and the code coming from the pi up my life website just to see if it worked or not and as of yet still not working
tlfong01

tlfong01
2878
No problem. I can also use CLI bash, pip, nano etc for the preliminary setsup, and perhaps more GUI later. I am getting started with the PN532 module which I tested OK with I2C some time ago. Now I am switching from LibNfc library to PyMyLifeUp library.
Yesterday evening I already used “pip3” to install “mfrc522”. As I said earlier, I don’t know nothing about SPI-Py, so I did not use the newbie scary “git-clone” and “python setup” to install the SPI-Py module. I noticed that what you built is a python 2.7 SPI-Py module. But I started learning python already with Rpi IDLE python 3.5.3. So I am not going back to the old python 2.7. Just now I checked out that pip3 seems happy with pip3 installed GPIO, SpiDev, and mfrc522 requirements. So I am hopping that I don’t need SPI-Py and go straight to python programming.
627117717.pr

627117717.pr
11
Sun 22:47
HI have you had anymore luck in trying to use the mfrc522 module to see if it reads any tags by any chance as thats the one im more focussed on?
if the same outcome for the mfrc522 can be achieved like testing the tag pn532 nfc module that would be great as i still can get anything to read what so ever!
tlfong01

tlfong01
2878
No luck! Now I am googling to debug. I need to make sure a couple of things, including: (1) Must I use python 2? (2) Must I use SPI-Py and not buster preinstalled SPI? You might like to check out the following discussion: raspberrypi.org/forums/viewtopic.php?t=207328#p1282986. Ah, supper time, see you tomorrow.
I just found out that I have forgotten to make the reset pin connection. So my module is not reset to start working. There might be other careless mistakes.
I also found out that there are at least two mfrc522 gitHub library. Perhaps I ppip install the wrong library: github.com/mxgxw/MFRC522-python. Of there are other reasons, say the library is only for Rpi 2, 3, not for Rpi4 buster. I also check the source code that SPI speed is set to 10MHz, which is too high for my long wiring. I need to set SPI speed lower, perhaps 400kHz. In short, too many possible bugs. libnfc 1.1.7 is very smooth, it took me only less than 30 minutes. Perhaps I tried the SPI librnfc version to verify that SPI speed can be set to 10MHz.
I also read that PN532 old versions 1.x are not reliable. So everybody is using V3. Are you on V3?
The following also says must lower SPI frequency from 5MHz to 50kHz. That means PN532 cannot do high frequency: wiki.sunfounder.cc/….
Now I am thinking of going back to libnfc and test the spi version, to make sure both libnfc v1.1.7 I2C and SPI version are OK, then I will come back to do PyMyLifeUp SPI version. I am also configuring minimal number of SPI, I2C, and UART channels/ports.
627117717.pr
627117717.pr
Sounds good the libnfc version is looking good so far hopefully the same will work for the mfrc522 chip and tags
13 hours later…
tlfong01

tlfong01
2878
Mon 11:42
I am still debugging. About SPI Py, my quick and dirty conclusion is that it is an old thing, developed 7 years ago, in the Rpi2, python 2.7 days. Gus’s tutorial started 2017, so he might use SPI Py which is universal for the general Rpi users.
Please let me know if you know a reason that we must use PI Py and python 2.7. Otherwise I will still try pip3 mfrc522 and python 3. I am also using Version 3 module. I will also try the Blue PCB Version 1, just to make sure yours V1 module is not working.
Your question says: “read RFID MFRC522 Mifare blue tag not working. … when i put the tag to read it doesn’t show up as quick?” (1) To read quick, you need to use the module interrupt output signal to interrupt Rpi, otherwise loop scanning might be slow,
(2) But your problem is not quick or slow, because the module might be reset/ setup ready to read, or your tag is bad. I forgot to wire the module reset wire to Rpi, so this might be a bug, among other more possible bugs. So I am going to wire “interrupt” and “reset” between Rpi and the NFC module. See you later.
tlfong01

tlfong01
2878
Mon 12:21
Now I am reading the following tutorial to learn how to connect the Reset pin. I found that the signal Low power downs the modules, and Rising Edge resets the module. In other words, I can just connect a jumper wire to manually reset the module. It is a bit confusing if I can leave the Reset pin floating. I need to check the schematic to see if this pin is pulled high.
user image
Sorry, I forgot to include the web link in the above comment. Here it is: “What is RFID? How It Works? Interface RC522 RFID Module with Arduino”:
lastminuteengineers.com/….
The LatMinuteEngineers’s colourful MiFare Classic 1k memory map is also very good to refresh my memory.
user image
4 hours later…
tlfong01

tlfong01
2878
Mon 16:27
Now I am going to look at the SimpleMFRC522.py library module, add debug print statements for debugging.
user image
627117717.pr

627117717.pr
11
Mon 17:18
@tlfong01 Hi would preferably want to use python 3 instead of 2.7, just really want to see if it works on your side so if it does i can see where i am going wrong and to basically use this a basis and manipulate the code for the mini project i am doing.
for me personally i just want to know the reason why it isnt working becasue even purchasing a brand new module and tags the same output is coming up you see
The mfrc522 modules are on and working i think, the tags dont read or even write when being placed near the chip and the code and wiring is copied exactly from the website and struggling to find where the error could be unless like you said i coul have missed parts out or the blue tags dont work.
quite new to this thing python and the raspberry pi just want to get to the bottom of this!! appreciate you helping out by the way !!
tlfong01

tlfong01
2878
Mon 17:57
@627117717.pr Just quick random comments. (1) python 2.7 supported is ending this year 2020. So there is no point trying to debug python 2.7 programs. (2) You seem to have installed python 2.7 compatible SpiDev and SPIPy and perhaps also pip2/git/clone python 2.7 version of mfrc522. That is why I skipped Your or Gus first instructions to install SPI Py and SpiDev. I only use pip3 to install mfrc522.
Since lsmod spi says spidev and spi_bcm2835 are installed, so SPI part should be OK. pip3 install update also says the other installed modules are OK. So I guess I can do everything in python 3. Actually use bash to open python 3 shell and can import mfrc522 without problem, so I think there is 90% chance I can move on to look at the mfrc522 library to see how to debug.
I found the main library/module is only 400 lines of python statements, and seems well documented, so it should be easy to do minor fixes, such as lowering SPI speed from 1MHz to 50kHz and so on. I am not going to move the main library mfrc500.py and the SimplePI speed from 1MHz to 50kHz and so on. I am not going to make a copy of the main library mfrc500.py and the smaller, 100 lines SimpleMFRC522 in a penzu reading log file and make more newbie comments.
If you don’t have much experience in Rpi and python, then perhaps you can just sit back and watch what I am messing around. But you are welcome to make suggestions, such as preferring python 3 and not python 2.7.
tlfong01

tlfong01
2878
Mon 18:18
@627117717.pr This penzu programming log file has the mfrc522 library (actually just a 400 line python program) and the SimpleMfrc522 (about 100 lines) . You can easily find the SPI speed setting in the main class. So it is easy to hack. Penzu programming log: penzu.com/p/08d4fdf2. Ah supper time! So see you late this evening or tomorrow.
1 hour later…
627117717.pr
627117717.pr
Mon 19:43
thats great sounds like a plan hopefully can get this working asap
2 hours later…
tlfong01

tlfong01
2878
Mon 21:44
@627117717.pr Ah, I am just a nfc newbie, scrapping the surface. So far I only have luck with libnfc-1.7.1 in I2C config. I am trying the other config, SPI. and UART, but I found things about SPI I don’t understand, so it might take me days to google to learn more about SPI. For SimpleMFRC522, I am googling to learn more, before I really starts. By the way, I read the following Q&A and saw a guy using SimpleMFRC, but I don’t know he ahs any problem with the SPI speed.
Anyway, if you have a tight deadline, then try libnfc I2C mode, because I have given the answer in the stackOverflow forum. Or google around harder.
This is the guy who had luck using SimpleMRFC: raspberrypi.stackexchange.com/….
tlfong01
tlfong01
Mon 22:12
I searched this forum for SimpleMRFC and found 11 results. Other forums might have many more we can read and learn something.
2 hours later…
627117717.pr
627117717.pr
Mon 23:48
nice one will look for sure
10 hours later…
tlfong01

tlfong01
2878
Tue 9:19
I read your question a second time. It appears that you were following two tutorials at the same time, the first one using python 3, second one python 2.7. One tutorial uses SpiPy, and compiled SpiDev, the other does not. I am not sure if you are also using pip/pip2/pip3 at the same time.
If you use pip2 first, the buster might set default to python 2 envrionment. Then if you use pip3 later, the piped drivers might go to the python 2 rooms, causing big mess, or a small dark hole, where garbage goes in will stuck and never come out. So I would suggest the following. Get two unwanted uSD cards, flash new buster, one for buster all preinstalled python 3, pip 3, spidev, only installed is pip 3 install mfrc522.
The above is what I did and summarized in this chat. Now you are in sync with my current configuration. And if you get stuck again, like a reading problem, then I can repeat your situation and program. The SimpleMFRC522 tutorial seem to write first and then read later. But you seem to skip the write part and jump to read part. I need to make sure if the write part does init something that is assumed in the following read part, …
In the mean time, I would study the 400 line MFRC522 library and the 100 line SimpleMFRC program, to see what print statements can be inserted to help debugging.
7 hours later…
627117717.pr
627117717.pr
Tue 16:43
When looking at the simplemfrc522 and mfrc522 code both dont seem to print the output after trying to read the tag but does before i.e “place tag to scan” and ‘new data’ where youd type it in
tlfong01
tlfong01
Tue 17:15
Ah, I have not yet started looking at the code. I need to catch up now.
627117717.pr
627117717.pr
OK thats good hopefully if we get this done asap can find where maybe i am going wrong maybe even a simple error on my part but grateful for the help!!!
tlfong01

tlfong01
2878
Tue 18:04
Ah, take it easy. I am a slow guy, walking slowly in a locking down city. I just finished a quick reading of the two big program and found it simple. The reading log is here:
Ah, supper time! See you late this evening or tomorrow. Cheers.
627117717.pr
627117717.pr
No problem
4 hours later…
tlfong01

tlfong01
2878
Tue 21:39
Now I am zooming in to the critical functions to see where to insert the print statements for debugging. Perhaps we can start with the Write.py program, trace it to as you said, around “where place tag to scan … and new data where you type it in …” I have now made a short listing to show where to insert the print statements:
This is the short listing: penzu.com/p/585c2995. You might trace the simpleMRFC522()’s read function back to the m\mrfc522’s read function which use the low level SPI functions. If the read steps get stuck at the SPI functions, then perhaps (1) Your SPI hardware wiring is bad, or (2) SpiDev is not compaitble to the MRFC module. I think (2) is unlikely. (3) You mixed up python 2 spidev tools with python 3 mrfc modules.
627117717.pr
627117717.pr
ok great where would that be because all im doing is copying a pasting the code from “PI UP MY LIFE” and adding it in to the sudo nano read.py or write.py
tlfong01

tlfong01
2878
I suggest you and me doing the following to debuig: (1) BACKUP a couple of micro SD card copies for messing up testing (Note 1). (2) run the Read.py again, inserting print statements when tracing back to where it hangs. (3) If anytime you suspect the spi hardware and software setup is bad, you need to do the SPI LOOP BACK test (I already did it to make sure it is the cause of trouble. Note 1- You know how to use Rpi GUI accessories > SD copier to make back up copies, don’t you?
Ah the simpleMFRC532 and the mrfc532 programs are located in the following directory:/usr/local/lib/python3.7 …, but is you used git/clone, or pip 2, then it might be in /usr/local/lib/python2.7, not very sure. Let me show you the picture again.
user image
This picture is sort of “cheat sheet”. The top dark/black part helps to make sure the right tools are in the right places. The bottom white part shows where are the two most important programs you are going to insert the debug print statement. Of course you the newbie will mess up things:( That is why I said you must make backup SD cards. Of course you also can back up the programs inside the directory.
Actually for initial testing, you can still use your current python 2.7 tools. Since you can do the last two statements “import simple…” reader = … , that means your python 2.7 setup is also OK, perhaps it is only your SPI wiring is bad, …
Ah bed time! I call it a day. Good luck and see you tomorrow.
tlfong01

tlfong01
2878
Tue 22:17
Two more things: (1) If you are weak in Rpi and python, AND your schedule is tight, you might like to consider (a) libnfc-1.71. I2C mode, which I found OK, (b) Arduino library, which is very mature and easy to find support, (c) Buy more boards to stand by and/or cross check. Reference: Arduino library for MFRC522
github.com/miguelbalboa/rfid

Before buy:

Please notice that there are many sellers (ebay, aliexpress, ..) who sell mfrc522 boards. The quality of these boards are extremely different. Some are soldered with wrong/low quality capacitors or fake/defect mfrc522.

(see full text)

Good luck and cheers.
13 hours later…
tlfong01

tlfong01
2878
Wed 11:44
@627117717.pr So we can debug the Read.py on both sides, you python2.7, me python 3.7. The trick is just insert print and pause statements at breakpoints of sampleMfrc532 and mfrc532 and see if the program is stuck and hangs/freezes at the low level SPI read functions.
If it is indeed the case, then we need to debug SPI by first using the LOOPBACK method, just to make sure if (a) spiDev and python are compatible, (b) if hardware wiring are correct (Note: MOSI and MISO must crossover). You can search “SPI Loopback” in this and other forums for more details. Below are some posts you might like to start with.
SPI Loopback references: (1) SPI Loopback Test – rpi.org 2019
raspberrypi.org/documentation/…

(2) SPI Loopback Test using Python (See Appendix C for the python program) – rpi.stackexchange 2019
raspberrypi.stackexchange.com/…

(3) SPI Loopback Test (struct spi_ioc_transfer level) – rpi.org forum 2019
raspberrypi.org/forums/…

(see full text)

3 hours later…
tlfong01

tlfong01
2878
Wed 14:33
So I have done the SPI loop back test OK.
user image
Next step is to use a multi-meter to check the open/short wiring of SPI connection of MRFC522 module to Rpi.
2 hours later…
627117717.pr
627117717.pr
Wed 16:58
Ok thats good everything else ok?
5 hours later…
tlfong01

tlfong01
2878
Wed 21:44
Well, going slow. (1) Appendix E shows where mfrc522.py and SimpleMFRC522.py are hiding. (2) I don’t want to run the programs in their hiding place, worrying that I might mess up things. Instead made a copy of the two programs in a newly created directory “/home/testMfrc”. Now I am testing my version of Write.py which import mfrc522 and try to write something to the tag.
Now a question: Do you have any experience in OOP (Object Oriented Programming), such as Arduino C++? If you are not experienced in nay OOP language, then I can forget the mrfc Class thing and directly do everything using functions, ie FP (Functional Programming).
Question 2 – Do you have a web link to the NFC module your are playing? Is it a cheapy eBay Version 1? I remember when I bought my Version 3 NFC module, the shop says they are Version 3, and that other shops’ Version 1 are bad.
2 hours later…
627117717.pr

627117717.pr
Wed 23:25
Hi again, i do have experience in OOP C++ but its been a long time since and forgot most of it tbh, and the link i bought it from amamzon is amazon.co.uk/…
Hopefully this helps
18 hours later…
627117717.pr
627117717.pr
yst 17:00
Any luck with the MFRC522 module tutorial and debugging?
1 hour later…
tlfong01

tlfong01
2878
yst 18:22
Ah, still walking slowly. A couple of random comments: (1) Your module from Amazon UK looks good quality. I skimmed the comments and found some of them useful – such as that the SimpleMFRC522 is not updated.
I visited the MFRC522 GitHub and found the following 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.”
I also found Mario Gómez’s original MFRC522 GitHub version very good. Eg, his read/write program has good documentation, comparing to SimpleMFRC522 which is too brief.

So as I said earlier, I will forget SimpleMFRC522 and directly use Mario Gómez’s original version to learn what he is doing. He also explains about the requirements of SPI-Py and GPIO, both of which I suspect is not standard, and might not be compatible to Rpi4B buster. But I am only 50% sure.

Now I am thinking of writing a very small version of MarioGomez’s MRFC522 class, but not an OO class, instead a couple of python functions. The objective is checking out if a module is basically working. The checking out method is to “ping”, or write something to a MRFC522 register and read back later.
627117717.pr

627117717.pr
11
yst 18:41
HI that’s what i was orginally doing! using SMIPLEMFRC522 ill try using the more direct version and see how thats goes hopefully works !!
the objective of all of this is to see if the tag can read/write properly ill try the marios code and see if that is any better !!
tlfong01

tlfong01
2878
yst 19:28
Ha, yes, so I am back to Square 1 – What you were originally doing. Now I have listed the programs I unzipped into a penzu entry for easy reference: penzu.com/p/dff77f0d
But have you already done the SPI loopback check? The reason that you could not read the tag might be because your SPI hardware wiring is broken. If that is the case, you just fix the broken SPI wiring, then your problem should disappear! 🙂 Anyway, late supper time. See you very late this evening or tomorrow. Good luck, cheers.
627117717.pr

627117717.pr
ok nice one mate ill try the loopback right now and update you after!
tried the loopback test
how can i attach a picture to show you?
1 hour later…
tlfong01

tlfong01
yst 20:54
Ah, to display an image, I usually do this: (1) Use any camera to shoot a 2MB or smaller jpeg image, and copy to a PC Windows directory, (2) click the upload button, browse to the directory and click the image name. I sometimes use imgur.com to upload the images and then paste to the answer in this forum.
627117717.pr
627117717.pr
ok no problem will do so
how would i do this on rasperry pi im new to this thing not sure how to!
tlfong01

tlfong01
2878
yst 21:20
I did’t catch you. Did you mean how to test Mario Gomez’s program in Rpi. What I am going to do is the following. (1) Download the zipped programs from GitHub to Win10 PC, (2) Unzip to find the three important programs: (a) mfrc522.py, (b) read.py, (c) write.py. In Rpi, create a new directory under Rpi home directory, say “test_mrfc522”. Copy the above three programs in this new directory, (c) I will use Rpi Desktop GUI Thonny IDE to run read01.py.
You might use terminal command something like this. “$ python read01.py” to run it. My modified read.py program is in this penzu: “mfrc programming log V0.7”: penzu.com/p/df27d289.
627117717.pr
627117717.pr
OK great stuff could i download and unzip the file to my pi? from github
tlfong01

tlfong01
2878
yst 21:49
Ah yes, you can download the zip file (Ref 27 of my answer) to either Windows PC or Rpi. I am a GUI guy and I can do it in GUI Win10 many times faster than in CLI Rpi buster, perhaps vice versa for you. I sometimes use Windows PureFTP or Samba to transfer files between PC and Rpi. But often I just use a USB finger as a courier. I also have a 500GB SSD in Rpi and also in Win10 to back up things, just in case I mess up things. Good luck.
627117717.pr
627117717.pr
yst 22:33
Thank you ill try !
tlfong01

tlfong01
2878
Ah bed time! I call it a day. Good luck and see you tomorrow.
And before I go –
user image
4 hours later…
627117717.pr
627117717.pr
2:14
nice one see you tommorow ill try to find a way to sent the code pictures!!!
12 hours later…
tlfong01

tlfong01
2878
14:20
@627117717.pr Looking forward to your pictures. Two more things. (1) You SPI wiring should not be longer than 1 meter, (2) The rpi.org “Official” loop back program warns that only CLK, MOSI and MISO wiring, but CE0/1 are NOT tested.
To test all 4 signals, you should do a “read after write” test to make sure what is written can be read back. My penzu file includes such a program. The test program I am writing tries to write something to the MRFC device and read back, to make “100%” that the MRFC chips is working OK.
user image
tlfong01
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.