반응형

RFID-RC522 모듈과 아두이노를 이용해 RFID카드를 읽고 쓸 수 있다.


RFID-RC522


RFID Cards


위 다이어그램과 같이 연결한다.


ARDUINO - RFID-RC522

3.3V - 3.3V

D9 - RST

GND - GND

N/A - IRQ

D12 - MISO

D11 - MOSI

D13 - SCK

D10 - SDA



Library Manager에서 MFRC522을 검색하고 설치한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
 
// Array for new NUID 
byte nuidPICC[4= {0};
 
void setup() {
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522
}
 
void loop() {
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if (!rfid.PICC_IsNewCardPresent()) {
    delay(500);
    
    return;
  }
 
  // Verify if the NUID has been read.
  if (!rfid.PICC_ReadCardSerial()) {
    delay(500);
    
    return;
  }
 
  if (rfid.uid.uidByte[0!= nuidPICC[0||
    rfid.uid.uidByte[1!= nuidPICC[1||
    rfid.uid.uidByte[2!= nuidPICC[2||
    rfid.uid.uidByte[3!= nuidPICC[3] ) {
    Serial.println("A new card has been detected.");
 
    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
  } else {
    delay(500);
 
    return;
  }
 
  Serial.print("PICC type: ");
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));
 
  // Check if classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println("Your tag is not MIFARE Classic type.");
    
    return;
  }
 
  Serial.print("Card UID: ");
  for (byte i = 0; i < 4; i++) {
    Serial.print(rfid.uid.uidByte[i]);
    if (i < 3)
      Serial.print("-");
  }
  Serial.println();
}


RFID 카드 읽기 소스를 컴파일 하고 아두이노에 업로드한다. 


RFID-RC522 모듈에 카드를 가까이 대면 카드 타입과 ID가 표시된다.


반응형
Posted by J-sean
: