{"id":14374,"date":"2016-03-23T13:20:23","date_gmt":"2016-03-23T13:20:23","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=14374"},"modified":"2023-04-23T11:08:11","modified_gmt":"2023-04-23T11:08:11","slug":"security-access-using-mfrc522-rfid-reader-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/security-access-using-mfrc522-rfid-reader-with-arduino\/","title":{"rendered":"Security Access using MFRC522 RFID Reader with Arduino"},"content":{"rendered":"<p>This blog post shows&nbsp;a simple example on how to use the MFRC522 RFID reader. I&#8217;ll do a quick overview of the specifications and demonstrate a project example using an Arduino.<!--more--><\/p>\n<h2>Description<\/h2>\n<p>RFID means radio-frequency identification. RFID uses electromagnetic fields to transfer data over short distances. RFID is useful to identify people, to make transactions, etc\u2026<\/p>\n<p>You can use an RFID system to open a door. For example, only the person with the right information on his card is allowed to enter. An RFID system uses:<\/p>\n<ul>\n<li><strong>tags<\/strong> attached to the object to be identified, in this example we have a keychain and an electromagnetic card. Each tag has his own identification (UID).<\/li>\n<\/ul>\n<p><a href=\"http:\/\/ebay.to\/1YwzoUB\" target=\"_blank\" rel=\"noopener noreferrer\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-14379 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/tags.png?resize=387%2C126&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"tags\" width=\"387\" height=\"126\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/tags.png?w=387&amp;quality=100&amp;strip=all&amp;ssl=1 387w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/tags.png?resize=300%2C98&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 387px) 100vw, 387px\" \/><\/a><\/p>\n<ul>\n<li>two-way radio transmitter-receiver, the<strong> reader, <\/strong>that send a signal to the tag and read its response.<\/li>\n<\/ul>\n<h2><a href=\"http:\/\/ebay.to\/1YwzoUB\" target=\"_blank\" rel=\"noopener noreferrer\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-14380 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/reader.png?resize=214%2C157&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"reader\" width=\"214\" height=\"157\"><\/a>Specifications<\/h2>\n<ul>\n<li>Input voltage: 3.3V<\/li>\n<li>Price: approximately 3$ (<a href=\"https:\/\/makeradvisor.com\/tools\/mfrc522-rfid-reader\/\" target=\"_blank\" rel=\"noopener noreferrer\">check best price on Maker Advisor<\/a>)<\/li>\n<li>Frequency: 13.56MHz<\/li>\n<\/ul>\n<h2>Library download<\/h2>\n<p>Here\u2019s the library you need for this project:<\/p>\n<ol>\n<li>Download the <a href=\"https:\/\/github.com\/miguelbalboa\/rfid\/archive\/master.zip\" target=\"_blank\" rel=\"noopener noreferrer\">RFID library here<\/a>&nbsp;created by miguelbalboa<\/li>\n<li>Unzip the RFID library<\/li>\n<li>Install the RFID library in your Arduino IDE<\/li>\n<li>Restart your Arduino IDE<\/li>\n<\/ol>\n<h2>Pin wiring<\/h2>\n<table>\n<tbody>\n<tr>\n<td width=\"188\"><strong>Pin<\/strong><\/td>\n<td width=\"175\"><strong>Wiring to Arduino Uno<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"188\">SDA<\/td>\n<td width=\"175\">Digital 10<\/td>\n<\/tr>\n<tr>\n<td width=\"188\">SCK<\/td>\n<td width=\"175\">Digital 13<\/td>\n<\/tr>\n<tr>\n<td width=\"188\">MOSI<\/td>\n<td width=\"175\">Digital 11<\/td>\n<\/tr>\n<tr>\n<td width=\"188\">MISO<\/td>\n<td width=\"175\">Digital 12<\/td>\n<\/tr>\n<tr>\n<td width=\"188\">IRQ<\/td>\n<td width=\"175\">unconnected<\/td>\n<\/tr>\n<tr>\n<td width=\"188\">GND<\/td>\n<td width=\"175\">GND<\/td>\n<\/tr>\n<tr>\n<td width=\"188\">RST<\/td>\n<td width=\"175\">Digital 9<\/td>\n<\/tr>\n<tr>\n<td width=\"188\">3.3V<\/td>\n<td width=\"175\">3.3V<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Caution:<\/strong> You must power this device to 3.3V!<\/p>\n<h2>Circuit<\/h2>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/Mifare_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-14382 aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/Mifare_bb.png?resize=595%2C339&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Mifare_bb\" width=\"595\" height=\"339\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/Mifare_bb.png?w=1584&amp;quality=100&amp;strip=all&amp;ssl=1 1584w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/Mifare_bb.png?resize=300%2C171&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/Mifare_bb.png?resize=1024%2C584&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/a><\/p>\n<h3><strong>Reading Data from a RFID tag<\/strong><\/h3>\n<p>After having the circuit ready, go to File &gt; Examples &gt; MFRC522 &gt; DumpInfo and upload the code. This code will be available in your Arduino IDE (after installing the RFID library).<\/p>\n<p>Then, open the serial monitor. You should see something like the figure below:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-14383 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor1.png?resize=557%2C323&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"serial monitor1\" width=\"557\" height=\"323\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor1.png?w=557&amp;quality=100&amp;strip=all&amp;ssl=1 557w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor1.png?resize=300%2C174&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 557px) 100vw, 557px\" \/><\/p>\n<p>Approximate the RFID card or the keychain to the reader. Let the reader and the tag closer until all the information is displayed.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-14384 aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor2.png?resize=577%2C514&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"serial monitor2\" width=\"577\" height=\"514\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor2.png?w=577&amp;quality=100&amp;strip=all&amp;ssl=1 577w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor2.png?resize=300%2C267&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><\/p>\n<p>This is the information that you can read from the card, including the card UID that is highlighted in yellow. The information is stored in the memory that is divided into segments and blocks as you can see in the previous picture.<\/p>\n<p>You have 1024 bytes of data storage divided into 16 sectors and each sector is protected by two different keys, A and B.<\/p>\n<p><strong>Write down your UID<\/strong> <strong>card<\/strong> because you\u2019ll need it later.<\/p>\n<p>Upload the following code.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * \n * All the resources for this project: http:\/\/randomnerdtutorials.com\/\n * Modified by Rui Santos\n * \n * Created by FILIPEFLOP\n * \n *\/\n \n#include &lt;SPI.h&gt;\n#include &lt;MFRC522.h&gt;\n \n#define SS_PIN 10\n#define RST_PIN 9\nMFRC522 mfrc522(SS_PIN, RST_PIN);   \/\/ Create MFRC522 instance.\n \nvoid setup() \n{\n  Serial.begin(9600);   \/\/ Initiate a serial communication\n  SPI.begin();      \/\/ Initiate  SPI bus\n  mfrc522.PCD_Init();   \/\/ Initiate MFRC522\n  Serial.println(&quot;Approximate your card to the reader...&quot;);\n  Serial.println();\n\n}\nvoid loop() \n{\n  \/\/ Look for new cards\n  if ( ! mfrc522.PICC_IsNewCardPresent()) \n  {\n    return;\n  }\n  \/\/ Select one of the cards\n  if ( ! mfrc522.PICC_ReadCardSerial()) \n  {\n    return;\n  }\n  \/\/Show UID on serial monitor\n  Serial.print(&quot;UID tag :&quot;);\n  String content= &quot;&quot;;\n  byte letter;\n  for (byte i = 0; i &lt; mfrc522.uid.size; i++) \n  {\n     Serial.print(mfrc522.uid.uidByte[i] &lt; 0x10 ? &quot; 0&quot; : &quot; &quot;);\n     Serial.print(mfrc522.uid.uidByte[i], HEX);\n     content.concat(String(mfrc522.uid.uidByte[i] &lt; 0x10 ? &quot; 0&quot; : &quot; &quot;));\n     content.concat(String(mfrc522.uid.uidByte[i], HEX));\n  }\n  Serial.println();\n  Serial.print(&quot;Message : &quot;);\n  content.toUpperCase();\n  if (content.substring(1) == &quot;BD 31 15 2B&quot;) \/\/change here the UID of the card\/cards that you want to give access\n  {\n    Serial.println(&quot;Authorized access&quot;);\n    Serial.println();\n    delay(3000);\n  }\n \n else   {\n    Serial.println(&quot; Access denied&quot;);\n    delay(3000);\n  }\n} \n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/MFRC522_RFID_Reader_with_Arduino.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<p>In the piece of code above you need to change the<em> if (content.substring(1) == &#8220;REPLACE WITH YOUR UID&#8221;)<\/em> and type the UID card you\u2019ve written previously.<\/p>\n<h2>Demonstration<\/h2>\n<p>Now, upload the code to your Arduino and open the serial monitor.<\/p>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/mpLzcBDBl1U?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Approximate the card you\u2019ve chosen to give access and you\u2019ll see:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14385 size-full aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor3.png?resize=338%2C170&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"serial monitor3\" width=\"338\" height=\"170\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor3.png?w=338&amp;quality=100&amp;strip=all&amp;ssl=1 338w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor3.png?resize=300%2C151&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 338px) 100vw, 338px\" \/><\/p>\n<p>If you approximate another tag with another UID, the denial message will show up:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-14386 aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor4.png?resize=398%2C183&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"serial monitor4\" width=\"398\" height=\"183\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor4.png?w=398&amp;quality=100&amp;strip=all&amp;ssl=1 398w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/serial-monitor4.png?resize=300%2C138&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 398px) 100vw, 398px\" \/><\/p>\n<p>I hope you found this tutorial useful.<\/p>\n<p><strong>Share this post<\/strong> with a friend that also likes electronics!<\/p>\n<p>You can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by <a href=\"https:\/\/randomnerdtutorials.com\/download\">subscribing my blog<\/a>.<\/p>\n<p>Thanks for reading,<\/p>\n<p>-Rui Santos<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post shows&nbsp;a simple example on how to use the MFRC522 RFID reader. I&#8217;ll do a quick overview of the specifications and demonstrate a project example using an Arduino.<\/p>\n","protected":false},"author":1,"featured_media":14382,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,267,2,302,269,223,264,7,10],"tags":[],"class_list":["post-14374","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino-project","category-arduino","category-0-guide","category-guide-project","category-modules","category-project","category-a-theory-and-blog","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/11\/Mifare_bb.png?fit=1584%2C903&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/14374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=14374"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/14374\/revisions"}],"predecessor-version":[{"id":130641,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/14374\/revisions\/130641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/14382"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=14374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=14374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=14374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}