Electronic lock on Arduino. DIY code 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. For 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. A lithium battery and a boost module can handle 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

In this article I will tell you how to make a combination lock from Arduino. For this we need red and green LEDs, a buzzer, an Arduino nano, an LCD display with an I2C converter, a servo drive and a 4x4 matrix keyboard. When turned on, the display will write "Enter code."

the red LED will turn on,

and the green light will go out, the servo will be set to 0°. As you enter numbers, * will light up on the display.

If the code is entered incorrectly, the display will write “Enter code.”. If the code is correct, a beep will sound, the servo will rotate 180°, and the display will read "Open."

the green LED will turn on,

and the red one will turn off. After 3 seconds, the servo will return to its initial position, the red LED will turn on and the green LED will go out, the display will write “Close.”,

then the display will write "Enter code.". Now about the scheme. First we connect the Arduino with wires to development board(power contacts).

Then we connect the matrix keyboard to contacts D9 - D2.

Then the servo. We connect it to pin 10.

Red LED to pin 11.

Green - to pin 12.

Buzzer - to pin 13.

Now upload the sketch.

#include #include #include #include iarduino_KB KB(9, 8, 7, 6, 5, 4, 3, 2); LiquidCrystal_I2C lcd(0x27, 16, 2); Servo servo; int pass = (3, 6, 1, 8); int in; int r = 11; int g = 12; void setup() ( KB.begin(KB1); pinMode(r, OUTPUT); pinMode(g, OUTPUT); lcd.init(); lcd.backlight(); digitalWrite(g, LOW); digitalWrite(r, HIGH ); servo.attach(10); lcd.setCursor(0, 0); void loop() (lcd.clear(); lcd.print("Enter code."); while ( !KB.check(KEY_DOWN)) ( delay(1); ) in = KB.getNum; lcd.setCursor(0, 0); check(KEY_DOWN)) ( delay(1); ) in = KB.getNum; lcd.print("*"); while (!KB.check(KEY_DOWN)) ( delay(1); ) in = KB.getNum; lcd.print("*"); while (!KB.check(KEY_DOWN)) ( delay(1); ) in = KB.getNum("*"); if (in == pass) ( if (in == pass) ( if (in == pass) ( if (in == pass) ( lcd.clear(); lcd.setCursor(0, 0); lcd.print("Open."); tone( 13, 400, 750); digitalWrite(r, LOW); delay(3000); lcd.setCursor(0, 0); print("Close."); tone(13, 300, 700); digitalWrite(g, LOW); delay(1000);

) ) ) ) )

That's all. Enjoy the combination lock!

List of radioelements Designation Type Denomination QuantityNoteShop
My notepad E1

Arduino board

1 Arduino Nano 3.0 5V
To notepad E8, E9

Resistor

2 220 Ohm 5V
SMD E6

Light-emitting diode

1 AL102G 5V
Red E6

E7

1 AL307G 5V
Green E3LCD display1 With I2C interface 5V
Green backlight E5Servo1 SG90 5V
180 degrees E2Arduino Nano 3.01 Buzzer 5V
Bu E4Keyboard1 4X4 5V
Matrix NoBreadBoard1 640 points

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

- RFID lock.

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

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 scheme is quite simple: first install the ESP8266 on the 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 download 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 (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, robotic arm or quadcopter...

You can develop your smart House". For example, remotely activate an irrigation system on Arduino or turn on the lights in a room... Don't forget that you can simultaneously activate an almost 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!