Skip to content
Advertisements

Quick guide to wire and use the RC522 RFID module with Arduino

 

Hello everybody to this quick tutorial, today we gonna use a Mifare RC522 RFID module with Arduino Uno, It works on every Arduino Board.

IMPORTANT: Don’t forget that the module is powred with 3.3v from Arduino and you can use the other pins normally but for better use, you should use a level shifter (5v to 3.3v) to not damage the component over time.

I didn’t know about this so my RC522 is kinda broken, I couldn’t use it the proper way.

In case it’s your first time using LCD i2c Screen here’s a tutorial:

Arduino LCD I2C simple use and direct write from serial monitor

Wiring:

For better use as I said you should wire the pins from the module with a level shifter then with the Arduino Uno

As you can see it’s pretty simple to use, I’ll try one when I receive it. You have HV pins for High voltage (5v) from Arduino and LV pins for low Voltage for the Module.

 

Codes:

Code 1:

//Code for reading RFID Tag/Card with RFID-RC522 and show it's ID on the lcd i2c display

//SurtrTech Youtube channel

//Facebook @SurtrTech

//


#include <Wire.h> //Libraries for i2c and rfid

#include <LCD.h>

#include <LiquidCrystal_I2C.h>

#include <SPI.h>

#include <RFID.h> //RFID library


#define SS_PIN 10 //SDA pin

#define RST_PIN 9 //Reset pin

#define I2C_ADDR 0x27 //I2c Address

#define BACKLIGHT_PIN 3

#define En_pin 2

#define Rw_pin 1

#define Rs_pin 0

#define D4_pin 4

#define D5_pin 5

#define D6_pin 6

#define D7_pin 7


LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);


RFID rfid(SS_PIN, RST_PIN); 



void setup() {

  Serial.begin(9600);

  SPI.begin(); 

  rfid.init();

  lcd.begin (16,2);

  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);

  lcd.setBacklight(HIGH);

  lcd.home ();

  lcd.print("Hello"); //Showing hello on the screen of 3s

  delay(3000);

  lcd.clear();

  }




void loop() { 

    if (rfid.isCard()) { //Waiting for a Tag/Card to be near the RFID antenna

        if (rfid.readCardSerial()) { //If there is a RFID card

          lcd.clear(); //Clearing lcd screen from previous value

            

                Serial.println(" "); //Showing the ID on the serial monitor

                Serial.println("RFID card ID is:");

                

                for(int i=0;i<5;i++)

                     {

                       Serial.print(rfid.serNum[i],HEX);

                       Serial.print(", ");

                

                     }

                 Serial.println(" ");

                 Serial.println("RFID card ID in HEX is:");

                for(int i=0;i<5;i++)

                     {

                       Serial.print(rfid.serNum[i]);

                       Serial.print(", ");

                

                     }



                 

                  delay(500); //Delay of 500ms so we don't have multiple reading at once

                  lcd.setCursor (0,0); // Printing the Hexadecimal value of the ID on the LCD

                  lcd.print("Card ID is: ");

                  lcd.setCursor(0,1);

                  lcd.print(rfid.serNum[0],HEX);

                  lcd.setCursor(2,1); //You can see that the position is incremented by 2 because everynumber occupy 2 cases

                  lcd.print(rfid.serNum[1],HEX);

                  lcd.setCursor(4,1);

                  lcd.print(rfid.serNum[2],HEX);

                  lcd.setCursor(6,1);

                  lcd.print(rfid.serNum[3],HEX);

                  lcd.setCursor(8,1);

                  lcd.print(rfid.serNum[4],HEX);

   

             }

             }

          }

Code 2:

The second code I didn’t used because the module barely worked on the first one, so the second code is for giving access to cards or not, you should first use the first code to know the cards ID then add them on the code

//Code for reading RFID Tag/Card and giving access or not to it

//SurtrTech Youtube channel

//Facebook @SurtrTech

//

int serNum[5];

int cards[][5] = {

   {35,231,84,0,144}, // Authorized Card ID, You can add others if you want after the comma like {x1,x2,x3,x4,x5}

    };

 bool access = false;

#include <Wire.h> //Libraries for i2c and rfid

#include <LCD.h>

#include <LiquidCrystal_I2C.h>

#include <SPI.h>

#include <RFID.h> //RFID library

#define SS_PIN 10 //SDA pin

#define RST_PIN 9 //Reset pin

#define I2C_ADDR 0x27 //I2c Address

#define BACKLIGHT_PIN 3

#define En_pin 2

#define Rw_pin 1

#define Rs_pin 0

#define D4_pin 4

#define D5_pin 5

#define D6_pin 6

#define D7_pin 7

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

RFID rfid(SS_PIN, RST_PIN);

void setup() {

  Serial.begin(9600);

  SPI.begin();

  rfid.init();

  lcd.begin (16,2);

  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);

  lcd.setBacklight(HIGH);

  lcd.home ();

  lcd.print("Hello"); //Showing hello on the screen of 3s

  delay(3000);

  lcd.clear();

  lcd.print("Please place");

  lcd.setCursor(0,1);

  lcd.print("your card");

  }

void loop() {

    if (rfid.isCard()) { //Waiting for a Tag/Card to be near the RFID antenna

        if (rfid.readCardSerial()) { //If there is a RFID card

          lcd.clear(); //Clearing lcd screen from previous value

           

              for(int x = 0; x < sizeof(cards); x++){

                    for(int i = 0; i < sizeof(rfid.serNum); i++ ){ //These two loops are for comparing the card ID with authorized ID's we set before

                  if(rfid.serNum[i] != cards[x][i]) {

                      access = false; //If one number is different from the authorized card, access become false

                      break;

                  } else {

                      access = true;

                  }

              }

              if(access) break;

            }

     

             }

             delay(300);

             if(access){ //If acces is given do the following instructions, I just show a simple message, you can turn on a LED

              lcd.clear();// Or activate a relay to unlock a real door or something...

              lcd.setCursor(0,0);

              lcd.print("Access granted");

 }

  else{

    lcd.clear();

    lcd.setCursor(0,0);

    lcd.print("Access denied");

  }

              delay(5000);//After 5s we show the first message again to be on standby

              lcd.clear();

              lcd.print("Please place");

              lcd.setCursor(0,1);

              lcd.print("your card");

             }

          }
Advertisements

Yassine View All

Electronics, Automation engineering student... I try to share my little experience with other electronics amateur.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: