rfid rc522 arduino uno
时间: 2025-05-05 22:56:26 浏览: 22
### 如何使用 RFID RC522 模块与 Arduino Uno 进行集成
#### 材料准备
为了实现这一项目,需要准备好以下材料:
- Arduino Uno × 1
- MFRC522 NFC/RFID Shield 或者独立的MFRC522模块 × 1
- 面包板以及连接线若干
- 几张兼容的NFC标签或卡片用于测试
#### 接线方式
对于不同的硬件版本接法可能略有差异,在这里提供一种常见的连线方案[^1]。
| **Arduino Pin** | **MFRC522 Pin** |
|--|
| SDA | SDI |
| SCK | SCK |
| MOSI | MOSI |
| MISO | MISO |
| RST | NRSTPD |
| GND | GND |
| D10 (SS) | SS |
注意:如果采用的是带有SPI接口的扩展板,则通常只需要按照默认设置插好即可自动完成大部分配对接口的工作;而如果是裸版则需手动按上述表格一一对应焊接或者通过杜邦线相连。
#### 安装库文件
要让Arduino能够识别并操作该设备还需要安装相应的驱动程序——`MFRC522 Library`。可以通过Arduino IDE中的管理工具来轻松获取这个库。打开软件后依次点击【Sketch】> 【Include Library】> 【Manage Libraries...】,接着在弹出窗口内搜索“mfrc522”,找到由Mario Banovic开发维护的那个条目并下载安装最新稳定版。
#### 编写代码实例
下面给出一段简单的读取卡号的例子供参考学习之用:
```cpp
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // If using a board with a hard-wired reset pin, set this to that pin number.
#define SS_PIN 10 // SPI Slave Select signal is connected on digital I/O 10.
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent()) { // Look for new cards
return;
}
if (!mfrc522.PICC_ReadCardSerial()) { // Read UID of the Card
return;
}
// Print out UID as hex-values separated by spaces
Serial.print(F("UID Tag :"));
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
delay(2000);
}
```
这段代码实现了基本的功能,即当检测到有新的卡片靠近时会打印其唯一的序列号至串行监视器上显示出来以便观察验证效果。
阅读全文
相关推荐


















