B4R Library MFRC522 - RFID reader / writer

SS-2016-05-22_16.19.26.jpg


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:
B4X:
'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(10, 9, "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:
B4X:
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(18) As 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).
 

Attachments

  • rMFRC522.zip
    80.2 KB · Views: 1,046
Last edited:

henry montoya

Member
Licensed User
Longtime User
Hello.
Someone knows if it is possible to extend the reading range of this module with some antenna and how to do it.
Many thanks for all your contributions.
 

amarnath

Member
Licensed User
Longtime User
hi there i am developing an app using b4a and i want to connect rfid em-18 with my android app
right now i am using
1 atmel at89c2051-24pu
2 uart wifi module
3 b4a

task is my android app should be able to read all the data from rfid reader and my android app should be able to write all the data to rfid module

please send me rfid library or any other details to do so

thanks in advance
waiting for answer.
 

Beja

Expert
Licensed User
Longtime User
Thanks Erel..

Any visual or more clear Schematics for this example?
Something like this .
Thanks
 

falbertini

Member
Licensed User
Longtime User
Hi Erel, I tried with the Arduino IDE 1.8.5 with ESP32 and MFRC522 using, for example, an old version of the library selectable from IDE (es. 1.3.6) and i get the error "WARNING: Communication failure, is the MFRC522 properly connected" while with the latest version of the library MFRC522 1.4.3 i don't have the error. Using the rMFRC522.zip library in B4R with ESP32 i get the same error as if the library were not aligned with the latest version available. How can I have library B4R rMFRC522.zip updated to the latest version of the Arduino library MFRC522 1.4.3?

B4X:
'depends on rRandomAccessFile and rMFRC522 libraries

' Pin schema :

'ESP32   MFRC522
' 18       SCK
' 19       MISO
' 21       SDA
' 22       RST
' 23       MOSI
' GND      GND
' 3v3      3v3

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(21, 22, "rfid_CardPresent")
    rfid.LogVersion
End Sub

Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
    Log("UID: ", bc.HexFromBytes(UID))
End Sub

Thanks
 

Beja

Expert
Licensed User
Longtime User
B4X:
Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
   Log("UID: ", bc.HexFromBytes(UID))
End Sub

Hi Erel
Pardon my ignorance.. Where's the UID value logged? Does the arduino have to stay connected to the computer and the value is displayed on the PC screen?
Thanks.
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

please for help,
I am using PIN connection for Wemos D1 mini
I have board: Wemos D1 R1


I am using this code:

B4X:
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(10, 9, "rfid_CardPresent")
   rfid.LogVersion
End Sub

Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
   Log("UID: ", bc.HexFromBytes(UID))
End Sub

I get in LOGS this result:
Firmware Version: 0xEE = (unknown)

What I am doing wrong please?
Thank you
p4ppc

I have board settings:
boardselector.jpg
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
Thank you for your answer,

after uploading program to ESP8266 (Wemos D1R1 board) module via USB I see in LOGs (B4R) that program is STARTED and I see this text:
Firmware Version: 0xEE = (unknown)

I have checked wires and everything is OK. I have tried run it with connected and diconnected RESET pin, but nothing happen.
I have tried NODE V3 board, there is the same situation.

I am testing boards only in B4R not in Arduino.
Tommorow I go to buy second RFID board , because I want have certain that rfid board is OK.
In actual RFID board is led fleshing, everything looks OK.

Thank you very much,
p4ppc
 
Top