DIY Smart Lock with Arduino and RFID

Ian Buckley 03-04-2017

You walk up to an invisible lock that only you have the key for, and open it without even touching it. Sounds cool? Today we are going to build a simple RFID-based smart lock using an Arduino as the backbone and a few cheap components.


The technology behind this project is already used in many industries. Anyone who has worked in a modern office or used public transport will probably have used it every day. RFID (radio-frequency identification) identifies data stored on a chip in a card or dongle, and compares them to a list of tags that have already been scanned.

We will build a test circuit to control access using a Mifare MFRC522 reader module, to open and close a lock. We’ll use a master keycard to add or remove access to different tags, and create a simple LED readout to tell us what is happening in the system.

Finally, we will add a solenoid to act as the lock, and a MOSFET to safely trigger it on and off using our Arduino.

The MFRC522 reader can read and write data to RC522 chips, and stores this data in the Arduino’s EEPROM How to Use Arduino EEPROM to Save Data Between Power Cycles Did you know the Arduino can store data when it's turned off? I'm talking about the EEPROM, so join me as I show you how to read and write to it. Read More . 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 DIY alarm systems How To Make a Simple Arduino Alarm System Detect movement, then scare the heck out of an intruder with a high pitched alarm sounds and flashing lights. Does that sound fun? Of course it does. That's the goal of today's Arduino project, suitable... Read More or DIY security camera DIY Pan and Tilt Network Security Cam with Raspberry Pi Learn how to make remotely viewable pan and tilt security camera with a Raspberry Pi. This project can be completed in a morning with only the simplest of parts. Read More systems.

You Will Need

The MFRC522 Module

The star of this setup is a cheap MFRC522 module, which came with both a card and fob containing an s50 chip, each storing its own unique permanent identification number (UID). These are both functionally identical, just in a different shape.


module and card

Begin by searching for the MFRC522 library in the library manager of your Arduino IDE, and install it. Alternatively you can download the library and install it manually to the libraries folder. If you are totally new to Arduino you might find this primer for getting started Getting Started With Arduino: A Beginner's Guide 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. Read More useful!

The library also contains a Fritzing diagram, which I have annotated indicating how to attach the module to your Arduino.

arduino rfid pinout wiring
Image Credit: Miguel Balboa via Github


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 DumpInfo sketch from File > Examples > MFRC522 > DumpInfo 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:

dump info serial readout

If you get errors in the read out saying MIFARE_Read() failed: Timeout in communication, or PCD_Authenticate() failed: Timeout in communication, 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.


The Rest of the Circuit

Now that we have verified that our module works, lets add the rest of the components. Connect your components like this:

full setup circuit fritzing

  • 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.
  • The LEDS are connected to pins 2, 3, and 4 and to the ground rail through 220 ohm resistors.
  • 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.
  • Connect the positive terminal of the 12v Solenoid, and the VIN of the Arduino to the 12v rail on the breadboard.

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.

Once it’s all put together it should look something like this:


circuit layout

While it isn’t necessary, I created a little rig to simulate a door lock from scrap wood.

Modifying the Sketch

With the circuit built, it’s time to set up our Arduino Sketch. Conveniently, the MFRC522 library comes with an example sketch called Access Control that does almost exactly what we want to do. Connect your Arduino to your computer, and open File > Examples > MFRC522 > AccessControl in the Arduino IDE.

There is a wealth of information provided both in the example sketch, and on the GitHub page for the library. We only need to modify a few lines. Alternatively, you can download our modified code from this GitHub Gist.

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.

//#define COMMON_ANODE

Now, match our LED pins to those defined in the sketch.

#define redLed 3    // Set Led Pins
#define greenLed 4
#define blueLed 2

We need to change the relay pin (although we are using a MOSFET in this case) to match our setup.

#define relay 5  // Set MOSFET Pin

To make it easier to change how long the lock stays open later, we will create a variable for it.

int lockDelay=10000; // lock stays open for 10 seconds.

We only need to make one more change. Right at the bottom of the loop method, buried in an if statement is the method call granted(300). We need to change this so that it uses our lockDelay variable.

granted(lockDelay); // Open the door lock for lockDelay duration

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 Everything Ready

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.

Testing the Full Setup

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!

rfid arduino open lock

All Done!

While this project is a simple start in using RFID devices in your DIY setup, it is not the most secure setup. We don’t suggest jury rigging this to your front door.

You could build the whole mechanism into a box, and use the solenoid to lock it. Fill the box with cookies, and use your master key to decide who has access and who doesn’t. Become the cookie master!

You could do away with the solenoid altogether and attach an LED strip in it’s place, and have a RFID activated light. You could use the same ideas to display data like a Wi-Fi password to a small screen when a card or fob with access is held up to the reader.

Have you been using RFID in your set ups at home? Let us know about your projects in the comments section below!

Image Credit: Annmarie Young via

Related topics: Arduino, Home Security, Smart Locks.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

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

  1. Yousaint
    June 29, 2019 at 11:25 pm

    Do we have the possibility to check which UID have access and which not.
    Can we print such a list or update such a list by excel file or any other file format, to update the UID with and without access.
    Thanks in advance for the help.

    Best regards

  2. person
    May 7, 2019 at 2:37 pm

    what is a communication failure and how do we fix it?

  3. alberto ioli
    January 19, 2019 at 7:17 am

    good morning and congratulations, I would need to keep a register on excel file of who and when enter and go out from that door, is it possible?

  4. jamie sworobuk
    November 13, 2018 at 2:55 am

    Hey I understand the instructions I just wanted to know how to set up the twelve volt power supply. Can I buy it or do I have to jerry rig it?

  5. gulam
    November 6, 2018 at 3:59 pm

    Sir I had got project to make a RFID lock using Arduino I have done everything which in your but then my project is not working
    My solenoid lock is always on .

    • Dug
      January 14, 2019 at 4:15 am

      Yeah, he forgot to mention in the "modification" instructions, that the relay pin needs set to "LOW" throughout the code while in the locked position. The original code used the relayPin that's set to "HIGH" while in the locked position. Basically for a relay, the pin is set to high while in the locked position, and when using a mosfet or transistor in place of a relay, the pin is set to low for the locked position.

      • Oli
        February 11, 2019 at 12:17 pm

        Hi guys,

        Do you guys have a link to the mosfet he used? I've been looking and don't know if it's:

        STP55NF06L MOSFET Logic N 60V 55A
        STP36NF06L MOSFET Logic N 60V 30A
        STP40NF10L MOSFET Logic N 100V 40A

        Do you have a list of vendor links for the parts I'm having a hard time finding them all :((

        Thanks, Oli

  6. Christoph Suter
    October 15, 2018 at 1:07 pm

    I like to use it on the Garage door/HomeDoor. You mentioned it is not secured enough.
    How can we add this Security. I googled some stuff but did not find how to increase security. I have read thet a simple ID can be cloned.

    Please add a howto to get this really scured. So we can actually make use of this project.

  7. william
    August 25, 2018 at 9:17 pm

    i wanted to know why my led arent working even if i test them individualy there work great and there are well conected to the breadboard ... i have no clue what is the problem?

  8. Jeph
    March 26, 2018 at 4:57 pm

    Assumes a basic knowledge of "Arduino", "MOFSET", etc. A link to a site or sites to bring a novice up-to-speed on these matters would be helpful--if not complete.

  9. hazim osman
    February 6, 2018 at 3:59 pm

    what kind of mosfet do you use? can u tell me what model is it?

    • Pedro Santos
      June 30, 2018 at 12:08 pm

      Também gostava de saber qual o modelo
      que tipo de mosfet você usa? você pode me dizer que modelo é?


    • noah
      September 14, 2018 at 12:56 am


  10. Eduardo
    September 18, 2017 at 11:49 am

    If I want to use it with more security, what would recommend me?
    And is there a way to keep the "intelligence" in another component without using the Arduino hardware? Like, I want to use the Arduino board to another project and I don't have another one to dedicated to the door.

    Thank you very much

    • ETTAOUDI Omar
      December 20, 2017 at 4:37 pm

      Good work. Thank you for your efforts. I am a newbie. I liked your project. I needed help on how to add a serial number to master keycard
      Thank you so much