아두이노

Arduino Mega 2560으로 RFID 쓰기 [2/2]

k1asd1 2024. 1. 29. 13:17
728x90
반응형

Arduino Mega 2560으로 RFID 읽기에 이어 쓰기도 같이 진행합니다. 핀 배열은 이전과 동일하며 소스만 조금 수정하여 사용합니다.


2024.01.29 - [아두이노] - Arduino Mega 2560으로 RFID 읽기 [1/2]

 

Arduino Mega 2560으로 RFID 읽기 [1/2]

마침 아두이노 배울 당시 초창기에 구입했었던 Arduino Mega 2560이 있어 RFID 읽기를 해보았습니다. * 준비물 - Arduino IDE - Arduino MEGA 2560 - RC522 모듈 - RFID TAG(13.56MHZ) * Arduino MEGA 2560와 RC522 모듈 연결하기

k1asd1.tistory.com


* 소스 불러오기 및 수정

- Arduino IDE -> 파일 -> 예제 -> MFRC522 -> ChangeUID 열기

- 소스 상단의 내용을 아래와 같이 변경하고 업로드합니다

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          53         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

byte newUid[] = {0x01, 0x23, 0x45, 0X67}; //new UID

void setup() {
	Serial.begin(9600);		// Initialize serial communications with the PC
	while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
	SPI.begin();			// Init SPI bus
	mfrc522.PCD_Init();		// Init MFRC522s
	delay(4);				// Optional delay. Some board do need more time after init to be ready, see Readme
	//mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
	// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
	if (!mfrc522.PICC_IsNewCardPresent()) {
		return;
    Serial.println("Found Card");
	}

	// Select one of the cards
	if (!mfrc522.PICC_ReadCardSerial()) {
		return;
    Serial.println("Read Card Done");
	}

  if (mfrc522.MIFARE_SetUid(newUid, (byte)4, true)) {
    Serial.println(F("wrote new UID to Card"));
  }

  mfrc522.PICC_HaltA();

  delay(2000);
}

 

* 기존의 내용에서 일부 변경되었습니다.

ChangeUID.ino
0.00MB

 

*결과 확인

- 업로드가 완료되면 시리얼 모니터를 열고 새로운 카드를 대어줍니다.

 

- 정상적으로 쓰기 되었을 경우 아래와 같은 문구가 출력됩니다.

정상적으로 쓰기 되었을 경우

 

-UID 쓰기가 불가능한 TAG일 경우 아래와 같은 문구가 출력됩니다.

UID 변경이 불가능한 TAG일 경우


이상입니다.

728x90
반응형