Unusual combination lock on Arduino. Combination lock from arduino 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 video cycle about electronic locks on arduino. In general terms, the master will explain the idea.

There are several options for creating an electronic lock system. Most often used to lock doors and drawers, cabinets. And also to create hiding places and secret safes. Therefore, you need to make a layout that is convenient to work with and you can clearly and in detail show the structure of the system from the inside and outside. Therefore, I decided to make a frame with a door. To do 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 coil resistance is almost 12 ohms, which means that at a voltage of 12 volts, the coil will eat about 1 ampere. A lithium battery and a step-up module will cope with this task. We adjust to the appropriate voltage. Although maybe a little more. The latch is attached to the inside of the door at a distance so that it does not catch the edge and can slam shut. The heck should be a counterpart in the form of a metal box. Using it without it is inconvenient and wrong. We'll have to put a step, at least created the appearance of normal operation.

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

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

Second part

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

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

Idea

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

Accessories

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

Total:$33.5 and 350 rubles.

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

Assembly preparation

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

Programming.

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

Photo and video



Arduino and actuator


Power Supply


Keyboard


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

Video of the device operation process:

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

In the future, you can refine and expand the functionality. For example, add the function "adding new keys and removing them from memory". In the base case, consider a simple example, when a unique key identifier is pre-specified 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 the Buzzer, which will give a signal if the key worked and the lock opens, and the second signal when the lock closes.

We connect the buzzer in the following sequence:

Arduino Buzzer
5V VCC
GND GND
pin 5 IO

3) A servo will be used as the unlocking mechanism. Any servo can be chosen, depending on the dimensions you require and the forces that 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 tag 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 servo to closed state ) void loop() ( // Search for a new label if (! mfrc522.PICC_IsNewCardPresent()) ( return; ) // Selecting a tag if (! mfrc522.PICC_ReadCardSerial()) ( return; ) uidDec = 0; // Issuing the serial number of the tag 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 analyze the sketch in more detail:

In order to find out the UID of the card (Labels), you need to write this sketch to arduino, assemble the circuit described above, and open the Console (Monitoring of the serial port). 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 specified one, then the servo opens the valve.

For each card, this identifier is unique and does not repeat. Thus, when you present a card whose ID you have set in the program, the system will open access using a servo.

Video:

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

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

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

The castle is closed

The castle is open

Details needed to create this project:

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

Additional details:

  • Battery (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 in the photo). If you point a flashlight at this RFID card, you can see a small chip and a coil that surrounds it. This chip does not have a battery to generate power. It receives power from the reader wirelessly using this large coil. It is possible to read an RFID card like this from up to 20mm away.

The same chip exists in RFID key fob tags.

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

Step 3OLED Display

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

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

The display has a resolution of 128 by 64 pixels and is tiny in size. There are two display options. One of them is monochrome, and the other, like the one used in the tutorial, can display two colors: yellow and blue. 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 except for Vcc and GND. If you're new to Arduino and want to use an inexpensive and simple display in your project, start here.

Step 4: Putting All the Parts Together

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

Be careful, the RFID reader must be connected to the 3.3V output from the Arduino Uno or it will be damaged.

Since the display can also run at 3.3V, we connect the VCC from both modules to the positive breadboard rail. This rail is then connected to the 3.3V output from the Arduino Uno. Then we connect both grounds (GND) to the breadboard ground bus. We then 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. So we are going to use the Arduino UNO hardware SPI pins.

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 analog Pin 5 and the SDA on the display to 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(Management of libraries). Find MFRC522 and install it.

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

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 should recognize. This is an array of integers:

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

We then 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 the reader has a tag, we read its UID and print it on 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 display 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 the project recognizes more RFID tags. 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 if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 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(); Serial.println(" 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: Final Result

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

I was rewatching 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 needed such an electronic lock on the front door.

After a little fiddling, I assembled a working model of a smart lock. In this article I will tell you how I collected it.

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 the latch
  • cardboard
  • wires

Instruments:

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

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 that's just the basic idea, because you can add a knock sensor to react to a special knock, or you can add a voice recognition system!

A servo lever connected to the bolt will close (0°) and open (60°) the bolt using commands received via the Bluetooth module.

Step 3: Wiring Diagram


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

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

I advise you to test the servo before proceeding with the assembly. To do this, in the Arduino IDE program, select Sweep in the examples. 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 just yet! Once these connections are soldered, you won't be able to upload any codes to the Arduino, so upload all your codes first and only then solder the connections.

Here is the connection diagram of the module and the microcontroller:

  • Rx module - Tx Arduino board
  • Tx module - Rx board
  • Vcc (positive terminal) of the module - 3.3v of the Arduino board
  • Ground connect to Ground (ground to ground)

If the explanation is not clear to you, follow the wiring diagram provided.

Step 4: Test

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

Step 5: Enclosure 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, trace 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 body. In the front wall, you will need to cut a hole for the controller power cord.

Dimensions of the sides of the case:

  • Bottom - 7.5x4 cm
  • Lid - 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 did not have the opportunity to test the application on apple devices, maybe some drivers will be needed.

I'm sure some of you have the opportunity to check it out. 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. Once the pairing is established, open the installed application, enter the options and select "Establish connection (insecure)". Your smartphone is now the Arduino serial monitor, meaning you can communicate with the controller.

If you enter 0, the door will close and the smartphone screen will display the message "Door closed".
If you enter 1, you will see the door open and the message "Door open" will appear 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 a servo, the mounting holes should be flush with the latch. Then you need to place the servo lever in the slot of the latch, where the latch handle was. Check how the lock moves in the case. If everything is OK, fix the servo lever with glue.

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

Step 8: Nutrition


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 terminal of the power supply to the ground terminal 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 socket.

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.