RC522 Radio Frequency Identification (RFID) IC Card Induction Module
Near Field Communication (NFC) is a short-range, high-frequency wireless communication technology that allows electronic devices to engage in non-contact point-to-point data transfer (within 10 cm). This technology evolved from contactless Radio Frequency Identification (RFID) and is backward compatible with RFID. It was first successfully developed by SONY and PHILIPS. NFC is mainly used in mobile phones and other handheld devices to enable M2M (Machine to Machine) communication. Due to its inherent security, NFC technology is considered to have significant potential in applications like mobile payments. Moreover, NFC is regarded as having better security compared to other wireless communication technologies and is often referred to by the China Internet of Things (IoT) Industry-University Alliance as a "secure dialogue" between machines.
Module Source
Purchase Link:
https://item.taobao.com/item.htm?spm=a21n57.1.0.0.33fb523cor6YaQ&id=19376862391&ns=1&abbucket=0
Baidu Netdisk Download Link:
https://pan.baidu.com/s/1pGSaohXnOi8tu6M3i3KFcQ
Password: suah
Specifications
Operating Voltage:3.3V
Operating Current:10-26mA
Module Dimension: 40mm×60mm
Supported Card Types: mifare1 S50、mifare1 S70、mifare UltraLight、mifare Pro、mifare Desfire
Control Method: SPI
Hardware Connection
RC522 Module Development Board
SDA Pin 10
SCK Pin 13
MOSI Pin 11
MISO Pin 12
IRQ Not connected (optional)
GND GND
RST Pin 9
3.3V 3.3V
2
3
4
5
6
7
8
9
Usage Method
Install the Library
- In your Arduino IDE, you need to install the MFRC522 library. You can find and install the MFRC522 library by going to the "Tools > Manage Libraries..." menu.
Enter the code:
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 11, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // 配置RST引脚
#define SS_PIN 10 // 配置SS引脚
MFRC522 mfrc522(SS_PIN, RST_PIN); // 创建MFRC522类的实例
// 标签的默认密码为FF FF FF FF FF FF
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // 初始化串口通信
SPI.begin(); // 初始化SPI总线
mfrc522.PCD_Init(); // 初始化MFRC522卡
// 初始化MIFARE标签的密码
for (byte i = 0; i < 6; i++) {
key.keyByte[] = 0xFF;
}
Serial.println("Ready to read/write card");
}
void loop() {
// 寻找新的卡片
if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
delay(50);
return;
}
// 显示卡片的UID
Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[], HEX);
}
Serial.println();
// 写入数据到标签的一个扇区块
byte blockAddr = 4; // 块地址可以在1到15之间选择,但是不要写入制造块(块 0)
byte writeData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; // 要写入的数据
writeBlock(blockAddr, writeData);
// 读取相同扇区块的数据
byte buffer[]; // 数据读取缓冲区
readBlock(blockAddr, buffer);
printBlock(buffer);
// 结束卡片通信
mfrc522.PICC_HaltA();
delay(1000);
}
void readBlock(byte blockAddr, byte *buffer) {
byte size = sizeof(buffer);
MFRC522::StatusCode status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
}
}
void writeBlock(byte blockAddr, byte *buffer) {
MFRC522::StatusCode status = mfrc522.MIFARE_Write(blockAddr, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("Writing failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
} else {
Serial.println("Data written to block successfully");
}
}
void printBlock(byte *buffer) {
Serial.print("Read block: ");
for (byte i = 0; i < 16; i++) {
Serial.print(buffer[] < 0x10 ? " 0" : " ");
Serial.print(buffer[], HEX);
}
Serial.println();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Note! Remember, before writing to an RFID tag, you must first perform authentication (using MIFARE_Key
). In the code, the default key FF FF FF FF FF FF
is defined for operation. However, in real applications, you need to ensure the use of the correct key and avoid overwriting important data blocks. Additionally, note that different types of tags and different sections of the tag may require different authentication methods.