READ RFID TAG USING RFID-RC522 AND ARDUINO

Introduction

In this tutorial, I’ll share with you on how to read the RFiD Tag Using RFID-RC522 and Arduino. The video will cover on :

  1. Installing library for RFID-RC522.
  2. Reading the RFID Tag with example provided.
  3. Use the data read by the RFID-RC522.

Video

Hardware Preparation

This tutorial use :

You can make the hardware connection based on the diagram and table below.

Read Rfid Tag Using Rfid Rc522 And Arduino

Sample Code

This is the sample code used for this tutorial. Try it!

/*
——————————————————————————————————————–
Example sketch/program showing how to read data from a PICC to serial.
——————————————————————————————————————–
Typical pin layout used:
—————————————————————————————–
MFRC522 Arduino Arduino Arduino Arduino Arduino
Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
Signal Pin Pin Pin Pin Pin Pin
—————————————————————————————–
RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
SPI SS SDA(SS) 10 53 D10 10 10
SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
#define PIEZO 8
#define RED_LED 4
#define GREEN_LED 3
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
#define NOTE_G4 392
#define NOTE_C5 523
#define NOTE_G5 784
#define NOTE_C6 1047
int TrueMelody[] = {NOTE_G5, NOTE_C6};
int TrueNoteDurations[] = {12, 8};
int FalseMelody[] = {NOTE_C6, NOTE_G5};
int FalseNoteDurations[] = {12, 8};
#define playTrueMelody() playMelody(TrueMelody, TrueNoteDurations, 2)
#define playFalseMelody() playMelody(FalseMelody, FalseNoteDurations, 2)
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
Serial.println("Please put your card on the RFID reader..");
pinMode(RED_LED,OUTPUT);
pinMode(GREEN_LED,OUTPUT);
pinMode(PIEZO,OUTPUT);
digitalWrite(RED_LED,HIGH);
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String RFIDtag = "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
RFIDtag.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
RFIDtag.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
RFIDtag.toUpperCase();
if ( RFIDtag.substring(1) == "00 00 00 00" )//Change the 00 00 00 00 to your UID card number that you want to give access
{
Serial.println("Access is granted");
playTrueMelody();
digitalWrite(GREEN_LED,HIGH);
digitalWrite(RED_LED,LOW);
delay(3000);
digitalWrite(GREEN_LED,LOW);
digitalWrite(RED_LED,HIGH);
Serial.println("Please put your card on the RFID reader..");
}
else {
Serial.println("Access is denied");
playFalseMelody();
digitalWrite(GREEN_LED,LOW);
digitalWrite(RED_LED,HIGH);
delay(1000);
Serial.println("Please put your card on the RFID reader..");
}
}
void playMelody(int *melody, int *noteDurations, int notesLength)
{
pinMode(PIEZO, OUTPUT);
for (int thisNote = 0; thisNote < notesLength; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(PIEZO, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO);
}
}

view raw
RFID_RC522.ino
hosted with ❤ by GitHub

References :

  1. Viral Science Creativity

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Getting Started with Robotic Operating System (ROS) and JetRacer AI Kit
Barrier Gate with micro:bit and MakeCode
Play MP3 File on Maker Pi Pico Using CircuitPython
Remote Jetson Nano using SSH with Visual Studio Code
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Tutorials of Cytron Technologies Scroll to Top