arduino rc522

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

读取卡号

#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
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

氿 柒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值