RFID on the arduino with the MF522-AN module

This module is very easy to setup and use. Here is how I wired it for the arduino uno.

Reset > Pin 5
SS > Pin 10
MOSI > Pin 11
MISO > Pin 12
SCK > Pin 13
Ground > Ground
3.3v > 3.3v

I used the following code example. There is also similar code at github however the comments are all in spanish. Both sets of code are basically the same code and both compiled and worked on ardiono 1.0.1. With the first code listed you do have to uncomment lines 186-196 to get it to read the card. There is a lot of code there to play with and develope a basic understanding of how to work with this module. Once you have the code compiled and uploaded just open up the serial monitor and put a card in front of the reader. You should see the data pop up in the serial monitor.

It’s probably worth noting that this code uses SPI which is Synchronous – That means that the clock signal accompanies the data signal. MOSI – Master Out Slave In; MISO – Master In Slave Out; SCK – Clock signal from master to slave; SS – Slave Select signal selects salve devices. SPI is a bus and can have multiple devices on the bus. Because the clock signal is present, SPI can be operated faster than UART.

 

SPI wiring

 

RFID on the arduino uno usning the MF522-AN module

RFID on the arduino uno usning the MF522-AN module

 

Here is a link to my pde file. I used to have it as text in the blog post but I recently figured out that wordpress didn’t handle it well, sorry for any issues that caused.

Edited to add – I used the same code with an arduio micro and the MF522-AN

 

There are a couple more things I would like to take the time to point out. This post has been linked to by several sites selling this product and that is fine. And while I have written a couple articles about the MF522-AN module, I have not been compensated in any way nor have I received anything directly or indirectly from any one or any business. These are strictly my opinions based solely on my experience. If you find value in them great and if not that’s fine too. If you are like me you tend to read up on a product as much as possible before making that final decision to purchase a product. The sample code is available from various sources all linked within this article and my version of it is here.

Tagged on: ,

25 thoughts on “RFID on the arduino with the MF522-AN module

  1. Pingback: Arduino microcontrollers | One Mans Anthology

  2. John McLear

    Example code fires an error

    Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 0
    at processing.app.Sketch.setCurrentCode(Sketch.java:1182)
    at processing.app.Sketch.load(Sketch.java:220)
    at processing.app.Sketch.(Sketch.java:142)
    at processing.app.Editor.handleOpenInternal(Editor.java:2191)
    at processing.app.Editor.(Editor.java:304)
    at processing.app.Base.handleOpen(Base.java:705)
    at processing.app.Base.handleOpen(Base.java:670)
    at processing.app.Base.handleOpenPrompt(Base.java:659)
    at processing.app.Editor$4.actionPerformed(Editor.java:497)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266)
    at java.awt.Component.processMouseEvent(Component.java:6263)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6028)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

  3. kumar

    Using this as Libraary I can Read such that
    Card found
    Cardnumber:
    Dec: 149, 213, 192, 180, 52
    Hex: 95, D5, C0, B4, 34
    …….
    but i want to replace 149 with some other number example 149 as 100, so how to use this function for writing???????

      1. hytekblue Post author

        David,

        The Function:MFRC522_Write will allow basic writing of data to the tag in theory but you have to have a tag that has user writable memory. A lot of these cheap to RFID access cards don’t have user writable memory blocks at all.
        Cheers!
        Hytekblue

    1. hytekblue Post author

      The general answer is that the serial number on an RFID tag is write once and read many. So it’s not alterable. Some RFID tags have user programable memory blocks. If you have tag that can hold user data then you simply write to that block and then read that block back instead of reading the serial number. make sense?

      Cheers,

      Hytekblue

  4. TK

    Have you used successfully the function to write a block to a tag? It doesn’t seem to work.
    I’m referring to
    uchar MFRC522_Write(uchar blockAddr, uchar *writeData)

  5. Noriel Domondon

    Hi,

    I have these devices at hand, but i want it to interfaced in Microsoft C# .Net if possible. Please send me a copy of C# sample code if you have. Thanks very much in advance.

    Noriel Domondon

    1. hytekblue Post author

      Can you be a bit more specific about what does not work? Do you get any errors when you compile? What do you get in the terminal monitor window when you put a card in the field?

  6. Rocky

    Hi sir,
    I am making a Electronic toll gate project using ARDUINO UNO with MFRC522.
    Can you help me out with deduction part. For eg – if initially user have 1000 in his RFID tag then after crossing toll plaza his new balance should be 900. And when user recharges his RFID tag then new balance should be = Existing Balance + New Recharge Balance.
    please help me out..
    thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *