RFID.ino 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <M5Stack.h>
  2. #include <Wire.h>
  3. #include "MFRC522_I2C.h"
  4. // 0x28 is i2c address on SDA. Check your address with i2cscanner if not match.
  5. MFRC522 mfrc522(0x28); // Create MFRC522 instance.
  6. void setup() {
  7. M5.begin();
  8. M5.Power.begin();
  9. M5.Lcd.fillScreen(BLACK);
  10. M5.Lcd.setCursor(0, 0);
  11. M5.Lcd.setTextColor(YELLOW);
  12. M5.Lcd.setTextSize(2);
  13. M5.Lcd.fillScreen(BLACK);
  14. M5.Lcd.setCursor(0, 0);
  15. M5.Lcd.println("M5StackFire MFRC522");
  16. Serial.begin(115200); // Initialize serial communications with the PC
  17. Wire.begin(); // Initialize I2C
  18. mfrc522.PCD_Init(); // Init MFRC522
  19. ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
  20. Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
  21. M5.Lcd.println("Scan PICC to see UID, type, and data blocks...");
  22. }
  23. void loop() {
  24. // Look for new cards, and select one if present
  25. if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
  26. delay(50);
  27. return;
  28. }
  29. // Now a card is selected. The UID and SAK is in mfrc522.uid.
  30. // Dump UID
  31. Serial.print(F("Card UID:"));
  32. M5.Lcd.println(" ");
  33. for (byte i = 0; i < mfrc522.uid.size; i++) {
  34. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  35. Serial.print(mfrc522.uid.uidByte[i], HEX);
  36. M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  37. M5.Lcd.print(mfrc522.uid.uidByte[i], HEX);
  38. }
  39. Serial.println();
  40. }
  41. void ShowReaderDetails() {
  42. // Get the MFRC522 software version
  43. byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  44. Serial.print(F("MFRC522 Software Version: 0x"));
  45. Serial.print(v, HEX);
  46. if (v == 0x91)
  47. Serial.print(F(" = v1.0"));
  48. else if (v == 0x92)
  49. Serial.print(F(" = v2.0"));
  50. else
  51. Serial.print(F(" (unknown)"));
  52. Serial.println("");
  53. // When 0x00 or 0xFF is returned, communication probably failed
  54. if ((v == 0x00) || (v == 0xFF)) {
  55. Serial.println(
  56. F("WARNING: Communication failure, is the MFRC522 properly "
  57. "connected?"));
  58. }
  59. }