Arduino code lock. Electronic lock on Arduino

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

There are several options for creating an electronic lock system. Most often used to lock doors, drawers, and cabinets. And also for creating caches and secret safes. Therefore, you need to make a layout that is convenient to work with and can clearly and in detail show the structure of the system from the inside and outside. So I decided to make a frame with a door. To do this you will need a square beam 30 x 30. Plywood 10mm. Door hinges. Initially I wanted to make a plywood box, but I remembered that the room was full of spare parts. There is nowhere to put such a box. Therefore, a mock-up will be made. If someone wants to install an electronic lock for themselves, then looking at the layout they can easily repeat everything.

You will find everything you need for a castle in this Chinese store.

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

The door is ready. Now we need to figure out how to open and close electronically. A powerful solenoid latch from aliexpress is suitable for these purposes (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 consume about 1 ampere. Both a lithium battery and a boost module can cope with this task. Adjust to the appropriate voltage. Although a little more is possible. 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 latch should have a counterpart in the form of a metal box. Using it without this is inconvenient and incorrect. We'll have to install a step, at least to create the appearance of normal operation.

In idle mode, the latch opens normally, that is, if there is a handle on the door, we apply a pulse and open the door by the handle. But if you use a spring, this method is no longer suitable. The boost converter cannot cope with the load. To open the spring-loaded door you will have to use large batteries and more powerful converter. Or network source power supply and neglect the autonomy of the system. Chinese stores have large size latches. They are suitable for drawers. 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 drive connected to a connecting rod with any locking element - a latch or a more serious bolt. You may also need a piece of steel knitting needle to act as a connecting rod. Such a system does not require high current. But it takes up more space and has more cunning control logic.

There are two types of servos. Small weak ones and large powerful ones that 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 retractable wall.

Second part

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

RFID (English 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, is read or written using radio signals. Any RFID system consists of a reading device (reader, reader or interrogator) and a transponder (also known as RFID tag, sometimes the term RFID tag is also used).

This tutorial will use an RFID tag with 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 ID is not equal to a predefined value, the "Unlocked" message will not appear - see photo below.

The castle is closed

The lock is open

Parts needed to create this project:

  • RFID reader RC522
  • OLED display
  • Bread board
  • Wires

Additional details:

  • Battery (powerbank)

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

Step 2: RFID Reader RC522

Each RFID tag contains a small chip (white card shown in photo). If you shine a flashlight on this RFID card, you can see the small chip and the coil that surrounds it. This chip does not have a battery to generate power. It receives power from the reader wirelessly using this large coil. It is possible to read an RFID card like this from up to 20mm away.

The same chip also 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. Except for this UID, each tag can store data. This type of card can store up to 1 thousand data. Impressive, isn't it? This feature will not be used today. Today, all that is of interest is identifying a specific card by its UID. The cost of the RFID reader and these two RFID cards is about $4.

Step 3: OLED Display

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

This is a very good display to use with Arduino. This is an OLED display and that means it has low power consumption. The power consumption of this display is around 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 lesson, can display two colors: yellow and blue. The top of the screen can only be yellow, and the bottom can only be 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 Vcc and GND. If you are new to Arduino and want to use an inexpensive and simple display in your project, start here.

Step 4: Connecting all the parts

Code lock DIY on Arduino


Background

It just so happened that at work we decided to install a combination lock on our door, because we constantly run in and out of the office, the door to which should be constantly closed in the absence of the occupants. Keys often end up forgotten inside. In general, we decided that a combination lock was a great solution.

Having rummaged through Chinese flea markets and ebay, I didn’t find anything cheap and more or less serious and decided to make it myself. I’ll say right away that the Arduino platform was chosen for its simplicity, since I had no experience with microcontrollers at all.

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

Accessories

Arduino UNO = $18
Arduino protoshield + breadboard = $6
L293D = $1
Wire bundle 30pcs for Bradboard = $4
2 RJ45 sockets = $4
2 RJ45 plugs = $0.5
central locking actuator = 250 rub.
Reed switch = freely torn from the old window.
Giant metal latch = free
Housing from an old D-LINK hub made of one and a half millimeter 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 body = 100 rubles.
Remote control from burglar alarm= free.

Total: $33.5 and 350 rubles.

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

Preparing for assembly

I would like to say a few words about purchasing a key element of the actuator design. A local auto store offered me two types of actuators: “with two wires and with five.” According to the saleswoman, they were absolutely identical 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 retract the latch, therefore, it was necessary to modify it a little, namely, remove two rubber washers that shortened the stroke of the actuator lever. To do this, the body had to be sawed lengthwise with an ordinary hacksaw, because the second washer was inside. Blue electrical tape, as always, helped us in the future when assembling it back.

To control the actuator motor, we used an L293D motor driver, which can withstand a peak load of up to 1200 mA; when we stopped the actuator motor, the peak load increased to only 600 mA.

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

Programming.

So, I had no experience in programming Arduino until now. I used other people's work and articles from the site arduino.cc. Anyone interested can take a look at this ugly code :)

Photo and video

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 opening the door to other people.

This project will feature a DIY version smart lock on Arduino, which can be controlled remotely from anywhere on Earth.

In addition, the project has added the ability to open the lock after identifying a fingerprint. For this purpose, a fingerprint sensor will be integrated. Both door opening options will be powered by 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 a fingerprint sensor, there is an excellent library for Arduino, which greatly simplifies the process of setting up the sensor. This project uses Arduino Uno. An Adafruit CC3000 board is used to connect to the Internet.

Let's start with 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:

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

Now let's move on to the CC3000 module:

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

Well, at the end you need to provide power: Vin - to the 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 load data onto Adafruit IO, you need to transfer data about your fingerprint to the sensor. Otherwise, he will not recognize you in the future;). We recommend calibrating the fingerprint sensor using the Arduino separately. If this is your first time working with this sensor, we recommend that you familiarize yourself with the calibration process and detailed instructions for working with the fingerprint sensor.

If you haven't already done so, please create an account with Adafruit IO.

After this, we can move on to the next stage of developing a “smart lock” on Arduino: namely, developing a sketch that will transmit data to Adafruit IO. Since the program is quite voluminous, in this article we will highlight and consider only its main parts, and then we will provide 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 this, you need to slightly correct the sketch by inserting the parameters of your WiFi network, specifying the SSID and password:

#define WLAN_SECURITY WLAN_SEC_WPA2>

In addition, you must enter your name and AIO key to log into 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 interacting and processing data from the fingerprint sensor. If the sensor was activated (the fingerprint matched), there 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 this we can create an object for our sensor:

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

Inside the sketch we indicate which fingerID should activate the lock in the future. This example uses 0, which corresponds to the ID of the first fingerprint used by the sensor:

int fingerID = 0;

After this, we initialize the counter and delay in our project. Essentially we want the lock to automatically engage once opened. 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 activated, we send "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 the necessary libraries for Arduino !

Make sure you have made all the necessary changes to the sketch and upload it to your Arduino. After that, open the Serial Monitor window.

When the Arduino connects to the WiFi network, the fingerprint sensor will start flashing red. Place your finger on the sensor. The ID number should be displayed in the serial monitor window. If it matches, the message "OK!" will appear. This means that the data has been sent to the Adafruit IO servers.

Diagram and sketch for further configuration of the lock using the example of an LED

Now let's deal with that part of the project that is directly responsible for management door lock. To connect to a wireless network and activate/deactivate the lock, you will need an additional Adafruit ESP8266 module (the ESP8266 module does not have to be from Adafruit). Using the example below, you can evaluate how easy it is to exchange data 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. This will give us the opportunity to test our code without delving into the details of the lock design.

The circuit is quite simple: first install the ESP8266 on breadboard. After this, 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. We connect the second (cathode) of the LED to the GND pin on the ESP8266.

Fully assembled circuit shown in the photo below.


Now let's look at the sketch we are using for this project. Again, the code is quite large and complex, so we will only look at its main parts:

We start by connecting the necessary libraries:

#include

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

Configuring WiFi settings:

#define WLAN_SSID "your_wifi_ssid"

#define WLAN_PASS "your_wifi_password"

#define WLAN_SECURITY WLAN_SEC_WPA2

We also configure 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 which pin we connected the LED to (in the future this 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 operate in OUTPUT mode:

pinMode(relayPin, OUTPUT);

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

After this, we check what signal is being received. If "1" is transmitted, we activate the pin that we declared earlier, to which our LED is connected. If we receive "0", we transfer the contact to 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 to string data

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

if (command == "0") (

digitalWrite(relayPin, LOW);

if (command == "1") (

digitalWrite(relayPin, HIGH);

Find latest version You can find the sketch on GitHub.

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

To program the ESP8266 chip, you can use a simple USB-FTDI converter.

Upload the sketch to the Arduino and open the Serial Monitor window. At this stage, we simply checked whether 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 your Adafruit IO's user menu, under the Feeds menu. Check whether the fingerprint and lock channels are created or not (in the print screen below these are the fingerprint and lock lines):


If they do not exist, you will have to create them manually.

Now we need to ensure data exchange between the fingerprint and lock channels. The lock channel must take the value "1" when the fingerprint channel takes the value "1" and vice versa.

For this we use very powerful tool Adafruit IO: 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 fingerprint sensor and lock channels:


This is what it should look like when both triggers are configured:

All! Now we can actually test our project! We put our finger on the sensor and see how the Arduino began to wink with an LED that corresponds to data transmission. After this, the LED on the ESP8266 module should start blinking. This means that it has started receiving data via MQTT. The LED on the circuit board should also turn on at this moment.

After the delay you set in the sketch (the 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've 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 connect a relay instead of an LED.

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

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


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

The fully assembled project is shown in the photo below:


Power 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

Released in our project remote control door lock using your fingerprint.

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

You can develop your smart House". For example, remotely activate irrigation system on Arduino or turn on the lights in the room... Don't forget that you can simultaneously activate a virtually unlimited number of devices using Adafruit IO.

Leave your comments, questions and share personal experience below. New ideas and projects are often born in discussions!