2023年9月13日 星期三

An Introduction to RFID

 An Introduction to RFID

源自於 https://medium.com/autonomous-robotics/an-introduction-to-rfid-dc6228767691

The RC522 RFID module based on MFRC522 IC from NXP is one of the most inexpensive RFID options. It usually comes with an RFID card tag and key fob tag having 1KB memory. And best of all, it can write a tag.

Working

The RC522 RFID Reader module is designed to create a 13.56MHz electromagnetic field that it uses to communicate with the RFID tags (ISO 14443A standard tags). The reader can communicate with a microcontroller over a 4-pin Serial Peripheral Interface (SPI) with a maximum data rate of 10Mbps. It also supports communication over I2C and UART protocols.

The module comes with an interrupt pin. It is handy because instead of constantly asking the RFID module “is there a card in view yet? “, the module will alert us when a tag comes into its vicinity.

The operating voltage of the module is from 2.5 to 3.3V, but the logic pins are 5-volt tolerant, so it can be easily connected to an Arduino or any 5V logic microcontroller without using any logic level converter.

RC522 RFID Module Pinout

The RC522 module has a total of 8 pins that interface it to the outside world. The connections are as follows:

VCC supplies power for the module. This can be anywhere from 2.5 to 3.3 volts. It can be connected to 3.3V output from the Arduino.

RST is an input for Reset and power-down. When this pin goes low, hard power-down is enabled. This turns off all internal current sinks including the oscillator and the input pins are disconnected from the outside world. On the rising edge, the module is reset.

GND is the Ground Pin and needs to be connected to the GND pin on the Arduino.

IRQ is an interrupt pin that can alert the microcontroller when an RFID tag comes into its vicinity.

MISO / SCL / Tx pin acts as Master-In-Slave-Out when SPI interface is enabled, acts as serial clock when I2C interface is enabled and acts as serial data output when UART interface is enabled.

MOSI (Master Out Slave In) is SPI input to the RC522 module.

SCK (Serial Clock) accepts clock pulses provided by the SPI bus Master i.e. Arduino.

SS / SDA / Rx pin acts as Signal input when SPI interface is enabled, acts as serial data when I2C interface is enabled and acts as serial data input when UART interface is enabled. This pin is usually marked by encasing the pin in a square so it can be used as a reference for identifying the other pins.

Wiring

To start hooking up the Arudino, connect the VCC pin on the module to 3.3V on the Arduino and GND pin to ground. The pin RST can be connected to any digital pin on the Arduino. In this case, it’s connected to digital pin#5.

Each Arduino Board has different SPI pins which should be connected accordingly. Each Arduino Board has different SPI pins which should be connected accordingly.

Wiring RC522 RFID Reader Writer Module with Arduino UNO

Arduino Code — Reading

There’s a library called MFRC522 library which simplifies reading from and writing to RFID tags. Once the library is installed, open Examples submenu and select MFRC522 > DumpInfo example sketch.

This sketch will not write any data to the tag. It just tells us if it managed to read the tag, and displays some information about it.It should be made sure that the RST_PIN is correctly initialized.

Now the sketch is uploaded and the Serial Monitor is opened. On bringing the tag closer to the module, this is seen on the monitor.

It displays all the useful information about the tag including tag’s Unique ID (UID), the memory size and the whole 1K memory.

MIFARE Classic 1K Memory Layout

The 1K memory of the Tag is organized in 16 sectors (from 0 to 15) Each sector is further divided into 4 blocks (block 0 to 3). Each block can store 16 bytes of data (from 0 to 15).

16 sectors x 4 blocks x 16 bytes of data = 1024 bytes = 1K memory

The whole 1K memory with sectors, blocks and data is highlighted below.

3D Representation of MIFARE Classic 1K Memory Map Layout

The Block 3 of each sector is called Sector Trailer and contains information called Access Bits to grant read and write access to remaining blocks in a sector. That means only the bottom 3 blocks (block 0, 1 & 2) of each sector are actually available for data storage. Also The Block 0 of sector 0 is known as Manufacturer Block/Manufacturer Data contains the IC manufacturer data, and the Unique IDentifier (UID). The Manufacturer Block is highlighted in red below.

Arduino Code -Writing

This sketch will do a basic demonstration of writing custom data to RFID tag.

The sketch starts with including the MFRC522 and SPI library, defining Arduino pins to which RC522 is connected and instantiating MFRC522 reader objects.Next, a block in which we are going to store the data has to be defined. Here sector 0 block 2 is selected, since writing into ‘sector trailer’ block can make the block unusable. Next, an array of 16 bytes named blockcontent[16] is defined which holds the message to write into the block. Next, an array of 18 bytes named readbackblock[18] has to be defined. This can be used to read the written contents back. The MIFARE_Read method in MFRC522 library requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.

In loop function: first scanning is done to see if there is a card in view, if yes, that card is selected for writing and reading purposes.

Checking the success of the operation can be done using custom function called readBlock() which again takes two parameters — one is block number and other is array to store block contents. Finally, the contents of readbackblock array using a for loop are printed and displayed on the serial monitor.

The output on the serial monitor will look like this.

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...