From 6c9726e01d71e2ef3c80177a4da3d82a1dcd2d85 Mon Sep 17 00:00:00 2001 From: Semaf Electronics Date: Sun, 24 Jun 2018 15:31:58 +0200 Subject: [PATCH] Create mfrc522_i2c.ino --- mfrc522_i2c.ino | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 mfrc522_i2c.ino diff --git a/mfrc522_i2c.ino b/mfrc522_i2c.ino new file mode 100644 index 0000000..5bb4ee0 --- /dev/null +++ b/mfrc522_i2c.ino @@ -0,0 +1,54 @@ +#include +#include "MFRC522_I2C.h" + +#define RST_PIN 6 // Arduino UNO Pin +// #define RST_PIN 14 // D5 Pin on NodeMCU + + +// 0x28 is i2c address on SDA. Check your address with i2cscanner if not match. +MFRC522 mfrc522(0x28, RST_PIN); // Create MFRC522 instance. + +void setup() { + Serial.begin(115200); // Initialize serial communications with the PC + Wire.begin(); // Initialize I2C + mfrc522.PCD_Init(); // Init MFRC522 + ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details + Serial.println(F("Scan PICC to see UID, type, and data blocks...")); +} + +void loop() { + // Look for new cards, and select one if present + if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) { + delay(50); + return; + } + + // Now a card is selected. The UID and SAK is in mfrc522.uid. + + // Dump UID + Serial.print(F("Card UID:")); + for (byte i = 0; i < mfrc522.uid.size; i++) { + Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); + Serial.print(mfrc522.uid.uidByte[i], HEX); + } + Serial.println(); +} + +void ShowReaderDetails() { + // Get the MFRC522 software version + byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); + Serial.print(F("MFRC522 Software Version: 0x")); + Serial.print(v, HEX); + if (v == 0x91) + Serial.print(F(" = v1.0")); + else if (v == 0x92) + Serial.print(F(" = v2.0")); + else + Serial.print(F(" (unknown)")); + Serial.println(""); + // When 0x00 or 0xFF is returned, communication probably failed + if ((v == 0x00) || (v == 0xFF)) { + Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); + } +} +