HUM: RFID READER MFRC-522

HUM: RFID READER MFRC-522-Uncategorized

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Dasduino and the basic code. Everything else is left to your imagination.

INTRODUCTION

In this tutorial, we will introduce you to another technology that allows wireless data transfer. Although it is used at very short distances, it has its role in many parts of modern life due to its reliability and resistance to malfunction. From tracking goods in transport, all the way to controlling access to facilities.

• 13.56 MHz RFID technology
• IC : Philips MF RC522
• Voltage : 3.3V
• Current : 10mA(idle), 26mA(active)
• Reading distance from the sensor : <5cm (with tags that come with this reader)
• Comes with a passive RFID card and an appendage
• Comes with flat and bent headers, unsoldered.

HOW DOES IT WORK?

MFRC522 is an integrated IC for contactless communication at 13.56 MHz. This chip controls the antenna designed for reading and sending data. The base station, i.e. RFID reader consists of an antenna, an RF module and a control unit. RFID tags and cards have a similar construction, but without active components, which means they do not contain any batteries or power supplies. They only consist of an antenna and a microchip.
The communication takes place as follows. The base station (antenna) constantly emits electromagnetic waves. When the RFID card or tag approaches the base at a distance of about 10 cm, the voltage on the receiving antenna. which serves as a power supply for the built-in microchip, will be induced under the influence of an electromagnetic field. Now that the card or tag have the energy necessary for normal operation, the built-in chips manipulate the load so that the base station can read a series of ones and zeros.

HOW TO CONNECT IT?

Simply connect the module with your Dasduino via SPI communication. RST and SS can be changed in the code according to your needs. It is necessary to note that the module operates at 3,3V. Also, add a resistors of smaller values (e.g. 470 Ohm) to the LEDs and watch out for the polarity!
RST ====> 9
SDA(SS) ====> 10
MOSI ====> 11
MISO ====> 12
SCK ====> 13
Vcc ====> +3V3
GND ====> GND
red LED ====> 8
green LED ====> 7

ARDUINO CODE

After we have successfully connected the module, it is necessary to download the MFRC522 library and install it in the Arduino IDE. This library comes with several good examples that can help us learn how to use module. If you need any help installing libraries, check the instructions on the link. Since each RFID tag or card have their unique identification set of digits, known as UID (Unique Identifier) we can use it in a project so to, for example, unlock doors or grant access to the user, while the access for nonregistered users is denied. This will be shown in the example using two LEDs. When the user presses a valid card or tag against the reader, green light will turn on, i.e. that signal can be used to trigger another function, e.g. a servo that will unlock the door, and when a nonregistered tag or card are pressed against, red light signal will turn on, i.e. access denied.

#include "SPI.h"
#include "MFRC522.h"
#define RST_PIN 9  
#define SS_PIN  10 
#define rfid_green  7
#define rfid_red  8     
MFRC522 mfrc522(SS_PIN, RST_PIN);
String read_rfid;
// add cards or tags that grant access here
// in order to find out the card/tag UID, first try out the sketch "DumpInfo" and place the card
// or tag against the reader
// card/tag UID will be printed out on the serial monitor
String rfid_card = "70bf602b"
String rfid_tag = "ccc94683";
void setup() {
    Serial.begin(9600);     
    while (!Serial);         
    SPI.begin();              
    mfrc522.PCD_Init();        
    pinMode(rfid_green, OUTPUT);
    pinMode(rfid_red, OUTPUT);
}
void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid += String(buffer[i], HEX);
    }
}
void open_lock() {
  digitalWrite(rfid_red, LOW);
  digitalWrite(rfid_green, HIGH);
  delay(2000);
  digitalWrite(rfid_green,LOW);
}
void deny_access(){
  digitalWrite(rfid_green, LOW);
  digitalWrite(rfid_red, HIGH);
  delay(2000);
  digitalWrite(rfid_red,LOW);
}
void loop() {
  if ( ! mfrc522.PICC_IsNewCardPresent())
    return;
        
  if ( ! mfrc522.PICC_ReadCardSerial())
    return;
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    
  Serial.println(read_rfid);
    
  if (read_rfid == rfid_card) {
    open_lock();
  }
  else{
    deny_access();
  }
}