Menu
Is free
registration
home  /  ON/ Electronic lock for arduino nano. An unusual combination lock on Arduino

Electronic lock for arduino nano. An unusual combination lock on Arduino

Imagine a RF-key operated door lock.

The lock works like this: They brought OUR key (RFID-tag) - the lock closed, brought the key again - the lock opened. To visualize the operation of the lock, six two-color LEDs (ruler) are used. When closing, a red light runs through, when opening, a green one. If you bring ANYONE'S key, the red LEDs will flash.

As an actuator, I decided to use the car's central locking drive. You can buy a new one, you can use it, the difference in price is not big, so I used a new one, it is more reliable. The drive rod is connected to the latch. The espagnolette is still Soviet, strong. And I do not need a powerful "anti-vandalism".

The "machine" is controlled by two wires. One polarity extends the stem, reverse polarity retracts the stem. At 12 volts, the current is 6 amps, a lot ...
There are no limit switches in the "machine".

Proceeding from the fact that the lock circuit (as planned) has a guaranteed power supply, a 12 volt battery, to ensure the operation of the lock, in the event of a ~ 220 loss. Developed a bridge control scheme for the "machine". A special feature of the circuit is its nonlinearity, which ensures reliable operation of the lock mechanism, and at the same time - a gentle mode of operation of the "machine" and key transistors.

In the diagram (above), the "Close" shoulder is highlighted in red, and the "Open" shoulder is highlighted in green. The shoulders are powered separately, through resistors (located in the power supply). Separation of power supply of the bridge arms, introduced to eliminate false alarms.

Explanation: Through 33-ohm resistors (on the power supply diagram), a voltage of 12 volts charges the capacitors (2000 microfarads, in each arm). When the control voltage comes from the controller Arduino_ProMini- 168 to the "Close" input (or similarly to "Open"), through the PVT322 optocoupler - the corresponding key arm opens. In this case, the following happens: At the moment of opening the keys, the energy from the capacitors powerfully "pulls" the motor of the "machine". As the capacitors are discharged (this happens quickly), the motor of the "car" is powered by a current limited by resistors (33-Ohm). Due to this, at the end of the process of "closing" - "opening" the lock, the stem moves rather slowly.

This way of motor control is optimal.

Transformer power supply circuit. In general, the lock circuit is powered by a 12-volt, 2.8 -A / H battery. And the power supply circuit maintains the battery at a nominal level. The Network LED indicates normal operation of the power supply.

All diodes are 1N4007 (I forgot to indicate on the diagram, but the person asked the question - which ones?).

(1) assembled overcurrent limiter. Resistor R 1 the upper current threshold is set at 300 mA.
On the integrated stabilizer LM317 (2) assembled voltage stabilizer. The stabilization voltage is adjusted by a resistorR 2 ... The battery voltage should be 13.7 volts.

The battery voltage is supplied at three points.
Through resistors (33-Ohm each) on (X), (Y) - power supply of the arms of the "driver" keys of the "car" motor.

I collect most of my devices from what came to hand. This project is no exception. As a body I use a body :) from electronic ballast:

LEDs No.-2 ... No.-7 are two-color. They are arranged in a line. They are used to visualize the "opening" and "closing" processes of the lock. Embellishment.

This project is modular, i.e. you can connect / disconnect different elements and get different functionality. The pictures above show the option with full functionality, namely:

  • Locking mechanism... Serves for OPENING and CLOSING the door. This project covers the use of three different mechanisms:
    • Servo. There are big ones, there are small ones. Very compact and with a heavy bolt is a great option
    • Electric drive of the car door lock. A big and powerful thing, but it eats just insane currents
    • Solenoid latch. A good option as it slams itself

    In the firmware settings, you can select any of three types (setting lock_type)

  • Button inside... Serves to OPEN and CLOSE the door from the inside. Can be placed on the door handle (palm or finger side), on the door itself, or on the jamb
  • Button outside... Serves for CLOSING the door, as well as for AWAKENING from energy saving. Can be placed on the door handle (palm or finger side), on the door itself, or on the jamb
  • End stop to close the door. Serves to automatically close the lock when the door is closed. They can be:
    • Tact button
    • Hall sensor + magnet on the door itself
    • Reed switch + magnet on the door itself
  • Secret access reset button... Serves to reset the password / enter a new password / memorize a new key / combination, etc. May be hidden somewhere in the case
  • Light-emitting diode to indicate work. RGB LED, red and green colors are used (when mixed, they give yellow):
    • Lights up green - the lock is OPEN. Burns so as not to forget to close the door
    • Solid yellow - the system has woken up and is awaiting password entry
    • Blinking red - the battery is dead

Any of these elements can be excluded from the system:

  • We remove the limit switch. In the firmware in the settings, we also disable it (setting tail_button). Now to close the lock, you need to press the button
  • We remove the outer button. In the firmware in the settings, we also disable it (setting wake_button). Now the system does not need to be woken up, it wakes up by itself (energy consumption is slightly higher). And also we no longer have a close button on the front of the door, and we need a limit switch. Either the lock is the heck
  • We remove the inner button. This option is suitable for cabinets and safes. You don't need to change anything in the settings
  • We remove the LED. You don't need to change anything in the settings
  • The access reset button can be unsoldered after the first use, or you can rewrite the code for yourself
  • Door closed, pressed OUTSIDE - wake up, wait for password / RFID tag / electronic key / fingerprint
  • The door is closed, the system has woken up, waiting for the password to be entered. The time can be adjusted (setting sleep_time)
  • The door is closed, a password / tag / key has been entered, etc. - open
  • Door closed, pressed INSIDE - open
  • Door open, pressed OUT - close
  • Door open, pressed INSIDE - close
  • The door is open, the END is pressed - close

The lock provides for battery operation in low power saving mode (turn on turn off: setting sleep_enable), namely:

  • Wake up every few seconds, monitor the EVENT (optional if there is no button outside. You can enable it in the setting wake_button)
  • Every few minutes, monitor the voltage of Akum (on / off setting battery_monitor)
  • If Akum is discharged (voltage is set in the setting bat_low):
    • open the door (optional, can be configured in the firmware open_bat_low)
    • prohibit further opening and closing
    • when the buttons are pressed, blink red LED
    • stop monitoring the EVENT (i.e. enter password / tag, etc.)

When the system is awake, press the change password button (hidden button). We fall into password change mode:
Enter the password from numbers ( MAXIMUM 10 FIGURES !!!)

  • When you press *, the password is saved in memory and the system exits from changing the password
  • When you press #, the password is reset (you can enter it again)
  • If you do not press anything for 10 seconds, we will automatically exit the password change mode, the password will remain old

When the system is awake (woke up by buttons or sleep is disabled), press * to enter the password entry mode
If the system sleeps and periodically wakes up to check the EVENT, then press * and hold until the red LED lights up
Password input mode:

  • Password processing is done in such a way that the correct password is counted only when the correct sequence of numbers is typed, that is, if the password is 345, then any numbers can be entered until the sequence 345 appears, i.e. 30984570345 will open the lock as it ends in 345.
  • If the password is entered correctly, the door will open
  • If you do not press anything, after 10 seconds the system will return to normal (standby) mode
  • If you press #, we will immediately exit the password entry mode
  • If you press secret button change the password in the password input mode, then we will also exit from it

DACHA, like most who have it, associates it with the words: rest, barbecue, comfort and other pleasant body and spirit movements, but there is also back side: vegetable garden, digging, repairing, building, etc.

For 10 years my family and I have been trying to refine and create maximum comfort in our country house. We build, repair, etc. A house, a barn, a bathhouse ... ... and finally it came to a street fence, a gate and a gate. Doing so is conscientious, budget and convenience.

After discussing some details, it was decided that the gate should be automatic and that the gate should have some properties of the ACS. With the gate, the issue was resolved by purchasing a set of automation (drive, rail, remote control, etc.), and with the gate it was necessary to solve some problems, about them below.

The tasks were as follows:

  1. The castle was supposed to work in conjunction with earlier installed video intercom (open the gate without leaving the house)
  2. Be able to open the door with a regular key and without a key from the street and yard.
  3. Will keep within the remaining budget up to 5000 rubles.

Searches in Runet presented the following price range from 7000 to infinity. The purchase of a ready-made solution disappeared and an alternative with ample opportunities was conceived, namely, to cut the door yourself!

After some calculations and calculations, it was decided to buy an electromechanical lock for about 2000 rubles, a waterproof keyboard for 350 rubles, and an MK that will steer here. Since there were several Arduino nano boards, relays and loose pieces and a few wires in stock, the difference between the cost of the finished kit was more than 4000 tr. For me, a great bonus for the wallet and self-development.

Well, now, from words to action:

After buying all necessary components began to saw.

Keyboard connection diagram

Additional LED indication (white, green, red) of the panel with the keypad signals (enter, correct password open the door, refused).

  • pin 9 yellow
  • pin 10 green
  • pin 11 red

The panel (lattice) made of plexiglass, cut into a box of chocolates and a smile by the neighbors in the office. But the smallest cutter turned out to be a little fatter, I had to work with a file.

Well, here's the weekend, I moved to the dacha.

To open an electromechanical lock, you need 12 volts. The power supply supplying the MK was 5 V., the solution was to supply a step-up dc-dc converter from the sky for the lock. I started to check everything, it works, but when voltage was applied to the solenoid of the lock, the dunya rebooted, short circuit to the power supply. Further more, after connecting the outdoor panel from the video intercom to the lock, when the button was pressed to open the door, nothing happened, a small current to the lock. Pulling new wires is not an option, they were already concreted at the exit from the house. I decided to add another relay for the panel and put an additional 12 volt power supply. for the castle. After parsing / collecting, everything worked, MK stopped rebooting. I hid the whole thing in a waterproof junction box, hid the wires, glue, silicone and you're done!

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 a smartphone. Fortunately, smartphones and tablets have already entered our everyday life. In some cases, "smart locks" connect to " cloud services"like a google drive and open remotely. In addition, this option makes it possible to give access to open the door to other people.

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

In addition, the project added the ability to open the lock after fingerprint recognition. For this, 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.

Fingerprint sensor setup

For working with the fingerprint sensor, there is an excellent library for Arduino that makes the process of setting up the sensor much easier. This project uses Arduino Uno. An 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 of 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. For this, the red wire is connected to the +5 V rail, and the black wire is connected 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 tackle the CC3000 module:

  • The IRQ pin from the CC3000 board is connected 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.

Finally, 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 upload data to the Adafruit IO, you need to transfer 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 create an account at Adafruit IO.

After that, we can move on to the next stage of developing a "smart lock" on Arduino: namely, developing a sketch that will transfer 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 required 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 need to enter your name and AIO key (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 the interaction and processing of data from the fingerprint sensor. If the sensor was activated (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 that, 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 that is used by the sensor:

int fingerID = 0;

After that, we initialize the counter and delay in our project. Basically, we want the lock to work automatically 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 connect the CC3000 chip to your WiFi network.

In the body of the loop () function, connect to the 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" for processing in Adafruit IO:

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 required Arduino libraries!

Make sure you make 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 blink red. Place your finger on the sensor. The serial monitor window should display the ID number. If it matches, the message "OK!" Appears. 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 a LED

Now let's deal with the part of the project that is directly responsible for controlling the door lock. To connect to wireless network and activating / deactivating the lock you will need additional module Adafruit ESP8266 (ESP8266 does not need to be from Adafruit) Using the example below, you can appreciate how easy it is to exchange data between two platforms (Arduino and ESP8266) using the Adafruit IO.

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

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 connects to pin 5 on the ESP8266 module. The second (cathode) of the LED is connected to the GND pin on the ESP8266.

The fully assembled circuit is shown in the photo below.


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

We start by connecting the required libraries:

#include

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

Configuring WiFi parameters:

#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. The 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 is the same 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 the OUTPUT mode:

pinMode (relayPin, OUTPUT);

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

After that, we check what signal is being received. If "1" is transmitted, we activate the contact that we declared earlier, to which our LED is connected. If we get "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 sketch you can 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 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. On this stage we just checked if we were able to connect to the Adafruit IO: available functionality we will consider further.

Testing the project

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


If they are not there, then you will have to create it manually.

Now we need to ensure the exchange of data 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 of Adafruit IO. This will provide the ability to exchange data between the fingerprint sensor and lock channels:


This is how it should look when both triggers are configured:

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

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 got to the last part of the project: direct connection and control electronic lock with using Arduino and a 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 the LED, connect a relay.

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

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


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

Fully assembled project 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 connection pins recommended above, you won't have to change anything in the sketch.

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

Further development of the "Smart Lock" project

In our project we have released remote control door lock using your 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, manipulator or quadcopter ...

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

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

In this lesson, we will learn how to make a simple system that will unlock the lock by electronic key(Label).

In the future, you can modify and expand the functionality. For example, add the function "add new keys and remove them from memory". In the basic case, consider a simple example when unique identificator The key is preset in the program code.

In this tutorial we will need:

To implement the project, we need to install the libraries:

2) Now you need to connect a Buzzer, which will give a signal if the key is triggered and the lock opens, and a second signal when the lock is closed.

We connect the buzzer in the following sequence:

Arduino Buzzer
5V VCC
GND GND
pin 5 IO

3) The servo will be used as an unlocking mechanism. Any servo can be chosen, depending on the dimensions and forces required by you, which the servo creates. The servo has 3 pins:

More clearly, you can see how we connected all the modules in the picture below:

Now, if everything is connected, then you can proceed to programming.

Sketch:

#include #include #include // "RFID" library. #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522 (SS_PIN, RST_PIN); unsigned long uidDec, uidDecTemp; // to store the label number in decimal format Servo servo; void setup () (Serial.begin (9600); Serial.println ("Waiting for card ..."); SPI.begin (); // SPI initialization / Init SPI bus.mfrc522.PCD_Init (); // initialization MFRC522 / Init MFRC522 card.servo.attach (6); servo.write (0); // set the servo to closed) void loop () (// Find a new label if (! Mfrc522.PICC_IsNewCardPresent ()) (return; ) // Select a label if (! Mfrc522.PICC_ReadCardSerial ()) (return;) uidDec = 0; // Issue serial number labels. for (byte i = 0; i< mfrc522.uid.size; i++) { uidDecTemp = mfrc522.uid.uidByte[i]; uidDec = uidDec * 256 + uidDecTemp; } Serial.println("Card UID: "); Serial.println(uidDec); // Выводим UID метки в консоль. if (uidDec == 3763966293) // Сравниваем Uid метки, если он равен заданому то серва открывает. { tone(5, 200, 500); // Делаем sound signal, Opening servo.write (90); // Rotate the servo at an angle of 90 degrees (We unlock any mechanism: the latch, rotate the key, etc.) delay (3000); // pause for 3 seconds and the mechanism is locked. tone (5, 500, 500); // Make Beep, Close) servo.write (0); // set the servo closed)

Let's analyze the sketch in more detail:

In order to find out the UID of the card (Tags), you need to write this sketch to arduino, assemble the circuit described above, and open the Console (Serial Port Monitoring). When you bring the tag to the RFID, a number will be displayed in the console

The resulting UID must be entered in the following line:

If (uidDec == 3763966293) // Compare the Uid of the label, if it is equal to the given one then the servo opens the valve.

Each card has a unique identifier and does not repeat itself. Thus, when you bring the card, the identifier of which you have set in the program, the system will open access using the servo.

Video: