Skip to content

Instantly share code, notes, and snippets.

@TakuroFukamizu
Created September 9, 2019 14:32
Show Gist options
  • Save TakuroFukamizu/1624c168deb75a2060c41705c4605c28 to your computer and use it in GitHub Desktop.
Save TakuroFukamizu/1624c168deb75a2060c41705c4605c28 to your computer and use it in GitHub Desktop.
RFID-MFRC522 with M5Stack
#include <SPI.h>
#include <MFRC522.h>
#include <M5Stack.h>
constexpr uint8_t RST_PIN = 2;
constexpr uint8_t SS_PIN = 5;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522::MIFARE_Key key;
void setup() {
M5.begin(true, false, true, false); //SD, I2Cをdisableに
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
mfrc522.PCD_Init(); // Init MFRC522
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
M5.Lcd.begin(); // 画面のbeginを再実行する
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(2);
M5.Lcd.println("Scan PICC to see UID, SAK, type, and data blocks...");
Serial.println("Scan PICC to see UID, SAK, type, and data blocks...");
}
void loop() {
//カードの読み取り確認
if (!mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()) {
// (!mfrc522.PICC_IsNewCardPresent()) : Look for new cards
// (!mfrc522.PICC_ReadCardSerial()) : Select one of the cards
delay(500);
return;
}
char mes1[9];
sprintf(mes1, "%02x%02x%02x%02x", mfrc522.uid.uidByte[0], mfrc522.uid.uidByte[1], mfrc522.uid.uidByte[2], mfrc522.uid.uidByte[3]);
Serial.printf("UID: %s\n", mes1);
M5.Lcd.setCursor(10, 40);
M5.Lcd.setTextSize(2);
M5.Lcd.printf("UID: %s\n", mes1);
// // Dump debug info about the card; PICC_HaltA() is automatically called
// mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(1000);
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment