目的
MFRC522 RFID IC カード リーダーを使用して何かを制御するための前準備です。
準備
Arduino nano 互換品
MFRC522 RFID IC カード リーダー
回路図
MFRC522 Reader/PCD <---> Arduino Nano v3
下記の通り接続します。
RST <---> D9
SDA(SS) <---> D10
MOSI <---> D11
MISO <---> D12
SCK <---> D13
コード
MFRC522 RFID IC カード リーダーのライブラリを入手し、
DumpInfoサンプルコードをビルドして使用します。
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(115200); // 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 MFRC522
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() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
テスト
MFRC522 RFID IC カード リーダーにタグを近づけた際にIDなどを読み取ることができればOKです。
use MFRC522 RFIDモジュール pic.twitter.com/2FJHGNtYxA
— st (@st17890027) March 3, 2019