arduino rc522

文章介绍了如何使用MFRC522库在Arduino中读取RFID卡片的UID,以及通过比较特定卡号来控制LED灯的开关状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取卡号

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9     // Reset pin of the module
#define SS_PIN          10    // Slave Select pin of the module

MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance

void setup() {
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522
}

void loop() {
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) { // Check if new card is present and read its UID
    // Read the UID of the card
    uint8_t uidSize = rfid.uid.size;
    Serial.print("Card UID:");
    for (uint8_t i = 0; i < uidSize; i++) {
      Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
      Serial.print(rfid.uid.uidByte[i], HEX);
    }
    Serial.println();
    
    // Optionally, you could add this UID to an authorized list or perform other actions here.
    
    rfid.PICC_HaltA(); // Stop reading the card
    rfid.PCD_StopCrypto1(); // Disable encryption on PCD
  }
  delay(100); // Short delay before re-checking for a card
}

判断是不是指定卡号

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9     // Reset pin of the module
#define SS_PIN          10    // Slave Select pin of the module
#define LED_PIN         A0   // LED connected to digital pin 13

MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance
boolean ledStatus = false; // LED status variable

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  pinMode(LED_PIN, OUTPUT); // Configure LED pin as output
}

void loop() {
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    // Read the UID of the card
    uint8_t uidSize = rfid.uid.size;
    String cardUID = "";
    for (uint8_t i = 0; i < uidSize; i++) {
      cardUID.concat(String(rfid.uid.uidByte[i], HEX));
    }

    if (cardUID.equalsIgnoreCase("90A94926")) { // Compare the read UID with the target UID
      ledStatus = true;
      digitalWrite(LED_PIN, HIGH); // Turn on the LED
      Serial.println("Card UID: " + cardUID + ". LED turned ON.");
    } else {
      ledStatus = false;
      digitalWrite(LED_PIN, LOW); // Turn off the LED just in case it was previously on
    }

    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
  }

  // Keep the LED state even after the card is removed
  digitalWrite(LED_PIN, ledStatus ? HIGH : LOW);

  delay(100); // Short delay before re-checking for a card
}
### ArduinoRC522模块使用教程 #### 线方法 对于Arduino与MFRC522 RFID模块之间的连,通常采用SPI通信协议。具体的线方式如下表所示: | MFRC522 Pin | Arduino Uno Pin | |-------------|-----------------| | SDA | 10 (SS) | | SCK | 13 | | MOSI | 11 | | MISO | 12 | | IRQ | 不连 | | GND | GND | | RST | 9 | | VCC | 3.3V | 确保电源电压为3.3伏特而不是5伏特,因为过高的电压可能会损坏MFRC522芯片。 #### 示例代码 为了方便开发者快速上手,官方提供了多个示例程序用于展示如何操作该设备。这些例子可以在`文件 -> 示例 -> MFRC522`菜单下找到[^2]。下面是一个简单的读取卡片唯一标识符(UID)的例子: ```cpp #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522(SS_PIN, RST_PIN); // 创建MFRC522实例 void setup() { Serial.begin(9600); SPI.begin(); // 初始化SPI总线 mfrc522.PCD_Init(); // 初始化RC522阅读器 } void loop() { // 查看是否有新的标签进入射频场 if (!mfrc522.PICC_IsNewCardPresent()) { return; } // 选中一张卡并获取其序列号 if (!mfrc522.PICC_ReadCardSerial()) { return; } // 打印出所选中的卡片的UID Serial.print(F("Card UID:")); for (byte i = 0; i < mfrc522.uid.size; ++i) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); } Serial.println(); } ``` 这段代码展示了基本的功能——检测新卡片的到来以及打印它的UID到串口监视器中去[^4]。 #### 教程资源推荐 除了上述基础内容外,还有更多高级特性等待探索,比如更改UID、复制卡片信息等功能都可以通过查阅相关文档或在线社区获得更多信息[^3]。此外,Mixly作为一款强大的图形化编程平台也支持对这类硬件的操作,适合初学者尝试[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氿 柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值