B4R Library MFRC522 - RFID reader / writer

Discussion in 'B4R Libraries' started by Erel, May 22, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    This library is based on this open source project: https://github.com/miguelbalboa/rfid

    It allows reading and writing to RFID cards using RC522 modules. The RFID modules and the cards (or stickers) are inexpensive.

    The pins layout is described here: https://github.com/miguelbalboa/rfid#pin-layout
    Make sure to connect the module 3.3v pin to the Arduino 3V3 pin.

    Each card has a unique UID.

    Complete program that reads UIDs:
    Code:
    'depends on rRandomAccessFile and rMFRC522 libraries
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private rfid As MFRC522
       
    Private bc As ByteConverter
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       rfid.Initialize(
    109"rfid_CardPresent")
       rfid.LogVersion
    End Sub

    Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
       
    Log("UID: ", bc.HexFromBytes(UID))
    End Sub
    The library also supports reading and writing to Mifare cards. This requires some knowledge of the card structure. Overall it is quite simple.

    The card is organized into sectors. Each sector includes 4 blocks and each block is made of 16 bytes (the Mifare 4k also has larger sectors).
    The last block in each sector is a special block that holds authentication data about the sector. This means that you shouldn't write to block numbers 3, 7, 11, etc. Block 0 is also readonly as it stores the UID.

    You need to authenticate before you can read or write. This is done with
    MifareAuthenticate or MifareAuthenticate2 methods.

    Example that writes and reads from block #1:
    Code:
    Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
       
    Log("UID: ", bc.HexFromBytes(UID))
       
    Log("Type: ", CardType, ", Is it Mifare: ", rfid.IsMifare)
       
    If rfid.IsMifare Then
         
    If rfid.MifareAuthenticate(1) = False Then
           
    Log("Failed to authenticate")
           
    Return
         
    End If
         
    Dim buffer(18As Byte
         
    For i = 0 To buffer.Length - 1
           buffer(i) = i
         
    Next
         
    'write 16 bytes to block number 1
         Log("Write: ", rfid.MifareWrite(1, buffer))
         
    If rfid.MifareRead(1, buffer) > 0 Then 'buffer size must be 18 bytes or more
           Log(bc.HexFromBytes(buffer))
         
    Else
           
    Log("Failed to read")
         
    End If
       
    End If
    End Sub
    Updates

    V1.02 - Based on the latest version of the open source project (March 2019).
     

    Attached Files:

    Last edited: Mar 15, 2019
  2. cloner7801

    cloner7801 Active Member Licensed User

    I get this error


    AppStart
    Firmware Version: 0x0 = (unknown)
    WARNING: Communication failure, is the MFRC522 properly connected?

    every pins are connected!
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Are you using the exact chip as in the image above?
     
  4. cloner7801

    cloner7801 Active Member Licensed User

    Yes !
    RC522
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    1. Are you using an Arduino Uno?
    2. Disconnect all wires and connect them again.
    3. Try to run one of the C examples from the open source project with the Arduino IDE.
     
  6. cloner7801

    cloner7801 Active Member Licensed User

    1. Yes
    2. I tried but..
    3. Can you attach ?

    ---

    i restart b4r and worked !
     
    Last edited: Jun 1, 2016
  7. Hans- Joachim Krahe

    Hans- Joachim Krahe Active Member Licensed User

    hi, is there a different library for Mega2560? It seems to use different Pins...

    upload_2016-7-30_13-58-8.png
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Peter Simpson likes this.
  9. Hans- Joachim Krahe

    Hans- Joachim Krahe Active Member Licensed User

    ...great stuff. Thank you so much:)
     
  10. Hans- Joachim Krahe

    Hans- Joachim Krahe Active Member Licensed User

    ups,

    53 51 50 52 and reset 5 for mega dont work
    (WARNING: Communication failure, is the MFRC522 properly connected?)

    10 11 12 13 and reset 9 for uno works fine

    ' rfid.Initialize( 10, 9, "rfid_CardPresent") ' UNO
    rfid.Initialize( 53, 5, "rfid_CardPresent") ' MEGA
    rfid.LogVersion



    did anyone try successful with mega?

    thanks
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    I tested it successfully with Mega. Connected it exactly as described here: https://github.com/miguelbalboa/rfid#pin-layout

    The code:
    Code:
    rfid.Initialize(535"rfid_CardPresent")
    Output:
    Code:
    AppStart
    Firmware Version: 
    0x92 = v2.0
    UID: D5A4AC56
    UID: D5A4AC56
    UID: D5A4AC56
    UID: D5A4AC56
     
    Peter Simpson likes this.
  12. Hans- Joachim Krahe

    Hans- Joachim Krahe Active Member Licensed User

    thank you, Erel, I found the bug. Now, it works, but only with disconnected Pin 5 (rst). With connected Rst, it works exacly one time after reset arduino. Mysterious, but without rst it seems to be o.k.
     
  13. Cableguy

    Cableguy Expert Licensed User

    Maybe the pin needs to be defined as pull-up(?)
     
  14. Hans- Joachim Krahe

    Hans- Joachim Krahe Active Member Licensed User

    perfect - my error. Pin was defined OUTPUT by another toy. Time for cleaning up - thanks:)
     
  15. coslad

    coslad Well-Known Member Licensed User

    trying sample code comes out this error :


    In file included from AsyncStreams.cpp:1:0:
    B4RDefines.h:21:17: fatal error: SPI.h: No such file or directory
    #include <SPI.h>
    ^
    compilation terminated.
    Errore durante la compilazione


    i solved loading the source code into the arduino ide and adding :
    #include <SPI.h>

    in the scr.ino file

    but this is a workaround and not "the solution" , where B4R compiler miss or where i miss ?

    Thanks !

    in attach the project
     

    Attached Files:

  16. Erel

    Erel Administrator Staff Member Licensed User

    It compiles fine here. Based on the error message it already added the #include <SPI.h> statement (you can see it in B4RDefines.h). It wasn't able to find the SPI library for some reason.

    Are you using Arduino IDE 1.6.9?
     
  17. coslad

    coslad Well-Known Member Licensed User

    No , 1.6.5 I will try the 1.6.9 , I saw that SPI.H is called in the B4RDefines.h but for some strange reasons it Doesn't find it .
     
  18. coslad

    coslad Well-Known Member Licensed User

    1.6.9. had solved the problem . Thanks
     
  19. Hans- Joachim Krahe

    Hans- Joachim Krahe Active Member Licensed User

    does RFID match with NANO too?
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    Peter Simpson likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice