arduino rc522

读取卡号

#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
}

相关推荐

  1. RC522 读卡

    2024-04-23 23:46:03       35 阅读
  2. arduino rc522

    2024-04-23 23:46:03       34 阅读
  3. STM32 RC522智能门锁

    2024-04-23 23:46:03       40 阅读
  4. leetcode - 527. Word Abbreviation

    2024-04-23 23:46:03       50 阅读
  5. 520. 检测大写字母

    2024-04-23 23:46:03       26 阅读
  6. <span style='color:red;'>52</span> https

    52 https

    2024-04-23 23:46:03      26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 23:46:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 23:46:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 23:46:03       82 阅读
  4. Python语言-面向对象

    2024-04-23 23:46:03       91 阅读

热门阅读

  1. Binlog、Redo 和 Undo 的概念与区别

    2024-04-23 23:46:03       36 阅读
  2. 详解Qt中的时间——QDateTime、QDate、QTime、QTimeZone

    2024-04-23 23:46:03       33 阅读
  3. Swift中的WebView

    2024-04-23 23:46:03       33 阅读
  4. AcWing 802. 区间和——算法基础课题解

    2024-04-23 23:46:03       38 阅读
  5. html实现点击按钮时下方展开一句话

    2024-04-23 23:46:03       31 阅读
  6. C++11中的智能指针

    2024-04-23 23:46:03       25 阅读
  7. Python小程序 - 文件类型统计

    2024-04-23 23:46:03       35 阅读
  8. python如何实现流式接收数据

    2024-04-23 23:46:03       28 阅读
  9. jpa 和 mybatis 的优缺点

    2024-04-23 23:46:03       24 阅读