[vc_row][vc_column][vc_video link="https://youtu.be/VfAOwO1o2fI" title="DIY RFID Smart Lock with Arduino"][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]The MFRC522 reader can read and write data to RC522 chips, and stores this data in the <a href="https://www.makeuseof.com/tag/use-arduino-eeprom-save-data-power-cycles/">Arduino’s EEPROM</a>. This would be a great addition to any DIY setup, and is especially relevant to any basic home security system. It could be used alongside <a href="https://www.makeuseof.com/tag/how-to-make-a-simple-arduino-alarm-system/">DIY alarm systems </a>or DIY <a href="https://www.makeuseof.com/tag/diy-pan-and-tilt-network-security-cam-raspberry-pi/">security camera</a> systems.
<h2><span style="font-size: 14pt;">You Will Need ( you can get from Incotel)</span></h2>
<ul>
 	<li>Arduino. We’ve used an Uno, though any Arduino board or clone will suffice.</li>
 	<li>3 x 220 ohm resistors</li>
 	<li>1 x 10k ohm resistor</li>
 	<li>Logic-level N channel Mosfet</li>
 	<li>MFRC522 module with at least two cards/fobs to read. Most come with one of each, and can be bought for  cheap price, but you probably already have one in your wallet in the form of a public travel card.</li>
 	<li>Red, blue, and green LEDs</li>
 	<li>12v Solenoid ($5)</li>
 	<li>12v power supply</li>
 	<li>Breadboard and hook up wires</li>
</ul>
<h2>The MFRC522 Module</h2>
The star of this setup is a cheap MFRC522 module, which came with both a card and fob containing an <strong>s50</strong> chip, each storing its own unique permanent identification number (UID). These are both functionally identical, just in a different shape.

<img class="aligncenter lazy-load" src="https://static.makeuseof.com/wp-content/uploads/2017/03/module-and-card_670.jpg" alt="module and card" width="670" height="488" data-src="//static.makeuseof.com/wp-content/uploads/2017/03/module-and-card_670.jpg" data-loaded="true" />
<p class="linked-out">Begin by searching for the <strong>MFRC522</strong> library in the library manager of your Arduino IDE, and install it. Alternatively you can <a href="https://github.com/miguelbalboa/rfid" target="_blank" rel="noopener">download the library</a> and install it manually to the libraries folder. If you are totally new to Arduino you might find this <a href="https://www.makeuseof.com/tag/getting-started-with-arduino-a-beginners-guide/">primer for getting started</a> useful!</p>
<span class="link-callout visible"><span class="link-callout-image"><span class="link-callout-image-container"><a href="https://www.makeuseof.com/tag/getting-started-with-arduino-a-beginners-guide/">Getting Started With Arduino: A Beginner's Guide</a></span></span> <span class="link-callout-info"><span class="link-callout-title"><a href="https://www.makeuseof.com/tag/getting-started-with-arduino-a-beginners-guide/">Getting Started With Arduino: A Beginner's Guide </a></span><span class="link-callout-excerpt">Arduino is an open-source electronics prototyping platform based on flexible, easy-to use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.</span></span></span>

The library also contains a Fritzing diagram, which I have annotated indicating how to attach the module to your Arduino.
<div id="snhb-in_content-2-0" class="aligncenter mobile-only"></div>
<figure id="attachment_636607" class="wp-caption aligncenter"><img class="aligncenter lazy-load" src="https://static.makeuseof.com/wp-content/uploads/2017/03/arduino_rfid_pinout_wiring_670.jpg" alt="arduino rfid pinout wiring" width="670" height="504" data-src="//static.makeuseof.com/wp-content/uploads/2017/03/arduino_rfid_pinout_wiring_670.jpg" data-loaded="true" /><figcaption class="wp-caption-text">Image Credit: <a href="https://github.com/miguelbalboa/rfid" target="_blank" rel="nofollow noopener">Miguel Balboa</a> via Github</figcaption></figure>
Be careful: this board works on 3.3V, not 5V, so take care to connect it to the right pin.

To test the setup, open the <strong>DumpInfo</strong> sketch from <strong>File &gt; Examples &gt; MFRC522 &gt; DumpInfo</strong> and upload it to your Arduino board. Open the serial monitor, and hold one of your RFID objects up to the reader. You should see something like this:

<img class="aligncenter lazy-load" src="https://static.makeuseof.com/wp-content/uploads/2017/03/dumpinfo_serialreadout_670.jpg" alt="dump info serial readout" width="670" height="345" data-src="//static.makeuseof.com/wp-content/uploads/2017/03/dumpinfo_serialreadout_670.jpg" data-loaded="true" />
<div id="snhb-in_content-3-0" class="aligncenter mobile-only"></div>
If you get errors in the read out saying <code>MIFARE_Read() failed: Timeout in communication</code>, or <code>PCD_Authenticate() failed: Timeout in communication</code>, don’t worry. It likely means you did not hold the tag up to the reader for long enough to read all of the data. As long as you get the card UID (which is read as soon as the tag is within range of the reader), it will work with this project. If you are not getting a reading at all, check your wiring and try again.
<h2>The Rest of the Circuit</h2>
Now that we have verified that our module works, lets add the rest of the components. Connect your components like this:

<img class="aligncenter lazy-load" src="https://static.makeuseof.com/wp-content/uploads/2017/03/full_setup_670.jpg" alt="full setup circuit fritzing" width="670" height="513" data-src="//static.makeuseof.com/wp-content/uploads/2017/03/full_setup_670.jpg" data-loaded="true" />
<ul>
 	<li>Our 12v power supply (unplugged for now) connects to the rails of our breadboard. Connect the Arduino GND pin, and MFRC522 GND pin to the ground rail.</li>
 	<li>The LEDS are connected to pins 2, 3, and 4 and to the ground rail through 220 ohm resistors.</li>
 	<li>Our MOSFET’s gate leg (left on image) connects to pin 5, and to ground through a 10k ohm resistor. The drain leg (middle) connects to the negative terminal of our 12v solenoid, and the source leg (right) connects to the ground rail.</li>
 	<li>Connect the positive terminal of the 12v Solenoid, and the VIN of the Arduino to the 12v rail on the breadboard.</li>
</ul>
With this setup, whenever we send a HIGH signal from the Arduino to the MOSFET, it will allow the current to pass to the Solenoid. There is nothing to stop you from using a higher powered or more heavy duty solenoid, though you’d need a stepdown transformer to power the Arduino from higher than 12V. Also pay close attention to the datasheet for your MOSFET to make sure you wouldn’t be overloading it.
<div id="snhb-in_content-4-0" class="aligncenter mobile-only" data-google-query-id="COf0gtav9-ACFQzW7QodhbECxg">
<div id="google_ads_iframe_/1065821/sws-hb//makeuseof.com//in_content-4_0__container__"></div>
</div>
Once it’s all put together it should look something like this:

<img class="aligncenter lazy-load" src="https://static.makeuseof.com/wp-content/uploads/2017/03/circtuit_670.jpg" alt="circuit layout" width="670" height="374" data-src="//static.makeuseof.com/wp-content/uploads/2017/03/circtuit_670.jpg" data-loaded="true" />

While it isn’t necessary, I created a little rig to simulate a door lock from scrap wood.
<h2>Modifying the Sketch</h2>
With the circuit built, it’s time to set up our Arduino Sketch. Conveniently, the MFRC522 library comes with an example sketch called <strong>Access Control</strong> that does almost exactly what we want to do. Connect your Arduino to your computer, and open <strong>File &gt; Examples &gt; MFRC522 &gt; AccessControl</strong> in the Arduino IDE.

There is a wealth of information provided both in the example sketch, and on the <a href="https://github.com/miguelbalboa/rfid" target="_blank" rel="noopener">GitHub page</a> for the library. We only need to modify a few lines. Alternatively, you can <span style="color: #ff0000;"><strong>download our modified code</strong></span> from this <a href="https://gist.github.com/anonymous/5ec489385bcf1ac6e6efa789321aee81" target="_blank" rel="noopener">GitHub Gist</a>.

Firstly, the sketch was designed for a circuit with a single RGB LED using a common anode. We won’t be using that, so simply comment out this section.
<pre class=" language-cpp"><code class=" language-cpp"><span class="token comment" spellcheck="true">//#define COMMON_ANODE</span></code></pre>
Now, match our LED pins to those defined in the sketch.
<pre class=" language-cpp"><code class=" language-cpp"><span class="token macro property">#<span class="token directive keyword">define</span> redLed 3    </span><span class="token comment" spellcheck="true">// Set Led Pins</span>
<span class="token macro property">#<span class="token directive keyword">define</span> greenLed 4</span>
<span class="token macro property">#<span class="token directive keyword">define</span> blueLed 2</span></code></pre>
We need to change the relay pin (although we are using a MOSFET in this case) to match our setup.
<pre class=" language-cpp"><code class=" language-cpp"><span class="token macro property">#<span class="token directive keyword">define</span> relay 5  </span><span class="token comment" spellcheck="true">// Set MOSFET Pin</span></code></pre>
To make it easier to change how long the lock stays open later, we will create a variable for it.
<pre class=" language-cpp"><code class=" language-cpp"><span class="token keyword">int</span> lockDelay<span class="token operator">=</span><span class="token number">10000</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// lock stays open for 10 seconds.</span>
</code></pre>
We only need to make one more change. Right at the bottom of the <strong>loop</strong> method, buried in an if statement is the method call <strong>granted(300)</strong>. We need to change this so that it uses our <strong>lockDelay</strong> variable.
<pre class=" language-cpp"><code class=" language-cpp"><span class="token function">granted</span><span class="token punctuation">(</span>lockDelay<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Open the door lock for lockDelay duration</span></code></pre>
Save the sketch under a new name, and upload it to your Arduino. When it’s finished, open the serial monitor. The first time you do this, it will ask for you to scan something to use as your master card. Hold your card up to the reader, and the UID of the card should display on the serial monitor, along with the message <strong>Everything Ready</strong>

That’s it! Your master key is all set up. Unplug your Arduino board from the computer. Your master key details will be saved in the Arduino’s EEPROM, even after the power is turned off.
<h2>Testing the Full Setup</h2>
Take one last quick look over your wiring to check everything is in place, and connect your 12v power supply. At this point, it is worth mentioning that you should be wary of the duty cycle of your solenoid. The cheap solenoid I am using for this test does not have a 100 percent duty cycle, consequently it should not be left in it’s locked position for long periods of time. To make this into a permanent setup, use a 100 percent duty cycle solenoid. Even better would be a normally closed (NC) solenoid, which remains locked when it is not powered. This also means that anyone wanting to bypass the system cannot simply unplug it!

When the circuit is powered up, the Blue LED should light up to show that the device is operational. Holding the master card over the reader puts it in admin mode should cause all three LEDs to flash. While they are flashing you can hold other cards or fobs over the reader to add or take away access rights. It will flash green for giving access, and blue for taking away. Use the master card again to exit admin mode.

Now when you hold a card or fob with access up to the reader it should flash green and open the lock. If it flashes red, access has been denied!

<img class="aligncenter size-full wp-image-636611 lazy-load" src="https://static.makeuseof.com/wp-content/uploads/2017/03/open_lock_670.gif" alt="rfid arduino open lock" width="670" height="376" data-src="//static.makeuseof.com/wp-content/uploads/2017/03/open_lock_670.gif" data-loaded="true" />

All Done!
<span style="font-size: 12pt;">article credit for : makeuseof.com</span>[/vc_column_text][/vc_column][/vc_row]