Interfacing RFID RC522 Reader to Arduino: A step-by-step guide on lighting up LED

in utopian-io •  7 years ago  (edited)

sku_403052_1.jpg

image source

Radio-Frequency Identification (RFID) uses electromagnetic fields to automatically identify and track tags attached to objects. These tags contain electronically stored information. Different tags are usually attached to objects and when we place that object in front of the reader, the reader reads the tags. Another benefit of RFID is that it doesn’t require to be in a line of sight to get detected. In this tutorial, I used MF RC522 that can detect tags up to 15cm. And this tutorial aims to light up an LED when the right tag will be detected.

What Will I Learn?

  • How to interface RC522 RFID Scanner with Arduino
  • Light up LED using the RC522 RFID Scanner

Requirements

  • Arduino Uno
  • MF RC522 RFID Scanner
  • Connecting Wires
  • LED
  • 330 ohms resistor
  • Breadboard

Difficulty

  • Basic

Tutorial Contents

Step 1: Physical Connection from RC522 RFID Scanner to Arduino Uno

Untitled Sketch_bb.jpg

Created using fritzing

Connect the following:

RFID ScannerArduino Uno
SDApin 10
SCKpin 13
MOSIpin 11
MISOpin 12
GNDGND
RSTpin 8
3.3V3.3V
  • pin 4 to resistor and the resistor connected to the (+ sign) of LED then the (- sign) to the GND.

Step 2: Software Connection

  • Connect the USB Cable to a PC/Laptop.
  • Open your Arduino IDE, change your board to Arduino Uno: Go to Tools > Board: > Arduino/Genuino Uno
    1.png
  • And also, change the Port settings: Go to Tools > Port: > COM6 (Arduino/ Genuino Uno)
    2.png
  • Install the MFRC522 library this will be used to initialize our pins in Arduino. Go to Sketch > Include Library > Manage Libraries
    3.png
  • A new window will appear and search for "MFRC522" then click Install.
    5.png

Step 3: The Code

Include the RC522 Library

#include <MFRC522.h> 

Used for communication via SPI with the Module

#include <SPI.h> 

RFID Module SDA Pin is connected to the pin 10
RFID Module RST Pin is connected to the pin 8
LED is connected to the pin 4

#define SDAPIN 10 
#define RESETPIN 8 
#define LED 4

Initializing all the variables

byte FoundTag; // Variable used to check if Tag was found
byte ReadTag; // Variable used to store anti-collision value to read Tag information
byte TagData[MAX_LEN]; // Variable used to store Full Tag Data
byte TagSerialNumber[5]; // Variable used to store only Tag Serial Number
byte GoodTagSerialNumber[5] = {0x22, 0x23, 0x15, 0xD}; // The Tag Serial number we are looking for

Initialization of the library using the ARDUINO pins declared above

MFRC522 nfc(SDAPIN, RESETPIN);

Code inside the setup()

void setup() {
  SPI.begin(); // Opens up the communicationto the serial monitor in Arduino IDE
  Serial.begin(9600); 
  pinMode(LED, OUTPUT); // Setup the pin 4 to be the output of LED
// Start to find an RFID Module
  Serial.println("Looking for RFID Reader");
  nfc.begin();
  byte version = nfc.getFirmwareVersion(); // Variable to store Firmware version of the Module
// If can't find an RFID Module
  if (! version) {
    Serial.print("Didn't find RC522 board.");
    while (1); //Wait until a RFID Module is found
  }
  // If found, print the information about the RFID Module
  Serial.print("Found chip RC522 ");
  Serial.print("Firmware version: 0x");
  Serial.println(version, HEX);
  Serial.println();
}

Code inside the loop()

String GoodTag = "False"; // Variable used to confirm good Tag Detected
// Check to see if a Tag was detected
 // If yes, then the variable FoundTag will contain "MI_OK"
  FoundTag = nfc.requestTag(MF1_REQIDL, TagData);
if (FoundTag == MI_OK) {
    delay(200);
    // Get anti-collision value to properly read information from the Tag
    ReadTag = nfc.antiCollision(TagData);
    memcpy(TagSerialNumber, TagData, 4); // the tag information in the TagSerialNumber variable
// Check if detected Tag has the right Serial number we are looking for
    for (int i = 0; i < 4; i++) {
      if (GoodTagSerialNumber[i] != TagSerialNumber[i]) {
        break; // if not equal, then break out of the "for" loop
      }
      if (i == 3) { // if we made it to 4 loops then the Tag Serial numbers are matching
        GoodTag = "TRUE";
      }
    }
    if (GoodTag == "TRUE") {
      Serial.println("Success!");
      Serial.println();
      digitalWrite(LED, HIGH);
      delay(1500);
    }
else {
      Serial.println("TAG NOT ACCEPTED");
      Serial.println();
      digitalWrite(LED, LOW);
    }
    delay(500);
  }
}

My Circuit

26803899_1802744933070616_2064544024_n.jpg

When the card tag is scanned the LED will light up.
26856805_1802744949737281_562709516_n.jpg

When the keychain tag is scanned the LED will be off.
26913372_1802744946403948_31032016_n.jpg

Tip to remember: The range of RFID to detect a tag depends on the fequency of what the module has, so if you need a longer range in your project choose the higher frequency.

Thanks for reading! Hope you learn something.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

The contribution has been accepted.

[utopian moderator]

Thank you sir

very intersting you have my vote!!!!

thanks bro!

Another exceptional post right there @kimp0gi!
You have just proven how good the engineering studs our university has produced and how good engineers in the future we could be. You are a very important asset of the steemph.cebu community.

wohoo! im just next to you @japh hahaha

Hey @kimp0gi I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x