Quick guide to wire and use the RC522 RFID module with Arduino
Hello everybody and welcome to this quick tutorial, today we gonna use a Mifare RC522 RFID module with an Arduino Uno and an optional LCD i2c screen.
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
The RFID-RC522 will permit you to read RFID Tags, identify their IDs, you can use it in door lock projects, garages, safes…. For the tags there are two types, here I only used one as shown in the video, it’s not” re-writable”, it has a fix ID. There are other versions that are re-writable which means you can modify the Tag data, and it could be very interesting project in case you want to make a credit system to grant access for only few times… or store data.
This is not a tutorial that I’m proud of, now I check after couple of years it needs a revisit, and a proper project, but this will get you started and push through your school project as I did before.
Wiring:
This wiring will do the work but won’t last for long, as I had trouble using a dodgy version of a level shifter. The LCD i²c screen is optional.

For better use as mentioned you should wire the pins from the module with a level shifter then with the Arduino Uno.
As you can see it’s technically 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.
Libraries
You’ll need the RFID library, this is the one I used to not confuse you: Download here, but you can use some other ones that are better like MFRC522…
If you’re using LCD i²c you’ll need a library also: Download here.
Codes:
The first code is a simple reading of the TAG’s code, every TAG has its “own” code, and it displays it on the serial monitor and the LCD if wired as well.
Code 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
//Code for reading RFID Tag/Card with RFID-RC522 and show it's ID on the lcd i2c display //SurtrTech Youtube channel #include <LiquidCrystal_I2C.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 use it on camera, because the module barely worked on the first one, so the second code is for giving access to cards or not, you should use the first code to know the cards ID then add them on the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
//Code for reading RFID Tag/Card and giving access or not to it //SurtrTech Youtube channel #include <LiquidCrystal_I2C.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 int serNum[5]; int cards[][5] = { {35,231,84,0,144}, // Authorized Card ID, You can add others if you want after the last comma like {x1,x2,x3,x4,x5},{y1,y2,y3,y4,y5}, }; bool access = false; 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"); } } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
Hello , i want to ask . What is the function int serNum[5]; ,int cards[][5] = {35,231,84,0,144}. If you dont mind, can u explain it more detail.Thank you
Those are arrays to store the RFID Tag Id, the one with numbers is already registered and we compare it with the one used with module using functions that use the serNum array.