An unusual combination lock on Arduino. Arduino combination lock How to make a combination lock on arduino

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

There are several options for creating an electronic lock system. Most often they are used to lock doors and drawers, cabinets. And also for the creation of hiding places and secret safes. Therefore, you need to make a mock-up 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. For this you need a 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 resistance of the coil 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 cope with this task. We 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 on the edge and can slam shut. The latch should be a counterpart in the form of a metal box. It is inconvenient and wrong to use it without it. We'll have to put a step, at least to create the appearance of normal work.

In idle mode, the latch opens normally, that is, if there is a handle on the door, give an impulse, open the door by the handle. But if you spring it up, this method is no longer suitable. The boost converter cannot handle the load. To open the spring-loaded door you will have to use a larger battery and a more powerful converter. Either the mains power supply and hammer on the autonomy of the system. There are oversized hecks in Chinese stores. They are suitable for boxes. Power can be supplied using a relay or a transistor mosfet, 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 valve. He may also need a piece of steel spoke that acts as a connecting rod. Such a system does not need a lot of current. But it takes up more space and more cunning control logic.

There are two types of servos. Small, weak and powerful, with which you can safely push 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 we decided to install a combination lock on our door at work, because we constantly run in - we run out of the office, the door to which must be closed constantly in the absence of the inhabitants. Keys are often forgotten inside. In general, we decided that the combination lock is a great solution.

Having rummaged through Chinese flea markets and ebay, I did not 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 of communicating with microcontrollers at all.

Idea

On the door, on the outside of the door, there should be a keyboard on which the password is entered; on the inside, the rest of the structure is fixed. 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 by 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 is opened by entering a 4-digit password and pressing "#".

Components

Arduino UNO = $ 18
Arduino protoshield + breadboard = $ 6
L293D = $ 1
30pcs braid wire bundle = $ 4
2 RJ45 sockets = $ 4
2 RJ45 Plugs = $ 0.5
central locking actuator = 250 rubles.
Reed switch = torn free from the old window.
Metal latch of giant size = free
Old D-LINK hub made of 1.5mm iron = free
A power supply from the same D-LINK hub for 12 and 5v = is also free
A bunch of screws and nuts for attaching all this stuff to the body = 100 rubles.
Alarm control panel = free.

Total:$ 33.5 and 350 rubles.

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

Preparing for assembly

I would like to say a few words about the purchase of a key element of the actuator design. In a local auto shop I was offered 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 the case! I chose a device with two wires, it was powered by 12V. The 5-wire design includes limit switches to control the movement of the arm. I realized that I bought the wrong one only when I took it apart and it was too late to change it. The lever travel was too short to move the latch normally, therefore, it was necessary to modify it a little, namely to remove two rubber washers shortening the travel of the actuator lever. To do this, the body had to be cut along with a regular hacksaw, because the second washer was inside. The blue electrical tape, as always, helped us in the future when assembling it back.
To control the motor of the actuator, the L293D motor driver was used, which can withstand a peak load of up to 1200 mA, when we stopped the motor of the actuator, the peak load rose to only 600 mA.
From the control panel from the burglar alarm, contacts from the keyboard, speaker and two LEDs were removed. The remote control and the main device were supposed to be connected using twisted pair and RJ45 connectors.

Programming.

So, I have not had any experience with Arduino programming until now. I used other people's developments and articles from the arduino.cc site. Who cares about this ugly code :)

Photo and video



Arduino and actuator


Power Supply


Keyboard


Espagnolette (connected to the actuator with a metal spoke and on which a heat shrinkage is worn for beauty)

Video of the device operation process:

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

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 a unique key identifier 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 (); // initialization SPI / 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 the serial number of the label.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); // Делаем звуковой сигнал, Открытие servo.write(90); // Поворациваем серву на угол 90 градусов(Отпираем какой либо механизм: задвижку, поворациваем ключ и т.д.) delay(3000); // пауза 3 сек и механизм запирается. tone(5, 500, 500); // Делаем звуковой сигнал, Закрытие } servo.write(0); // устанавливаем серву в закрытое сосотояние }

Let's take a closer look at the sketch:

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 a servo drive.

Video:

Today's lesson on how to use an RFID reader with an Arduino to create a simple locking system, in simple words - an 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 are read or written by means of 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).

This tutorial will use an RFID tag from 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 message "Unlocked" will not appear - see the photo below.

The lock is closed

The lock is open

Details required to create this project:

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

Additional details:

  • Battery (powerbank)

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

Step 2: RFID reader RC522

Each RFID tag has a small chip (white card pictured). If you aim the 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 large coil. An RFID card like this can be read from up to 20mm away.

The same chip exists in the tags of the RFID key fob.

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,000 data. Impressive, isn't it? This feature will not be used today. Today, all that interests is the identification of a particular card by its UID. The cost of the RFID reader and these two RFID cards is around US $ 4.

Step 3: OLED display

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

This is a very good display for use with Arduino. It is 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. 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. The top of the screen can only be yellow and the bottom 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, excluding 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: connect all the details

The communication with the Arduino Uno board is very simple. First, connect power to both the reader and the display.

Be careful, the RFID reader must be connected to the 3.3V output of the Arduino Uno or it will get corrupted.

Since the display can also run at 3.3V, we connect the VCC from both modules to the positive rail of the breadboard. This bus is then connected to the 3.3V output from the Arduino Uno. Then we connect both grounds (GND) to the grounding bus of the breadboard. Then we connect the GND bus of the breadboard to the Arduino GND.

OLED Display → Arduino

SCL → Analog Pin 5

SDA → Analog Pin 4

RFID Reader → Arduino

RST → Digital Pin 9

IRQ → Not connected

MISO → Digital Pin 12

MOSI → Digital Pin 11

SCK → Digital Pin 13

SDA → Digital Pin 10

The RFID reader module uses the SPI interface to communicate with the Arduino. Therefore, we are going to use the hardware SPI pins from the Arduino UNO.

The RST pin goes to digital pin 9. The IRQ pin remains disconnected. The MISO pin goes to digital pin 12. The MOSI pin goes to digital pin 11. The SCK pin goes to digital pin 13, and finally the SDA pin goes to digital pin 10. That's it.

RFID reader connected. Now we need to connect the OLED display to the Arduino using the I2C interface. So the SCL pin on the display goes to the analog pin of Pin 5 and the SDA on the display to the analog Pin 4. If we now turn on the project and place the RFID card next to the reader, we can see that the project is working fine.

Step 5: Project Code

In order for the project code to be compiled, we need to include some libraries. First of all, we need the MFRC522 Rfid library.

To install it, go to Sketch -> Include Libraries -> Manage libraries(Library management). Find MFRC522 and install it.

We also need the Adafruit SSD1306 library and the Adafruit GFX library for mapping.

Install both libraries. The Adafruit SSD1306 library needs a little modification. Go to folder Arduino -> Libraries, open the Adafruit SSD1306 folder and edit the library Adafruit_SSD1306.h... Comment out line 70 and uncomment line 69 because the display has a resolution of 128x64.

First, we declare the value of the RFID tag that the Arduino needs to recognize. This is an array of integers:

Int code = (69,141,8,136); // UID

Then we initialize the RFID reader and display:

Rfid.PCD_Init (); display.begin (SSD1306_SWITCHCAPVCC, 0x3C);

After that, in the loop function, we check the tag on the reader every 100ms.

If there is a tag on the reader, we read its UID and print it to the display. We then compare the UID of the tag we just read with the value stored in the code variable. If the values ​​are the same, we print the UNLOCK message, otherwise we will not display this message.

If (match) (Serial.println ("\ nI know this card!"); PrintUnlockMessage ();) else (Serial.println ("\ nUnknown Card");)

Of course, you can change this code to store more than 1 UID value so that more RFID tags are recognized by the project. This is just an example.

Project code:

#include #include #include #include #define OLED_RESET 4 Adafruit_SSD1306 display (OLED_RESET); #define SS_PIN 10 #define RST_PIN 9 MFRC522 rfid (SS_PIN, RST_PIN); // Instance of the class MFRC522 :: MIFARE_Key key; int code = (69,141,8,136); // This is the stored UID int codeRead = 0; String uidString; void setup () (Serial.begin (9600); SPI.begin (); // Init SPI bus rfid.PCD_Init (); // Init MFRC522 display.begin (SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // Clear the buffer.display.clearDisplay (); display.display (); display.setTextColor (WHITE); // or BLACK); display.setTextSize (2); display.setCursor (10,0); display.print ("RFID Lock"); display.display (); ) void loop () (if (rfid.PICC_IsNewCardPresent ()) (readRFID ();) delay (100);) void readRFID () (rfid.PICC_ReadCardSerial (); Serial.print (F ("\ nPICC type:") ); MFRC522 :: PICC_Type piccType = rfid.PICC_GetType (rfid.uid.sak); Serial.println (rfid.PICC_GetTypeName (piccType)); // Check is the PICC of Classic MIFARE type! = PICMType22 && piccType! = MFRC522 :: PICC_TYPE_MIFARE_1K && piccType! = MFRC522 :: PICC_TYPE_MIFARE_4K) (Serial.println (F ("Your tag is not of type MIFARE Classic.")); return;) clearUID (); return;) clearUID (); Scanned PICC "s UID:"); printDec (rfid.uid.uidByte, rfid.uid.size); uidString = String (rfid.uid.uidByte) + "" + String (rfid.uid.uidByte) + "" + String (rfid.uid.uidByte) + "" + String (rfid.uid.uidByte); printUID (); int i = 0; boolean match = true; while (i

Step 6: the final result

As you can see from the lesson - for a little money, you can add an RFID reader to your projects. You can easily create a security system using this reader or create more interesting projects, for example, so that data from a USB disk is read only after unlocking.

I was watching The Amazing Spider-Man the other day and in one scene Peter Parker remotely opens and closes the door from his laptop. As soon as I saw this, I immediately realized that I also needed such an electronic lock on the front door.

With a little tinkering, I put together a working model of a smart lock. In this article, I will tell you how I put it together.

Step 1: List of materials





To assemble an electronic lock on Arduino, you will need the following materials:

Electronics:

  • 5V wall adapter

Components:

  • 6 screws for latch
  • cardboard
  • wires

Instruments:

  • soldering iron
  • glue gun
  • drill
  • drill
  • pilot hole drill
  • stationery knife
  • computer with Arduino IDE program

Step 2: how the lock works

The idea is that I can open or close the door without a key, and without even going to it. But this is just the basic idea, because you can also add a knock sensor so that it responds to a special knock, or you can add a voice recognition system!

The servo arm connected to the latch will close (0 °) and open (60 °) using commands received via the Bluetooth module.

Step 3: Wiring diagram


Let's first connect the servo to the Arduino board (note that although I have an Arduino Nano board, the pin layout is exactly the same on the Uno board).

  • the brown wire of the servo is ground, we connect it to ground on the Arduino
  • the red wire is a plus, we connect it to the 5V connector on the Arduino
  • orange wire - the output of the source of the servo, we connect it to the 9th pin on the Arduino

I advise you to test the servo before proceeding with the assembly. To do this, in the Arduino IDE program in the examples, select Sweep. After making sure that the servo is working, we can connect the Bluetooth module. You need to connect the rx pin of the Bluetooth module to the tx pin of the Arduino, and the tx pin of the module to the rx pin of the Arduino. But don't do it yet! Once these connections are soldered, you will no longer be able to upload any codes to the Arduino, so download all your codes first and only then solder the connections.

Here is the wiring diagram for the module and the microcontroller:

  • Rx Module - Tx Arduino Board
  • Tx module - Rx boards
  • Vcc (positive) of the module - 3.3v of Arduino board
  • Ground is connected to Ground (ground to ground)

If the explanation seems confusing to you, follow the wiring diagram provided.

Step 4: Test

Now that we have all the parts in action, let's make sure the servo can move the bolt. Before mounting the latch on the door, I put together a test piece to make sure the servo is powerful enough. At first it seemed to me that my servo was weak and I added a drop of oil to the bolt, after that it worked fine. It is very important that the mechanism glides well, otherwise you risk being trapped in your room.

Step 5: housing for electrical components



I decided to put only the controller and the Bluetooth module in the case, and leave the servo outside. To do this, on a piece of cardboard, we circle the outline of the Arduino Nano board and add 1 cm of space around the perimeter and cut it out. After that we also cut out five more sides of the case. In the front wall, you will need to cut a hole for the controller's power cord.

Dimensions of the sides of the case:

  • Bottom - 7.5x4 cm
  • Cover - 7.5x4 cm
  • Left side wall - 7.5x4 cm
  • Right side wall - 7.5x4 cm
  • Front wall - 4x4 cm (with a slot for the power cord)
  • Back wall - 4x4 cm

Step 6: Application

To control the controller, you need an Android or Windows gadget with built-in Bluetooth. I have not had the opportunity to test the operation of the application on apple devices, maybe some drivers will be needed.

I'm sure some of you have the opportunity to test this. For Android, download the Bluetooth Terminal app, for Windows, download TeraTerm. Then you need to connect the module to your smartphone, the name should be linvor, the password should be 0000 or 1234. As soon as the pairing is established, open the installed application, enter the options and select "Establish connection (insecure)". Your smartphone is now an Arduino serial monitor, meaning you can communicate with the controller.

If you enter 0, the door will close, and the message “Door closed” will be displayed on the smartphone screen.
If you enter 1, you will see the door open and the message "Door open" will be displayed on the screen.
On Windows, the process is the same, except that you need to install the TeraTerm application.

Step 7: mount the bolt


First you need to connect the servo to the latch. To do this, cut off the plugs from the mounting holes of the drive housing. If we put the servo, the mounting holes should be flush with the latch. Then you need to place the servo lever in the latch slot, where the latch handle was. Check how the lock moves in the case. If everything is ok, secure the servo arm with glue.

Now you need to drill pilot holes for the screws in the door. To do this, attach the latch to the door and mark the screw holes on the door leaf with a pencil. Drill approximately 2.5 cm deep screw holes in the marked places. Attach the latch and secure with the screws. Check the servo again.

Step 8: Power


To complete the device, you will need a power supply, a cord, and a mini usb plug to connect to the Arduino.
Connect the ground pin of the power supply to the ground pin of the mini usb port, connect the red wire to the red wire of the mini usb port, then run the wire from the lock to the door hinge and from there to the outlet.

Step 9: Code

#include Servo myservo; int pos = 0; int state; int flag = 0; void setup () (myservo.attach (9); Serial.begin (9600); myservo.write (60); delay (1000);) void loop () (if (Serial.available ()> 0) (state = Serial.read (); flag = 0;) // if the state is "0" the DC motor will turn off if (state == "0") (myservo.write (8); delay (1000); Serial. println ("Door Locked");) else if (state == "1") (myservo.write (55); delay (1000); Serial.println ("Door UnLocked");))

Step 10: Finished Arduino Lock

Enjoy your remote control lock and don't forget to "accidentally" lock your friends in the room.