Making an RFID lock using Arduino. Do-it-yourself code lock on Arduino

Combination lock DIY on Arduino


background

It just so happened that we decided to install a combination lock on our door at work, because, as we constantly run in, we run out of the office, the door to which must be constantly closed in the absence of inhabitants. Keys are often forgotten inside. In general, we decided that a combination lock is a great way out.

After rummaging through Chinese flea markets and ebay, I didn’t find anything cheap and more or less serious and decided to make it myself. I will make a reservation right away that the Arduino platform was chosen for its simplicity, since there was no experience at all with microcontrollers.

On the door, on the outside of the door, there must be a keypad on which the password is entered, with inside fixed the rest of the structure. A reed switch is used to control the complete closing of the door. Leaving the office, a person presses “*” on the keyboard and, without waiting for the door to close, the door closer goes about his business, when the door is completely closed, the reed switch will close and the lock will be closed. The door is opened by entering a 4-digit password and pressing "#".

Accessories

Arduino UNO = $18
Arduino protoshield + breadboard = $6
L293D = $1
Bundle of wires 30pcs for bredboard = $4
2 RJ45 sockets = $4
2 RJ45 plugs = $0.5
central lock actuator = 250 rubles.
Reed switch = torn free of charge from the old window.
Espagnolette metal gigantic sizes = free
Case from an old D-LINK hub made of 1.5 mm iron = free
Power supply from the same D-LINK hub for 12 and 5v = also free
A bunch of screws and nuts for attaching all this stuff to the case = 100 rubles.
Control panel from burglar alarm= free.

Total: $33.5 and 350 rubles.

Not so little, you say, and you will definitely be right, but you have to pay for pleasure! And it's always nice to collect something with your own hands. In addition, the design can be greatly reduced in price if you use a bare MK without an Arduino.

Assembly preparation

I would like to say a few words about the purchase of a key element of the actuator design. At a local auto shop, they offered me two types of actuators: "with two wires and with five." According to the saleswoman, they were exactly the same and the difference in the number of wires meant absolutely nothing. However, as it turned out later, this is not so! I chose a device with two wires, it was powered by 12v. The five-wire design features limit switches to control the movement of the lever. I realized that I bought the wrong one only when I took it apart and it was too late to change it. The stroke of the lever turned out to be too short to properly push the latch, therefore, it was necessary to modify it a little, namely, to remove two rubber washers shortening the stroke of the actuator lever. To do this, the body had to be sawn along with a regular hacksaw, because the second washer was inside. The blue duct tape to us, as always, helped us later on when assembling it back.

To control the actuator motor, the L293D motor driver was used, which can withstand a peak load of up to 1200 mA, in our case, when the actuator motor was stopped, the peak load increased to only 600 mA.

Contacts from the keyboard, speaker and two LEDs were removed from the control panel from the security alarm. The remote control and the main device were supposed to be connected using a twisted pair and RJ45 connectors

Programming.

So, as I have not had any experience with Arduino programming until now. I took advantage of other people's developments and articles from the arduino.cc website. Who cares, can look at this ugly code :)

Photo and video

The host of the youtube channel “AlexGyver” was asked to make an electronic lock with his own hands. Welcome to the video cycle about electronic locks on arduino. AT in general terms the master will explain the idea.

There are several options for creating an electronic lock system. Most often used to lock doors and drawers, cabinets. And also to create hiding places and secret safes. Therefore, you need to make a layout that is convenient to work with and you can clearly and in detail show the structure of the system from the inside and outside. Therefore, I decided to make a frame with a door. This will require square bar 30 x 30. Plywood 10mm. Door hinges. Initially I wanted to make a plywood box, but I remembered that everything in the room was littered with spare parts. There is nowhere to put such a box. Therefore, a layout will be made. If someone wants to put himself an electronic lock, then looking at the layout, you can easily repeat everything.

Everything you need for a castle can be found in this Chinese store.

The goal is to develop the most efficient circuits and firmware for electronic locks. You will be able to use these results to install these systems on your doors, drawers, cabinets and hiding places.

The door is ready. Now you need to figure out how to open and close electronically. For these purposes, a powerful solenoid latch from aliexpress is suitable (link to the store above). If you apply voltage to the terminals, it will open. The coil resistance is almost 12 ohms, which means that at a voltage of 12 volts, the coil will eat about 1 ampere. Will cope with this task and lithium battery and boost module. We adjust to the appropriate voltage. Although maybe a little more. The latch is attached to the inside of the door at a distance so that it does not catch the edge and can slam shut. The heck should be a counterpart in the form of a metal box. Using it without it is inconvenient and wrong. We'll have to put a step, at least created the appearance of normal operation.

In idle mode, the latch opens normally, that is, if there is a handle on the door, we give an impulse, we open the door by the handle. But if you spring it, this method is no longer suitable. The boost converter can't handle the load. To open the spring-loaded door, you will have to use larger batteries and a more powerful converter. Or a mains power supply and score on the autonomy of the system. There are hecks in Chinese stores large sizes. They fit in boxes. Power can be supplied using a relay or mosfet transistor, or a power switch on the same transistor. A more interesting and less expensive option is a servo connected to a connecting rod with any locking element - a latch or a more serious valve. It may also need a piece of steel knitting needle that acts as a connecting rod. Such a system does not need a large current. But it takes up more space and more cunning control logic.

There are two types of servos. Small weak and large powerful ones, which can be easily pushed into holes in serious metal pins. Both options shown work on both doors and drawers. You will have to tinker with the box, making a hole in the sliding wall.

Second part

It just so happened that at work we decided to install a combination lock on our door, because, as we constantly run in, we run out of the office, the door to which must be constantly closed in the absence of inhabitants. Keys are often forgotten inside. In general, we decided that a combination lock is a great way out.

After rummaging through Chinese flea markets and ebay, I didn’t find anything cheap and more or less serious and decided to make it myself. I will make a reservation right away that the Arduino platform was chosen for its simplicity, since there was no experience at all with microcontrollers.

Idea

On the door, on the outside of the door, there should be a keypad on which the password is entered, and the rest of the structure is fixed on the inside. A reed switch is used to control the complete closing of the door. Leaving the office, a person presses “*” on the keyboard and, without waiting for the door to close, the door closer goes about his business, when the door is completely closed, the reed switch will close and the lock will be closed. The door is opened by entering a 4-digit password and pressing "#".

Accessories

Arduino UNO = $18
Arduino protoshield + breadboard = $6
L293D = $1
Bundle of wires 30pcs for bredboard = $4
2 RJ45 sockets = $4
2 RJ45 plugs = $0.5
central lock actuator = 250 rubles.
Reed switch = torn free of charge from the old window.
Espagnolette metal gigantic sizes = free
Case from an old D-LINK hub made of 1.5 mm iron = free
Power supply from the same D-LINK hub for 12 and 5v = also free
A bunch of screws and nuts for attaching all this stuff to the case = 100 rubles.
The control panel from the security alarm system = free of charge.

Total:$33.5 and 350 rubles.

Not so little, you say, and you will definitely be right, but you have to pay for pleasure! And it's always nice to collect something with your own hands. In addition, the design can be greatly reduced in price if you use a bare MK without an Arduino.

Assembly preparation

I would like to say a few words about the purchase of a key element of the actuator design. At a local auto shop, they offered me two types of actuators: "with two wires and with five." According to the saleswoman, they were exactly the same and the difference in the number of wires meant absolutely nothing. However, as it turned out later, this is not so! I chose a device with two wires, it was powered by 12v. The five-wire design features limit switches to control the movement of the lever. I realized that I bought the wrong one only when I took it apart and it was too late to change it. The stroke of the lever turned out to be too short to properly push the latch, therefore, it was necessary to modify it a little, namely, to remove two rubber washers shortening the stroke of the actuator lever. To do this, the body had to be sawn along with an ordinary hacksaw, because the second washer was inside. The blue duct tape to us, as always, helped us later on when assembling it back.
To control the actuator motor, the L293D motor driver was used, which can withstand a peak load of up to 1200 mA, in our case, when the actuator motor was stopped, the peak load increased to only 600 mA.
Contacts from the keyboard, speaker and two LEDs were removed from the control panel from the security alarm. The remote control and the main device were supposed to be connected using a twisted pair and RJ45 connectors

Programming.

So, as I have not had any experience with Arduino programming until now. I took advantage of other people's developments and articles from the arduino.cc website. Who cares, can look at this ugly code :)

Photo and video



Arduino and actuator


Power Supply


Keyboard


Espagnolette (connected to the actuator with a metal needle and on which heat shrink is put on for beauty)

Video of the device operation process:

Progress does not stand still and "Smart locks" are increasingly appearing on the doors of apartments, garages and houses.

A similar lock opens when you press a button on your smartphone. Fortunately, smartphones and tablets have already entered our everyday life. In some cases, "smart locks" are connected to "cloud services" like Google Drive and opened remotely. In addition, this option makes it possible to give access to the opening of the door to other people.

In this project, a DIY version of a smart lock on Arduino will be implemented, which can be controlled remotely from anywhere in the world.

In addition, the project has added the ability to open the lock after fingerprint recognition. For this, a fingerprint sensor will be integrated. Both options for opening doors will work based on the Adafruit IO platform.

A lock like this can be a great first step in your Smart Home project.

Setting up the fingerprint sensor

To work with the fingerprint sensor, there is an excellent library for Arduino, which greatly simplifies the process of setting up the sensor. This project uses Arduino Uno. The Adafruit CC3000 board is used to connect to the internet.

Let's start by connecting the power:

  • Connect the 5V pin from the Arduino board to the red power rail;
  • The GND pin from the Arduino connects to the blue rail on the solderless circuit board.

Let's move on to connecting the fingerprint sensor:

  • Connect the power first. To do this, the red wire is connected to the +5 V rail, and the black wire to the GND rail;
  • The white sensor wire connects to pin 4 on the Arduino.
  • Green wire goes to pin 3 on the microcontroller.

Now let's take a look at the CC3000 module:

  • Connect the IRQ pin from the CC3000 board to pin 2 on the Arduino.
  • VBAT - to pin 5.
  • CS - to pin 10.
  • After that, you need to connect the SPI pins to the Arduino: MOSI, MISO and CLK - to pins 11, 12 and 13, respectively.

And finally, you need to provide power: Vin - to Arduino 5V (red rail on your circuit board), and GND to GND (blue rail on the breadboard).

A photo of the fully assembled project is shown below:

Before developing a sketch that will upload data to Adafruit IO, you need to pass your fingerprint data to the sensor. Otherwise, he will not recognize you in the future;). We recommend calibrating the fingerprint sensor using the Arduino separately. If you are working with this sensor for the first time, we recommend that you familiarize yourself with the calibration process and detailed instructions for working with the fingerprint sensor.

If you haven't done so already, then sign up for an Adafruit IO account.

After that, we can move on to the next stage of developing a "smart lock" on Arduino: namely, the development of a sketch that will transmit data to Adafruit IO. Since the program is quite voluminous, in the article we will highlight and consider only its main parts, and then we will give a link to GitHub, where you can download the full sketch.

The sketch begins by loading all the necessary libraries:

#include

#include

#include

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_CC3000.h"

#include

#include >

After that, you need to slightly correct the sketch by inserting the parameters of your WiFi network, specifying the SSID and password (password):

#define WLAN_SECURITY WLAN_SEC_WPA2>

In addition, you must enter a name and AIO key (key) to enter your Adafruit IO account:

#define AIO_SERVERPORT 1883

#define AIO_USERNAME "adafruit_io_name"

#define AIO_KEY "adafruit_io_key">

The following lines are responsible for the interaction and processing of data from the fingerprint sensor. If the sensor was activated (fingerprint matched), it will be "1":

const char FINGERPRINT_FEED PROGMEM = AIO_USERNAME "/feeds/fingerprint";

Adafruit_MQTT_Publish fingerprint = Adafruit_MQTT_Publish(&mqtt, FINGERPRINT_FEED);

In addition, we need to create an instance of the SoftwareSerial object for our sensor:

SoftwareSerial mySerial(3, 4);

After that, we can create an object for our sensor:

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

Inside the sketch, we specify which fingerID should activate the lock in the future. In this example, 0 is used, which corresponds to the ID of the first fingerprint used by the sensor:

int fingerID = 0;

After that, we initialize the counter and the delay (delay) in our project. Basically, we want the lock to automatically work after opening. This example uses a delay of 10 seconds, but you can adjust this value to suit your needs:

int activationCounter = 0;

int lastActivation = 0;

int activationTime = 10 * 1000;

In the body of the setup() function, we initialize the fingerprint sensor and ensure that the CC3000 chip is connected to your WiFi network.

In the body of the loop() function, we connect to Adafruit IO. The following line is responsible for this:

After connecting to the Adafruit IO platform, we check the last fingerprint. If it matches and the lock is not enabled, we send a "1" to Adafruit IO for processing:

if (fingerprintID == fingerID && lockState == false) (

Serial.println(F("Access granted!"));

lockState=true;

Serial.println(F("Failed"));

Serial.println(F("OK!"));

lastActivation = millis();

If, within the loop() function, the lock is activated and we have reached the delay value indicated above, we send "0":

if ((activationCounter - lastActivation > activationTime) && lockState == true) (

lockState=false;

if (! fingerprint.publish(state)) (

Serial.println(F("Failed"));

Serial.println(F("OK!"));

You can download the latest version of the code on GitHub.

It's time to test our project! Don't forget to download and install all required Arduino libraries!

Make sure you've made all the necessary changes to the sketch and upload it to your Arduino. Then open the serial monitor window.

When the Arduino connects to WiFi networks, the fingerprint sensor will flash red. Put your finger on the sensor. The serial monitor window should display the ID number. If it matches, a message will appear, "OK!". This means that the data has been sent to the Adafruit IO servers.

Scheme and sketch for further setting up the lock using the example of an LED

Now let's deal with the part of the project that is directly responsible for managing door lock. To connect to a wireless network and activate / deactivate the lock, you will need an additional Adafruit ESP8266 module (ESP8266 module does not have to be from Adafruit). The example below will show you how easy it is to communicate between two platforms (Arduino and ESP8266) using Adafruit IO.

In this section, we will not work directly with the lock. Instead, we will simply connect the LED to the pin where the lock will be connected later on. This will make it possible to test our code without delving into the specifics of the lock design.

The circuit is quite simple: first install the ESP8266 on the breadboard. Then install the LED. Don't forget that the long (positive) leg of the LED is connected through a resistor. The second leg of the resistor is connected to pin 5 on the ESP8266 module. The second (cathode) LED is connected to the GND pin on the ESP8266.

Fully assembled circuit shown in the photo below.


Now let's take a look at the sketch we're using for this project. Again, the code is quite large and complex, so we will only cover the main parts of it:

We start by including the necessary libraries:

#include

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

Configure WiFi settings:

#define WLAN_SSID "your_wifi_ssid"

#define WLAN_PASS "your_wifi_password"

#define WLAN_SECURITY WLAN_SEC_WPA2

We also configure the Adafruit IO parameters. Same as in the previous section:

#define AIO_SERVER "io.adafruit.com"

#define AIO_SERVERPORT 1883

#define AIO_USERNAME "adafruit_io_username"

#define AIO_KEY "adafruit_io_key"

We indicate to which pin we connected the LED (in the future it will be our lock or relay):

int relayPin = 5;

Interaction with the fingerprint sensor, as in the previous section:

const char LOCK_FEED PROGMEM = AIO_USERNAME "/feeds/lock";

Adafruit_MQTT_Subscribe lock = Adafruit_MQTT_Subscribe(&mqtt, LOCK_FEED);

In the body of the setup() function, we indicate that the pin to which the LED is connected should work in OUTPUT mode:

pinMode(relayPin, OUTPUT);

Within the loop() loop, we first check if we have connected to Adafruit IO:

After that, we check what signal is coming. If "1" is transmitted, we activate the pin we declared earlier, to which our LED is connected. If we received "0", we put the contact in the "low" state:

Adafruit_MQTT_Subscribe *subscription;

while ((subscription = mqtt.readSubscription(1000))) (

if (subscription == &lock) (

Serial.print(F("Got: "));

Serial.println((char *)lock.lastread);

// Save the command as string data

String command = String((char *)lock.lastread);

if (command == "0") (

digitalWrite(relayPin, LOW);

if (command == "1") (

digitalWrite(relayPin, HIGH);

To find latest version You can check out the sketch on GitHub.

It's time to test our project. Don't forget to download all the necessary libraries for your Arduino and check if you made the correct changes to the sketch.

A simple USB-FTDI converter can be used to program the ESP8266 chip.

Upload the sketch to the Arduino and open the serial monitor window. At this stage, we just checked if we were able to connect to Adafruit IO: we will look at the available functionality further.

Testing the project

Now let's start testing! Go to the user menu of your Adafruit IO, in the Feeds menu. Check if channels for fingerprint and lock are created or not (on the print screen below, these are the fingerprint and lock lines):


If they don't exist, you'll have to create them manually.

Now we need to ensure data exchange between the fingerprint and lock channels. The lock channel must be set to "1" when the fingerprint channel is set to "1" and vice versa.

To do this, we use a very powerful Adafruit IO tool: triggers. Triggers are essentially conditions that you can apply to configured channels. That is, they can be used to interconnect two channels.

Create a new reactive trigger from the Triggers section in Adafruit IO. This will provide the ability to exchange data between the channels of the fingerprint sensor and the lock:


Here's what it should look like when both triggers are configured:

All! Now we can really test our project! We put our finger on the sensor and see how the Arduino began to wink with the LED, which corresponds to the data transfer. After that, the LED on the ESP8266 module should start blinking. This means that he began to receive data through MQTT. The LED on the circuit board should also turn on at this point.

After the delay you set in the sketch (default is 10 seconds), the LED will turn off. Congratulations! You can control the LED with your fingerprint from anywhere in the world!

Setting up an electronic lock

We have reached the last part of the project: direct connection and control electronic lock using Arduino and fingerprint sensor. The project is not easy, you can use all the sources in the form in which they are presented above, but instead of an LED, connect a relay.

To connect the lock directly, you will need additional components: a 12 V power supply, a power jack, a transistor (in this example an IRLB8721PbF MOSFET is used, but another one, such as a TIP102 bipolar transistor, can be used. If you are using a bipolar transistor, you will need to add a resistor.

Shown below circuit diagram connecting all components to the ESP8266 module:


Note that if you are using a MOSFET transistor, you will not need a resistor between ESP8266 pin 5 and the transistor.

The fully assembled project is shown in the photo below:


Power up the ESP8266 module using the FTDI module and connect the 12V power supply to the jack. If you used the pins recommended above for connection, you won't have to change anything in the sketch.

Now you can put your finger on the sensor: the lock should work in response to your fingerprint. The video below shows the automatic smart lock project in action:

Further development of the Smart Lock project

In our project released remote control door lock with fingerprint.

Feel free to experiment, modify the sketch and harness. For example, you can replace an electronic door lock with a relay to control the power of your 3D printer, robotic arm or quadcopter...

You can develop your smart House". For example, remotely activate the irrigation system on the Arduino or turn on the light in the room ... Do not forget that you can simultaneously activate an almost unlimited number of devices using Adafruit IO.

Leave your comments, questions and share personal experience below. In the discussion, new ideas and projects are often born!

Today's tutorial is about how to use an RFID reader with Arduino to create a simple locking system, in simple words- RFID lock.

RFID (Eng. Radio Frequency IDentification, radio frequency identification) is a method of automatic identification of objects in which data stored in so-called transponders, or RFID tags, are read or written using radio signals. Any RFID system consists of a reader (reader, reader or interrogator) and a transponder (aka RFID tag, sometimes the term RFID tag is also used).

The tutorial will use an RFID tag with an Arduino. The device reads the unique identifier (UID) of each RFID tag that we place next to the reader and displays it on the OLED display. If the UID of the tag is equal to the predefined value that is stored in the Arduino memory, then we will see the message “Unlocked” on the display. If the unique identifier is not equal to the predefined value, the "Unlocked" message will not appear - see photo below.

The castle is closed

The castle is open

Details needed to create this project:

  • RFID Reader RC522
  • OLED display
  • Bread board
  • wires

Additional details:

  • Battery (power bank)

The total cost of the project components was approximately $15.

Step 2: RFID Reader RC522

Each RFID tag has a small chip (pictured) white card). If you point a flashlight at this RFID card, you can see a small chip and a coil that surrounds it. This chip does not have a battery to generate power. It receives power from the reader wirelessly using this big coil. It is possible to read an RFID card like this from up to 20mm away.

The same chip exists in RFID key fob tags.

Each RFID tag has a unique number that identifies it. This is the UID that is shown on the OLED display. With the exception of this UID, each tag can store data. This type of card can store up to 1,000 data. Impressive, isn't it? This feature will not be used today. Today, all that is of interest is the identification of a particular card by its UID. The RFID reader and these two RFID cards cost about $4.

Step 3OLED Display

The tutorial uses a 0.96" 128x64 I2C OLED monitor.

This is a very good display to use with Arduino. It's an OLED display and that means it has low power consumption. The power consumption of this display is about 10-20mA and it depends on the number of pixels.

The display has a resolution of 128 by 64 pixels and is tiny in size. There are two display options. One of them is monochrome, and the other, like the one used in the tutorial, can display two colors: yellow and blue. Top part screen can only be yellow, and Bottom part- blue.

This OLED display is very bright and has a great and very nice library that Adafruit has developed for this display. In addition to this, the display uses an I2C interface, so connecting to the Arduino is incredibly easy.

You only need to connect two wires except for Vcc and GND. If you're new to Arduino and want to use an inexpensive and simple display in your project, start here.

Step 4: Putting All the Parts Together

What else to read