In the previous tutorial, I have made how to use the RC522 RFID reader with Arduino. In this article, I will make a tutorial on how to use this RFID reader with a nodeMCU.
We can use the same library that was made by miguel just like what we did with an Arduino. The differences are the pins. We have the different pins between NodeMCU and Arduino. So how do we connect an RC522 RFID reader with a NodeMCU?
You can buy this device at the link below (affiliate links) :
NodeMCU and RC522 RFID reader wiring diagram
Connect NodeMCU and RFID Reader just like the wiring diagram above. And we can start the code.
NodeMCU and RC522 RFID reader Code
We can use library made by miguel. You can download the library here, and install to arduino.
To Read the tag id or serial code you can use the code I made below. It simpler than in library example.
/* Read RFID Tag with RC522 RFID Reader * Made by miliohm.com */ #include <SPI.h> #include <MFRC522.h> constexpr uint8_t RST_PIN = D3; // Configurable, see typical pin layout above constexpr uint8_t SS_PIN = D4; // Configurable, see typical pin layout above MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; String tag; void setup() { Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 } void loop() { if ( ! rfid.PICC_IsNewCardPresent()) return; if (rfid.PICC_ReadCardSerial()) { for (byte i = 0; i < 4; i++) { tag += rfid.uid.uidByte[i]; } Serial.println(tag); tag = ""; rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } }
Upload the code to NodeMCU and you should see the tag code every a tag is tapped on RFID reader. If it doesn’t work. Recheck the wiring, make sure that the wiring is correct and every wiring is connected.
I also make a demo code that will ring a buzzer 3 times when a right tag is tapped, and the buzzer will ring long when wrong tag is tapped. The right tag is “1193818595”. You can edit the right tag as a key in code below.
/* Read RFID Tag with RC522 RFID Reader Made by miliohm.com */ #include <SPI.h> #include <MFRC522.h> constexpr uint8_t RST_PIN = D3; // Configurable, see typical pin layout above constexpr uint8_t SS_PIN = D4; // Configurable, see typical pin layout above MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; String tag; void setup() { Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 pinMode(D8, OUTPUT); } void loop() { if ( ! rfid.PICC_IsNewCardPresent()) return; if (rfid.PICC_ReadCardSerial()) { for (byte i = 0; i < 4; i++) { tag += rfid.uid.uidByte[i]; } Serial.println(tag); if (tag == "1193818595") { Serial.println("Access Granted!"); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); } else { Serial.println("Access Denied!"); digitalWrite(D8, HIGH); delay(2000); digitalWrite(D8, LOW); } tag = ""; rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } }
Add More Tags
If you want to add more tags that accepted, then you can simply use if-else. Here’s the example if I add more 2 tags. the tags uid is “2293818595” and “393818595” if you want to add more, just add more else if before else.
/* Read RFID Tag with RC522 RFID Reader Made by miliohm.com */ #include <SPI.h> #include <MFRC522.h> constexpr uint8_t RST_PIN = D3; // Configurable, see typical pin layout above constexpr uint8_t SS_PIN = D4; // Configurable, see typical pin layout above MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; String tag; void setup() { Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 pinMode(D8, OUTPUT); } void loop() { if ( ! rfid.PICC_IsNewCardPresent()) return; if (rfid.PICC_ReadCardSerial()) { for (byte i = 0; i < 4; i++) { tag += rfid.uid.uidByte[i]; } Serial.println(tag); if (tag == "1193818595") { Serial.println("Access Granted!"); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); } else if (tag == "2293818595") { Serial.println("Access Granted!"); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); } else if (tag == "3393818595") { Serial.println("Access Granted!"); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); digitalWrite(D8, HIGH); delay(100); digitalWrite(D8, LOW); delay(100); } else { Serial.println("Access Denied!"); digitalWrite(D8, HIGH); delay(2000); digitalWrite(D8, LOW); } tag = ""; rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } }
Here’s the complete video on this tutorial :