โมดูล RFID NFC RC-522 13.56MHz SPI SNP-00133
รหัสสินค้า: SNP-00133

โมดูล RFID NFC RC-522 13.56MHz SPI SNP-00133

โมดูล RFID RC522 เป็นโมดูลที่นิยมใช้สำหรับการอ่านบัตร RFID หรือแท็ก NFC สามารถใช้ร่วมกับ Arduino Uno ได้ง่ายโดยการเชื่อมต่อผ่าน SPI interface

 

1.ดาวน์โหลด และติดตั้ง Library MFRC522

 

ติดตั้ง Library

ArduinoIDE > Sketch > Include Library > Add .ZIP Library > หาไฟล์ที่ดาวน์โหลด

 

2.ต่ออุปกรณ์

โมดูล RFID NFC RC-522 13.56MHz SPI SNP-00133

MFRC522 > Arduino UNO

  • SDA > Pin 10
  • SCK > Pin 13
  • MOSI > Pin 11
  • MISO > Pin 12
  • IRQ > ไม่ต้องเชื่อมต่อ
  • GND > GND
  • RST > Pin 9
  • 3.3V > 3.3V (ใช้แรงดัน 3.3V เท่านั้น)

 

3.ลงโปรแกรม 

Copy โค้ดด้านล่าง

Arduino
1#include <SPI.h>
2#include <MFRC522.h>
3
4#define SS_PIN 10
5#define RST_PIN 9
6MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.
7
8void setup() {
9  Serial.begin(9600);   // Initialize serial communications with the PC
10  SPI.begin();          // Init SPI bus
11  mfrc522.PCD_Init();   // Init MFRC522 card
12  
13  Serial.println("Place your card near the reader...");
14}
15
16void loop() {
17  // Look for new cards
18  if ( ! mfrc522.PICC_IsNewCardPresent()) {
19    return;
20  }
21
22  // Select one of the cards
23  if ( ! mfrc522.PICC_ReadCardSerial()) {
24    return;
25  }
26
27  // Show UID on serial monitor
28  Serial.print("UID tag: ");
29  String content = "";
30  for (byte i = 0; i < mfrc522.uid.size; i++) {
31    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
32    Serial.print(mfrc522.uid.uidByte[i], HEX);
33    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
34    content.concat(String(mfrc522.uid.uidByte[i], HEX));
35  }
36  Serial.println();
37  delay(1000);  // Wait 1 second before reading again
38}
39

 

4.ผลลัพธ์

โค้ดจะทำการอ่านค่าของ UID จากบัตรหรือแท็ก RFID ที่ถูกวางใกล้กับเครื่องอ่าน UID จะถูกแสดงใน Serial Monitor

โมดูล RFID NFC RC-522 13.56MHz SPI SNP-00133

 

การนำไปใช้งาน
Copy โค้ดด้านล่าง

Arduino
1#include <SPI.h>
2#include <MFRC522.h>
3
4#define SS_PIN 10
5#define RST_PIN 9
6MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.
7
8void setup() {
9  Serial.begin(9600);   // Initialize serial communications with the PC
10  SPI.begin();          // Init SPI bus
11  mfrc522.PCD_Init();   // Init MFRC522 card
12  pinMode(LED_BUILTIN, OUTPUT);  // Set the LED_BUILTIN pin as an output
13  
14  Serial.println("Place your card near the reader...");
15}
16
17void loop() {
18  // Look for new cards
19  if ( ! mfrc522.PICC_IsNewCardPresent()) {
20    return;
21  }
22
23  // Select one of the cards
24  if ( ! mfrc522.PICC_ReadCardSerial()) {
25    return;
26  }
27
28  // Show UID on serial monitor
29  Serial.print("UID tag: ");
30  String content = "";
31  for (byte i = 0; i < mfrc522.uid.size; i++) {
32    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
33    Serial.print(mfrc522.uid.uidByte[i], HEX);
34    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
35    content.concat(String(mfrc522.uid.uidByte[i], HEX));
36  }
37  Serial.println();
38
39  // Check if the scanned card's UID matches the allowed card
40  if (content.equalsIgnoreCase(" B3 2E FE 28")) {
41    Serial.println("Authorized access! LED on.");
42    digitalWrite(LED_BUILTIN, HIGH);  // Turn on the LED
43  } else {
44    Serial.println("Access denied! LED off.");
45    digitalWrite(LED_BUILTIN, LOW);  // Turn off the LED
46  }
47
48  delay(1000);  // Wait 1 second before reading again
49}
50

 

ผลลัพธ์

โค้ดจะทำการอ่าน UID ของการ์ดที่สแกน และตรวจสอบว่า UID นั้นตรงกับ B3 2E FE 28 หรือไม่
หากตรงกัน LED ที่ติดตั้งบนบอร์ดจะเปิดขึ้นมา และแสดงข้อความ "Authorized access! LED on."
หากไม่ตรงกัน LED จะปิด และแสดงข้อความ "Access denied! LED off."

โมดูล RFID NFC RC-522 13.56MHz SPI SNP-00133