ESP8266 NodeMCU – Room Access Recognition Using RFID

This tutorial intends to introduce RFID technology. It is present in our everyday lives in many applications, such as the traceability of an item in the supply chain, aviation baggage control, sports timing, and access control in facilities, as is the case of this tutorial. The wide use of RFID systems results from this technology’s speed, reliability, and cost-effective advantages.

Parts Required

Note: Although an ESP8266 NodeMCU v1.0 board is used in this tutorial, any similar board can be used instead. Just be sure to change the pin numbers in the code if you use other boards.

RFID Technology

A Radio-Frequency Identification (RFID) module refers to a system where digital data encoded in RFID tags are captured by a reader via radio waves. The point is to automatically identify objects, collect data about them, and enter those data directly into computer systems with little or no human intervention. RFID systems are composed of two different components:

  • RFID tag;
  • RFID reader.

Regarding RFID tags, these are used to transmit data to the RFID reader. To do so, these tags contain a microchip that stores information and an antenna to receive and transmit the signal containing its data, as shown in Figure 1. These are usually passive devices, meaning that they do not require a battery to work. Therefore, passive tags must be powered by the RFID reader before they can transmit the data. Moreover, RFID tags are also composed of a protective material, which comes in a variety of shapes and sizes, that holds the pieces together and shields them from various environmental conditions.

Figure 1 – RFID tags.

In order to read the information encoded on a tag, it must be placed in close proximity to the reader, which is responsible for the conversion of the radio waves to a more usable form of data. The reader generates an electromagnetic field through an antenna which causes electrons to move through the tag’s antenna and subsequently power the chip. The powered chip inside the tag then responds by sending its stored information back to the reader in the form of another radio signal. This change in the radio frequency wave is detected and interpreted by the reader. Finally, the information collected from the tags can then be transferred to a host computer system or microcontroller.

Figure 2 – RFID reader/writer.

Circuit

The hardware used in this tutorial is the RC522 RFID module, which is based on the RFID MFRC522 IC reader/writer from NXP. This is one of the most reliable and inexpensive RFID options and hence the most adequate for many applications. It usually comes with an RFID card tag and key fob tag.

In Figure 3 is presented a diagram of the RFID-RC522 module pinout. In this module, there are 8 pins divided in two kinds: power and communication pins.

Figure 3 – RFID-RC522 pinout, adapted from [1].

The VCC pin supplies power for the module. This can be anywhere from 2.5 to 3.3 volts, according to the MFRC522 datasheet. In order to power-down the module, a low value must be sent to the RST pin. On a rising edge, the module is reset. A reference voltage value (0 Volts) should be applied to the GND pin. The RFID-RC522 module can go into sleep mode if a long period of time passes without the detection of an RFID tag, in order to save power. The IRQ pin is an interrupt pin that can alert the microcontroller when an RFID tag comes into its vicinity. In other words, the IRQ pin helps the module to wake up.

The remaining pins allow developers to interface with the module using any SPI, I2C, and UART based microcontrollers. Starting with the MISO/SCL/TX pin, it acts as a Master-In-Slave-Out when SPI interface is enabled, as a serial clock when I2C interface is enabled, and as a serial data output when UART interface is enabled, respectively. The MOSI pin corresponds to the SPI input to the RC522 module. Regarding the SCK pin, it accepts clock pulses provided by the SPI bus master, which in this case is the ESP8266 NodeMCU microcontroller. Finally, the SS/SDA/RX pin acts as a signal input when SPI interface is enabled, as serial data when I2C interface is enabled and as a serial data input when UART interface is enabled, respectively [2].

With this in consideration, we can begin to connect our circuit, which schematic is represented in Figure 4.

Figure 4 – Circuit’s schematic.

Note that, and recalling the ESP8266 NodeMCU pinout (presented here), there are specific pins for the communication functions, which in this case correspond to pins D5 to D8. Moreover, these pins differ depending on the microcontroller in use. The following table presents the pins of different Arduino boards which should be connected to the corresponding RC522 RFID module pins.

MISOMOSISCKSDA
Arduino UNO12111310
Arduino Mega50515253
Arduino NanoD12D11D13D10
Arduino LeonardoICSP-1ICSP-4ICSP-310
Table 1 – Arduino pins to be connected to the RC522 RFID module pins.

Code

In order for us to easily read from and/or write to RFID tags, a library called MFRC522 library was created and is available for everyone to download. To do so, download the ZIP of library here and open the Arduino IDE. Then, go to Sketch > Include Library > Add .ZIP Library, and then select the rfid-master.zip file that you just downloaded.

This way, we are all set to start coding! The purpose of this tutorial is simple: to recognize if a person is authorized or not to enter a certain room. If this person is allowed to enter the room, a message saying “Authorized” shall appear in the Serial Monitor of the Arduino IDE; otherwise, an “Unauthorized” message shall be displayed. Thus, we need to know firstly the Unic Identifier (UDI) of the tag we want to give access to. To do so, we can use the following function:

mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

As an example, this function can be used as shown in the code below:

#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 16;        // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 15;         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
	Serial.begin(9600);		// 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
}

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));
}

When applied, the result is as follows, where we can see the UID of the tag I want to give access to my room:

Figure 5 – UID of the RFID tag.

Once the UID is known, we can then develop the code to recognize the access to our room. First, we start by including the necessary libraries, define the pins, create an instance of MFRC522 object and define the tag which will have access to the room (called master tag). As the UID of the tag corresponds to a 4 byte character, a byte variable readCard with four bytes must be defined to store this value and for it to be interpreted by the module later on. Then, in the setup function, we must initialize the serial monitor, SPI interface, and MFRC522 object. In the loop function, after a new tag is scanned, its UID is compared with the master tag previously defined. Then, it is necessary to ensure that, depending on if the UID of the tag matches the master UID or not, the serial monitor will display the corresponding “Authorized” and “Unauthorized!” messages. To conclude, a function (getUID) must be defined in order to convert the 4 byte UID into a string and concatenate it to create a single string.

#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 16;        // Define pin D0 for the RST pin
constexpr uint8_t SDA_PIN = 15;        // Define pin D8 for the SDA pin

byte readCard[4];
String MasterTag = "77A4B9B4";  // Tag ID of your RFID card (TO BE SUBSTITUTED)
String tagID = "";

MFRC522 mfrc522(SDA_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
	Serial.begin(9600);	// Initialize serial communications with the PC
	while (!Serial);	// Do nothing if no serial port is opened
	SPI.begin();		// Initialize SPI bus
	mfrc522.PCD_Init();	// Initialize MFRC522
}

void loop() 
{
  //Wait until new tag is available
  while (getUID()) 
  {
    if (tagID == MasterTag) 
    { 
      Serial.println(" Authorized");
    }
    else
    {
      Serial.println(" Unauthorized!");
    } 
    delay(2000);
  }
}
  
  	boolean getUID() 
  {
    // Getting ready for reading Tags
    if ( ! mfrc522.PICC_IsNewCardPresent()) {   //If a new tag is placed close to the RFID reader, continue
    return false;
    }
    if ( ! mfrc522.PICC_ReadCardSerial()) {     //When a tag is placed, get UID and continue
    return false;
    }
    tagID = "";
    for ( uint8_t i = 0; i < 4; i++) {                  // The MIFARE tag in use has a 4 byte UID
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX));  // Adds the 4 bytes in a single string variable
    }
    tagID.toUpperCase();
    mfrc522.PICC_HaltA(); // Stop reading
    return true;
  }

Results

The results obtained show that, once I approximate the master tag (card tag) to the RC522 module, the message indicating that I am authorized to enter the room is shown in the serial monitor. However, if I place another tag (key fob tag) close to the module, the code will recognize that this tag does not have access to the room.

Figure 6 – Results of this tutorial.

Many other features can be developed in this project in order for it to be more complete. As an example, depending on the authorization recognized, different LEDs and buzzers sounds can be incorporated. Moreover, messages can appear on an LCD and a servo motor can move if the entrance is granted to someone. Your imagination is the limit. 😉

If you enjoyed this tutorial, you can visit our YouTube channel to watch all our videos. Thanks for following us and be sure to rate, comment, and share our content. Have fun!!

References

[1] https://microcontrollerslab.com/rc522-rfid-reader-pinout-arduino-interfacing-examples-features/

[2] https://lastminuteengineers.com/how-rfid-works-rc522-arduino-tutorial/

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO