Shop OBEX P1 Docs P2 Docs Learn Events
Propeller Object for NPX's MFRC522 MIFARE 13.56MHz RFID Readers — Parallax Forums

Propeller Object for NPX's MFRC522 MIFARE 13.56MHz RFID Readers

Duane DegnDuane Degn Posts: 10,588
edited 2020-08-24 08:19 in Propeller 1
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.
«1

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-10-23 20:55
    Reserved for software updates.


    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-20 21:27
    Reserved for links to projects using this object.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-20 21:28
    Reserved 4 of 4.
  • vanmunchvanmunch Posts: 568
    edited 2014-03-21 08:53
    Hey Duane,

    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
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-21 14:07
    Eureka! I found it!

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-21 16:08
    I cleaned up the code and separated out the RFID reading portion to a child object. The new version is attached to post #2.

    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.
    DAT'' Add serial numbers from previously read cards.
    
    
    serialNumber            long $739020BC
                            long $138DE88B
                            long END_OF_SERIAL_NUMBER_LIST
    
    
    '' The strings assigned under "correspondingText" need to be in the same order as
    '' the order of the serial numbers listed above.
    '' Make sure to terminate each string with a zero.
    '' Use the method "FindCardIndexNumber" to find the index number of
    '' a serial number read from a card. Use this index number with
    '' the method "FindStringOrderPtr" to access the strings below.
    '' See example in the method "MainLoop".
    
    
    correspondingText       byte "Pod Bay Access", 0
                            byte "Initiate Self Destruct", 0
                           
    notFoundText            byte "Unknown Card", 0
    

    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.
  • vanmunchvanmunch Posts: 568
    edited 2014-03-21 20:32
    Duane, this is excellent!!! I was away from my computer this evening and I just now ran the new demo code and it works like a charm every time! I'll add my keycard codes in tomorrow, right now... sleepy...

    I especially like the "Easter Egg" options in the code. :) That HAL is always getting me down...
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-22 17:20
    I was asked what the bug was in the initial program. I thought I'd answer the question here.

    The section of code that caused me trouble (or allowed myself to cause my own trouble) is from the function "Write_MFRC".
    //Waiting to receive data to complete	i = 2000;
        do 
        {
    
    
            n = Read_MFRC522(CommIrqReg);
            i--;
        }
        while ((i!=0) && !(n&0x01) && !(n&waitIRq));
    
    
    

    I initially translated this to:
    repeat readCounter from 0 to 1999 
    
        tempValue := ReadMfrc522(CommIrqReg)
     
        if tempValue & 1 == 0 or tempValue & waitInterrupt == 0
          quitFlag := 1
          quit
    

    The fix was to change the above "==" comparisons to "<>" comparisons.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-22 17:24
    I found, what I believe to be, a more complete example of using these sensors with an Arduino.

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-23 20:37
    I've had some success with using the Propeller to read information placed on the card by the Arduino.

    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.
  • FredBlaisFredBlais Posts: 370
    edited 2014-03-24 07:22
    I just ordered the MRFC522 RFID reader on ebay I will try your object as soon as I get it :)
    There is a ton of cool stuff to do with RFID!

    $_14.JPG
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-24 07:44
    Fred,

    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:
    User definable access conditions & password for each memory block
    1K byte EEPROM memory (768 FREE BYTES availability)
    Organized in 16 sector with 4 blocks of 16 bytes each (one block consists of 16 byte)
    Write endurance 100,000 cycle

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-09 21:32
    I found some information about how data is stored on the cards in this application note from NXP.

    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.
  • vanmunchvanmunch Posts: 568
    edited 2014-04-15 14:20
    Hey Duane,

    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
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-15 14:45
    vanmunch wrote: »
    Hey Duane,

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-15 20:24
    One thing that's not initially clear is only 75% of the memory of the 1K cards is available for data. One of the four blocks in each sector is used to hold the password data. Something to keep in mind if you want to use these cards for data storage.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-16 12:24
    I thought I was making progress on this today since I was able to use the Arduino to read a bunch of sectors (most of them with just zeros). I saw my name which I had previously written to the card and I also saw a portion of the alphabet I had managed to previously write to the card. This pretty exciting so I tried writing new data to the card to read back.

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-16 12:39
    Duane Degn wrote: »
    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.

    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.
  • vanmunchvanmunch Posts: 568
    edited 2014-04-20 18:46
    Awesome, very exciting. :-)
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2014-06-11 13:38
    @Duane,

    Is the March 21 version of your code the latest?

    Thanks,
    - Ron
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-11 14:35
    @Duane,

    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.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2014-06-11 18:31
    Duane Degn wrote: »
    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
    1. Does this table represent the correct mapping between the pins on the RFID PCB and the constant pin names in your demo code?
    2. Does SPI_CS (pin 6) in your program map to the "SDA" pin on the RFID board?
    3. I assume the IRQ pin is not used...
    updated




    RFID

    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#

    attachment.php?attachmentid=109086

    MFRC522 interface.jpg
    867 x 649 - 117K
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-11 19:58
    Does SPI_CS (pin 6) in your program map to the "SDA" pin on the RFID board?

    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.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2014-06-11 20:32
    Duane Degn wrote: »
    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!!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-11 22:00
    I just loaded the 110321e code and tried a new card I received today.

    This was the output (after "pressing any key" to start the program).
    Program Name = Mfrc522Demo140321e
    RFID Object Name = Mfrc522_140321e
    MFRC522 SPI Interface Demonstation Program
    
    
    Initializing RFID Reader
    
    
    
    
    
    
    Ready to read card.
    Card detected, type = $0400
    The card's number is  : $XXXXXXXX
    Card's CRC value = $53
    Calculated checksum = $53
    Card Data Successfully Read
    Checking to see if card can be identified.
    ****** Unknown card ******
    You might want to write this number down in order
    to add it to the program's choices.
    $XXXXXXXX
    
    
    
    
    Ready to read card.
    

    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.
    CON ' Pin Assignments
    
      SPI_CLK = 0    ' connects to SCK, pin #2 of the reader
      SPI_MOSI = 4    ' connects to MOSI, pin #3 of the reader
      SPI_MISO = 2    ' connects to MISO, pin #4 of the reader
      SPI_CS = 6    ' connects to SDA, pin #1 of the reader
      RESET = 8    ' connects to RST, pin #7 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.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2014-06-12 06:47
    Duane Degn wrote: »

    . . .

    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
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-12 07:09
    I'll review my hookup and try again.

    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.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2014-06-12 08:02
    Well no luck! Tried both modules on QuickStart and Propeller Activity Board.
    (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...

    attachment.php?attachmentid=109092



    RFID_1.jpg
    1024 x 768 - 137K
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2014-06-12 10:42
    Duane,
    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
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-12 13:58
    Duane,
    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.
Sign In or Register to comment.