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
}

猜你喜欢

转载自blog.csdn.net/weixin_52051554/article/details/138140170