Propeller Object for NPX's MFRC522 MIFARE 13.56MHz RFID Readers
Duane Degn
Posts: 10,588
A while ago Dave Dorhout (aka vanmunch) expressed interest the inexpensive MRFC522 RFID readers which are very common these days on ebay.
I was also curious about these and Dave persuaded me to write a Spin driver for this chip.
He found an Arduino SPI driver for these readers which I used as a template for my Spin version.
I wanted to include a link to this thread inside the object I just wrote, so once I make this post (and a few others) I'll add this thread's URL to the source code and attach it to post #2.
While the present code will read 13.56MHz cards and tags, I'm pretty sure I don't have something in the code quite right. When I read a card with the Propeller, it take several attempts before the card is noticed by the reader. When I use the Arduino with the sensor, the card shows up right away.
I'll take a look at the code again in the morning (my best time for finding bugs) to see if I can find what I'm doing wrong. If anyone else finds my bog before I do, I hope you let me know where it is.
I made some changes to the Arduino code which I think makes it more usable. I'll make sure and add the Arduino code below too.
I was also curious about these and Dave persuaded me to write a Spin driver for this chip.
He found an Arduino SPI driver for these readers which I used as a template for my Spin version.
I wanted to include a link to this thread inside the object I just wrote, so once I make this post (and a few others) I'll add this thread's URL to the source code and attach it to post #2.
While the present code will read 13.56MHz cards and tags, I'm pretty sure I don't have something in the code quite right. When I read a card with the Propeller, it take several attempts before the card is noticed by the reader. When I use the Arduino with the sensor, the card shows up right away.
I'll take a look at the code again in the morning (my best time for finding bugs) to see if I can find what I'm doing wrong. If anyone else finds my bog before I do, I hope you let me know where it is.
I made some changes to the Arduino code which I think makes it more usable. I'll make sure and add the Arduino code below too.
Comments
Initial Upload Mfrc522Spi140320m (Removed March 21, 2014. Program had a severe bug.)
Latest Upload Mfrc522Demo140321e.
ArduinoRfid.zip contains the Arduino version of the code.
Edit (23 October, 2015) : There's a version which will write data in a later post.
This is great! I was able to wire it up and it was able to read the two different cards that came with the reader. Like you mentioned, there're something that's requiring more time for the card to be read. It almost seamed like sometimes the program wouldn't load or it would load but it wouldn't detect the cards at all?
Regardless, way to go!
Dave
I found the bug. The code is working great. I just need to clean it up a little.
I'll upload the new improved version in a few minutes.
The child object needs to be regularly polled to see if a card has been read.
IMO, This Propeller version works every bit as well as the Arduino version.
If anyone has suggestions on improvements, I hope you let me know.
I added a few serial numbers from the cards I have to the DAT section of the code.
These numbers should be changed to match numbers of your cards or tags.
You'll also want to change the strings stored in the "correspondingText" array to text you wish to have displayed when certain cards are read.
Do these cards store data? It looks like there are functions in the Arduino code for accessing stored data but there wasn't any examples of this code being used.
I translated these additional functions to Spin methods but I haven't tried them yet.
Do any of you have suggestions on what sort of data can be stored?
I'll probably give this a try in in the near future. If any of you beat me to it, I hope you let me know what you find out.
I especially like the "Easter Egg" options in the code. That HAL is always getting me down...
The section of code that caused me trouble (or allowed myself to cause my own trouble) is from the function "Write_MFRC".
I initially translated this to:
The fix was to change the above "==" comparisons to "<>" comparisons.
The code I initially used was a trimmed down version of the code available from this page. (The code is labeled "Paintcode".)
It looks like there are more examples of using the various features of the cards and reads.
I think I've successfully read the size of the data section on a card. I'm hoping to figure out how to store data to the cards.
I'm still very unclear about how the card uses access codes and what "block addresses" refer to but I used the code I linked to in post #10 to write my name as data to the card. I can read this data back with the Propeller but only if I'm also using the same "sector key" as the one I used when the data was written to the card.
I still need to figure out how to write data to the card with the Propeller and what these "sector keys" do.
Does anyone have experience with these tags who can offer some hints as to how the data is organized on the cards? Does each sector have its own password?
I think I need to find a datasheet for the cards and tags. I couldn't figure out how data was stored from looking through the reader's datasheet.
There is a ton of cool stuff to do with RFID!
Hopefully I will have figured out how to use the Propeller to write to the cards by then.
I have successful read data from a card with the Propeller.
To answer some of my question in post #11, I found this information:
I think each sector has a six byte key in order to access the sector.
I've got other stuff I need to work on today, but I'm very confident I can (soon) get these readers to store and retrieve data using the Propeller as the controller.
As I mentioned earlier, I've been able to write data to a card with the Arduino and read from the card with the Prop. It's just a matter of time before I get a handle on how to do this for all the sectors of the card.
Once I get my taxes done, I'll get back to this project.
If anyone is experimenting with the code I posted earlier, make sure and clear any local variables in the Propeller version of the code. The Arduino code uses a pointer to set a single byte to a certain value. The Propeller code also sets a singe byte to the desired value but since the byte is part of a local long, the comparisons don't work as expected unless the local long is cleared first. I'll make sure these issues are fixed in the next version of the code I post.
I purchased one of the RFID tag reader/writers (link below) and got it to read and write data to the cards. In the data "blob" from the ebay page there is also some more information on the actual cards that I think might have what you're looking for (pdf posted below). Hope your taxes went well, just sent ours out.
Reader/writer:
http://www.ebay.com/itm/301148377615?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649
Mifare card manual in English!
Mifare_S50_en.pdf
Thanks for the links Dave.
I signed my taxes over at my accountant's office a couple of hours ago. I've been working on this project today. I appear to be caught up on my other paid work.
I like the pdf file you posted. It looks like it's easier to follow than the document I linked to earlier.
I'm pretty confident I can get these readers to read and write data using the Propeller but this project is at the "edge of the envelope" of my programming skills. Of course it's the "edge of the envelope" projects which are the most fun.
The card didn't like the way I wrote the data because after writing to the card I could read any of the previous info. All read attempts returned an error.
I think I don't understand what key A is to be used and when key B is to be used among many other things.
I'll need to return to an earlier version of the Arduino code to see how I had successfully written my name and parts of the alphabet to the card.
I stated typing a long question about part of the Arduino code I didn't understand. The process of typing the question helped me realize the answer.
I was also going to say I was done working on this for today but now I'll get back to work. Thanks for the answering my question and thanks for the encouragement forum.
I probably ought to purchase some more cards. I have a feeling I might have locked myself out one of them and I might end up "bricking" more cards before I get writing to the cards figured out.
Wow, you guys were right! Those extra bytes in the new key array were important.
Thanks again for the help.
I think I can safely say, I can now read and write multiple sector/blocks using the Arduino. I don't think it will be hard to translate the C into Spin.
I still don't understand all the subtleties of how to encrypt the data but I'm pretty sure I'll be able to store and retrieve data using the Propeller very soon.
Is the March 21 version of your code the latest?
Thanks,
- Ron
I doubt it. I know I have code that will read data to the cards but my initial attempts to write data to the cards with the Propeller didn't work. I got the Arduino code to write to any of the locations on the card but I have a bug in the Propeller version.
I'll check to see what my latest code looks like and try to find a better version to post.
The March 21 code will let you read the card's serial number.
Thanks Duane!
I am a bit confused on the pin wiring
- Does this table represent the correct mapping between the pins on the RFID PCB and the constant pin names in your demo code?
- Does SPI_CS (pin 6) in your program map to the "SDA" pin on the RFID board?
- I assume the IRQ pin is not used...
updatedRFID
Prop code
pin#
SDA
SPI_CS
6
SCK
SPI_CLK
0
MOSI
SPI_MOSI
4
MISO
SPI_MISO
2
IRQ
GND
RST
RESET
8
3.3V
I looks like you can use SPI, I2C, or serial UART with this chip and it automatically determines which to use (see below)
http://www.datasheetarchive.com/MFRC522-datasheet.html#
Yes, this is correct. You got the rest of pins correct too.
I used SPI to communicate with the chip since that's what the Arduino code I found uses.
I opened up my latest version of the code and I was reminded I had experimented with the IRQ pin. I don't remember if I got any useful information from the pin or not (I like to blame my forgetfulness on my many simultaneous projects rather than admitting to getting older).
I plan to review my latest version of code, this evening, to see if I have anything useful to add to what I've already posted.
I still intend to get the Propeller to write to the cards' EEPROM. I've had success changing the Arduino code to write to any of the sectors and blocks. I think I understand the way the cards work well enough to make this work with the Propeller.
Can't seem to get it working. Using a QuickStart and checked all the connections.
Are any of the pins supposed to pulled high (or low) with resistors?
Should IRQ be connected at all?
Thanks! I know you have a lot of projects going on!!
This was the output (after "pressing any key" to start the program).
I blocked out my card's serial number but the rest is what the program generated.
The reader reacts immediately when I bring the card near the top or the bottom of the reader.
I added some comments to the pin assignments to hopefully make it clear what connects where. Make sure and notice I don't use the pins sequentially (P4 comes before P2).
I left pin #5, IRQ unconnected. Pin #6, GND is connected to the QuickStart's Vss and pin #8 is connected to the QuickStart's 3.3V pin (position 38 on 40-pin header or one of the Vdd headers).
The board I'm using has a red LED labelled "D1" which is continually on while the board is powered. There isn't any indication on the board when a card is being read. I half expected to see the LED blink but it doesn't.
Do you just have the one board? Whenever I purchase cheap electronics off ebay I generally get at least two of the device so I can be sure (sort of anyway) the I'll have at least one working device.
If you're in a hurry to try this, I'll send you one of my boards. Just PM me your address and I'll drop a known working board in the mail to you.
Thanks for taking the time to provide more info. I'll review my hookup and try again.
I did buy two modules so I can also try the second one.
It's no big deal if they don't work - I just got them to play around with. I have several of the Parallax RFID readers too.
- Ron
I noticed as I checked my connections that there wasn't any apparent logical order to the way I used the I/O pins. I'm pretty sure the pin constant names were copied from another object where their order made more sense.
I'm going to monitor the current the reader pulls to see if it could be powered by a Propeller I/O pin. This would allow the board to be plugged into the QuickStart's header directly.
This reminds me. You might want to double check the I/O pins on your QuickStart are all working correctly. I had a heck of a time figuring out what was wrong with my "Mood Enhancing LEDs" project when one of the I/O pins on my QuickStart wasn't working.
(Also tried with module on separate breadboard and longer wires)
SDA - white - prop pin 6
SCK - brown - prop pin 0
MOSI - gray - prop pin 4
MISO - blue - prop pin 2
RST - green - prop pin 8
GND - brown (looks reddish under LED) - GND
IRQ (not used)
3.3V - red - 3.3V
Conclusion - must have bad modules...
I also tried the Arduino sketch from post#2 and it doesn't work either.
Who did you buy your modules from? I got mine here: http://www.ebay.com/itm/400426561641?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
I purchased a couple from this seller since they were in the US and I was hoping to get the readers fast.
I received a reader from Electrodragon yesterday. If you're interested in purchasing a reader from Electrodragon, let me know and I'll test mine.
Sorry to ask this, I'm betting you already know, but just to be sure, you're using the 13.56MHz Cards right? The Parallax cards won't work with these readers.
I haven't used that ebay seller you linked to but the page looks fine. I wouldn't have thought you'd end up with two bad readers from them.
I know with cheap ultrasounds, about 20% are duds. About 10% of the Nordic modules I get are also duds. My guess is you just had bad luck and got two duds.