code
نسخ
اقتباس
عرض
تنزيل
#include <SPI.h>
#include <MFRC522.h>
/*
MFRC522 SDA (Serial Data) pin to ESP32 GPIO pin (e.g., GPIO22)
MFRC522 SCK (Serial Clock) pin to ESP32 GPIO pin (e.g., GPIO18)
MFRC522 MOSI (Master Out Slave In) pin to ESP32 GPIO pin (e.g., GPIO23)
MFRC522 MISO (Master In Slave Out) pin to ESP32 GPIO pin (e.g., GPIO19)
MFRC522 RST (Reset) pin to ESP32 GPIO pin (e.g., GPIO5)
MFRC522 GND (Ground) pin to ESP32 GND (Ground)
MFRC522 3.3V (Power) pin to ESP32 3.3V (Power)
*/
#define RST_PIN 5 // Define the RST_PIN for the MFRC522 module
#define SS_PIN 22 // Define the SS_PIN for the MFRC522 module
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(115200);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Scan for an RFID card...");
}
void loop() {
// Look for new cards
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
// Show UID on serial monitor
Serial.print("UID tag :");
String content = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
Serial.println(content);
delay(1000);
}
}