Arduino RFID Reader RC522 + Access Control System

I just received my RC522 RFID reader and made this simple Arduino access control system that uses the reader, a buzzer for the alarm and a relay shield for the security system. The relay can be used to open a door lock and if you need a louder alarm then you can replace the small buzzer with a high power siren.

I will not talk about the RFID reader because you can find a lot of information about it on the web, but I will talk about the Arduino sketch which is very important. So, let’s start by watching this 7 minutes video to see the entire system in action:

/** source: http://www.electroschematics.com/11301/arduino-rfid-reader-rc522-access-control-system/

  1. * Read a card using a mfrc522 reader on your SPI interface
  2. * Pin layout should be as follows (on Arduino Uno):
  3. * MOSI: Pin 11 / ICSP-4
  4. * MISO: Pin 12 / ICSP-1
  5. * SCK: Pin 13 / ISCP-3
  6. * SS: Pin 10
  7. * RST: Pin 9
  8. */
  9.  Arduino RFID Reader RC522 + Access Control System
  10. #include <SPI.h>
  11. #include <RFID.h>
  12. #define SS_PIN 10
  13. #define RST_PIN 9
  14. RFID rfid(SS_PIN,RST_PIN);
  15. int startAlarm = false;
  16. int resetAlarm = 2;
  17. int relay = 7;
  18. int alarm = 8;
  19. int serNum[5];
  20. int cards[][5] = {
  21. {209,128,106,69,126}, // card 1
  22. {101,220,213,229,137} // card 2
  23. };
  24. bool access = false;
  25. void setup(){
  26. Serial.begin(9600);
  27. SPI.begin();
  28. rfid.init();
  29. pinMode(resetAlarm, INPUT);
  30. pinMode(relay, OUTPUT);
  31. pinMode(alarm, OUTPUT);
  32. digitalWrite(relay, HIGH); // or LOW if you have a regular relay
  33. attachInterrupt(0, reset_alarm, LOW);
  34. }
  35. void loop(){
  36. if(rfid.isCard()){
  37. if(rfid.readCardSerial()){
  38. Serial.print(rfid.serNum[0]);
  39. Serial.print(“,”);
  40. Serial.print(rfid.serNum[1]);
  41. Serial.print(“,”);
  42. Serial.print(rfid.serNum[2]);
  43. Serial.print(“,”);
  44. Serial.print(rfid.serNum[3]);
  45. Serial.print(“,”);
  46. Serial.print(rfid.serNum[4]);
  47. Serial.println(“”);
  48. for(int x = 0; x < sizeof(cards); x++){
  49. for(int i = 0; i < sizeof(rfid.serNum); i++ ){
  50. if(rfid.serNum[i] != cards[x][i]) {
  51. access = false;
  52. break;
  53. } else {
  54. access = true;
  55. }
  56. }
  57. if(access) break;
  58. }
  59. }
  60. if(access){
  61. Serial.println(“Welcome!”);
  62. startAlarm = false;
  63. digitalWrite(relay, LOW); // HIGH with regular relay
  64. } else {
  65. Serial.println(“Not allowed!”);
  66. startAlarm = true;
  67. digitalWrite(relay, HIGH); // LOW with regular relay
  68. }
  69. }
  70. if(startAlarm) {
  71. digitalWrite(alarm, HIGH);
  72. } else {
  73. digitalWrite(alarm, LOW);
  74. }
  75. rfid.halt();
  76. }
  77. void reset_alarm(){
  78. startAlarm = false;
  79. }
Arduino RFID Reader RC522 + Access Control System SchematicFirst off all extract the RFID library and move the folder inside the Libraries folder where you’ve installed Arduino software. Now connect the RC522 reader to the board as you can see on the first lines in the sketch:
  • MOSI -> pin 11
  • MISO -> pin 12
  • SCK -> pin 13
  • SS (or SDA) -> pin 10
  • RST -> pin 9
  • 3.3V -> 3.3V
  • GND -> GND

The resetAlarm integer is the Arduino pin where we connect the button that is used the reset the alarm. The relay is connected to pin 7 and the active buzzer to pin 8.

On the next lines you need to change the cards codes with the ones you have. You’ll have to read the codes in the serial monitor then decide which ones you want to use to allow access in your room (watch the video to see how I’ve made it). You can use only one card or as many as you want, but add the comma after each array of elements.

The boolean access is set to false and is used to store the value true or false when we read the tags (or cards). If you go to line 63 you can see a for loop where we check to see if the values we stores in the cards array are the same as the ones we read from our RFID reader and if are the same then we set access variable to true.

Now, on line 76 if access is true we print “Welcome” in the console, set startAlarm to false so it doesn’t turn on, and activate the relay. If is false then we setAlarm to true and let the relay in its initial state.

On line 87 we check to see if the startAlarm is set to true and if so then we turn on the buzzer. If is false then do nothing.

We also have a resetAlarm button that is used to turn off the buzzer and the relay will not be activated. We might as well use a good RFID tag to stop it but now the relay will be activated.

This project is very simple and can be a good starting point to develop more complicated applications where you use the RC522 RFID reader. Stay tuned because I will try to make some new and interesting systems based on this reader.

 

For more detail: Arduino RFID Reader RC522 + Access Control System


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top