實驗1-1 (ESP32 + MFRC522 RFID Reader )
(讀取 Tag 序號 並記錄 寫在卡片上)
ESP32 因為型號不同pin_out 位置也不同 須留意接線
* -----------------------------------------------------------------------------------------
* MFRC522 ESP32 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST GPIO27 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) GPIO5 10 53 D10 10 10
* SPI MOSI MOSI GPIO23 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO GPIO19 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK GPIO18 13 / ICSP-3 52 D13 ICSP-3 15
*
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 5 // ESP32 pin GIOP5
#define RST_PIN 27 // ESP32 pin GIOP27
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(115200);
SPI.begin(); // init SPI bus
rfid.PCD_Init(); // init MFRC522
Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.print("RFID/NFC Tag Type: ");
Serial.println(rfid.PICC_GetTypeName(piccType));
// print UID in Serial Monitor in the hex format
Serial.print("UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
rfid.PICC_HaltA(); // halt PICC
rfid.PCD_StopCrypto1(); // stop encryption on PCD
}
}
}
沒有留言:
張貼留言