Code lock with display on avr microcontroller. Combination lock on the PIC16F628A microcontroller. Video of a combination lock working

The combination lock on microcontroller collected enough simple microcontroller AVR ATtiny13. It can be used to restrict access to various storage areas, lock garage doors and house doors, as well as turn on various appliances that need to be restricted.

Principle of operation

The operation of the lock on a microcontroller is based on the alternate entry of three numbers. The size of each number can be in the range from 0 to 255. This in turn increases the level of secrecy of the combination lock compared to other locks in which each secret number has a size from 0 to 9.

If the entered sequence of these three numbers coincides with the three numbers that are entered into the memory of the microcontroller of the combination lock, then a control signal (log.1) will appear at the output (pin 3), the HL4 LED will light up for 15 seconds, signaling the correctness of the input and will work relay K1 controls the lock actuator. After 15 seconds, a log will appear at pin 4 of the microcontroller. 0 and the lock will return to its original standby state.

The operation of the combination lock is controlled by just two buttons, guided only by the LED indication. Moreover, the input secret code is carried out only by one button SB2, which is located on the outer panel of the lock. The second button SB1 is for programming, and it is located on the board itself.

Code lock control steps

  • Entering three code numbers into the non-volatile memory of the microcontroller.

Let's look at this specific example. Let's say we need to set the following secret code: the first digit is 8, the second digit is 12, the third digit is 9. To do this, we supply power to the device, then press and hold both buttons (SB1 and SB2). After this, release the SB1 button, and as soon as the HL1 LED starts blinking, release the SB2 button. After these manipulations, LED HL1 will be lit constantly, and LEDs HL2 and HL3 will not be lit. This LED state indicates that the device has entered programming mode.

Now, to write down the first number, we need to press and hold the SB2 button, and all three LEDs will start blinking. You need to count the required number of flashes (in our case it is 8) and release the button. After this, to confirm the correctness of the entered number, the LEDs will flash the same number of times (8 times). That's it, the first number is written down. Next, the HL2 LED lights up, reminding us that we need to write down the second number.

We do exactly the same as with recording the first number: press and hold the SB2 button and count the required number of LED flashes (in our example it is 12), release the button and check the correctness of the entry using repeated flashes. Then the HL3 LED lights up for the third number, and we repeat the same procedure for the third number (number 9).

After that, we wrote all three numbers into the microcontroller’s memory and to exit the programming mode you need to press the SB1 button.

  • Dial secret code

Let's also look at this with an example. Before this we wrote down the secret code 8-12-9. To enter, first press the SB1 button and release it immediately after the HL1 LED lights up, thereby switching our lock to code entry mode. The glow of the HL1 LED indicates that you need to enter the first digit. The procedure for entering numbers is similar to how numbers were entered during programming. That is, by pressing the SB1 button, we count the required amount, after which we release the button and observe confirmation of the dialed digit by flashing the LEDs. Then we move on to the second and third digits.

If all three digits of the secret code are entered correctly, the relay will operate and the HL4 LED will turn on for 15 seconds, the HL1, HL2, HL3 LEDs will glow in the running lights mode.

Three attempts are allowed to enter the secret code. If the code is entered incorrectly for the third time, the ability to enter is blocked for 2.5 minutes. After this time, the lock will be ready to enter the code again.

When programming the microcontroller, the following fuses should be set:

  • CKDIV8=0
  • BODLEVEL0 = 0
  • SPMEN = 0

(1.3 Mb, downloaded: 1,566)

This project will excellent option for beginners to repeat, it uses a 1602 LCD display, a 4x4 keyboard of buttons and of course the controller itself. In addition, a relay, a button and power connectors, PLS pins, a couple of transistors and little things were used. By the way, the brightness of the display in the project will be adjusted using the PWM method.

This device can be used to protect almost any object, the user must enter the correct password to gain access. The board has already been designed in a convenient way, and all that remains is to make a beautiful case for it. The password is entered using the 4x4 matrix built into the keyboard. The main LCD display module is used to display messages to the user and current information. As soon as the correct password is entered, the relay will operate. This will also be indicated by an LED installed next to the relay. To turn off the relay, you need to press the corresponding button on the keyboard.

After entering the four-digit password, you must press the "OK" button (S8). At any time, you can press the Cancel button (S12) to clear the code (for example, when entering any incorrect numbers).
The lock code can be easily changed by entering a special password “0000”, as soon as you enter this password, the device will switch to password change mode. Here you need to enter your old password to get permission and then enter New Password, everything is very simple.

The LCD backlight turns off automatically after the system has been in standby mode for a few seconds. The backlight dims very smoothly, just like in mobile phones. The display can be replaced with any similar one, with a similar controller or even a different resolution, the main thing I advise you to pay attention to is the pinout; in some models the display pinout may differ. The program for the controller is written in C++, the source code, as well as the firmware for the controller, are included. The microcontroller can be used with any index; the letter L means reduced power consumption.

File printed circuit board for the manufacture of is located below in the archive, the printed circuit board can be noticeably reduced if you use smaller buttons, or if you move the keyboard to a separate board. The number keys can be taken from an old computer or laptop keyboard.

Fuses:
HIGH=D9
LOW=E1

You can download the PCB file, source and firmware below

List of radioelements

Designation Type Denomination Quantity NoteShopMy notepad
U1 MK AVR 8-bit

ATmega8

1 ATmega8L To notepad
U2 Linear regulator

LM7805

1 To notepad
Q1, Q2 Bipolar transistor

BC548

2 To notepad
D1, D3 Rectifier diode

1N4007

2 To notepad
D2, D4 Light-emitting diode 2 To notepad
C1, C3-C5 Capacitor0.1 µF4 To notepad
C2 Electrolytic capacitor470 µF1 To notepad
R1 Resistor

200 Ohm

1 To notepad
R2, R4, R6 Resistor

4.7 kOhm

3 To notepad
R3, R5 Resistor

330 Ohm

2 To notepad
RV1 Trimmer resistor10 kOhm1 To notepad
LCD1 LCD display16x2 LCD1 To notepad
RL1 RelayRSV1 To notepad
S1-S16 Button 16 To notepad
SW1 Switch 1 To notepad
L1 Inductor10 µH1

The code call circuit is implemented on the ATtiny2313 microcontroller. The combination lock circuit consists of an AVR microcontroller and transistor switch, control relay.

To record the code, close the “sw” toggle switch, thereby switching the lock to code recording mode. Enter the dimension of the code combination using buttons from 1 to 7 (buttons 8, 9 and 0 are not used in the dimension set), enter any code combination equal to the code dimension.

The email will work. the lock magnet, opening it, thereby signaling that the code combination has been recorded in the “EEPROM” memory.
Work mode. We turn off the “sw” toggle switch and put the lock into the mode of checking the recorded code combination. We repeat the sequence for the recording mode, enter the size, enter the recorded code.

Schematic diagram of a combination lock on the AVR microcontroller:

The code dialing always starts with book. 1 _ 7 (dimension). When dialing a code combination, the code can be entered not only one digit at a time, but also, for example, by pressing the button. 7, without releasing, press the button. 8 then press the button. 6 and release one button in any sequence, as a result a five-digit code combination will be dialed.

If, after entering the correct combination, the lock does not open, you need to press the button several times. 8 - 9 or 0, maximum 7 times, or press these buttons at the same time twice and repeat the code. This may indirectly mean that an attempt was made to open the lock.

When flashing the microcontroller firmware, the fuse bits must be set as follows:

The assembled combination lock looks like this:


It was in the evening when an persistent, oversized woman appeared on the threshold of the office, offering to buy dishes from a famous brand. The very next day I received an order from my boss (aka) to protect him creative nature from attacks from sales representatives. This is how the idea of ​​creating a project codenamed Hungry_Wall came about. Of course, now there are many services involved in controlling access to premises. But it’s much more interesting to make an electronic lock with your own hands, especially for me, a novice programmer and electronics engineer.

As they say, the main thing is to correctly draw up the technical specifications, i.e. what we want to get as a result.

  1. Create a key recognition system.
  2. Compare the key with the database, and if the code of the presented key matches one of those recorded in the database, open the lock.
  3. Take readings from the magnetic sensor to identify the door's condition, and if the door is open, close the lock.
  4. Use a timer after which the lock closes if we change our minds about entering/exiting. This is designed to prevent “enemies” from getting into the secret lair by taking advantage of our change of mood.
  5. Ensure the door opens using a button located inside the room.
  6. Recording a new key in the database after presenting the master key and, naturally, recording the master himself.
  7. Removing a key from the database (feature).
  8. Display system for greater attractiveness.

Half the work is done, all that remains is to implement the plan in hardware and software. To do this you need:

  1. Electric lock
  2. Proxy (em-Marin) card reader "CP-Z" from IronLogic
  3. Keys or cards for recording in the database
  4. Button
  5. Power supply 12 V
  6. Body (so that everything is neat and beautiful)
  7. Electronics – microcontroller ATmega 8, “cribX28”, stabilizer KR1158EN5V, transistor IRLU 024 N, 6 KLEM 2 connectors, 1 WF 3 connector (COM-port), capacitor, LEDs and resistors to taste.

The board layout is shown in Diagram 1.

The board's internals are shown in Figure 1.

To solve the assigned problems, all used devices are divided into logical blocks presented in Diagram 2.

The lock block includes an electric lock itself, a timer TimeOpen, which allows you to set the maximum time the lock is open, and a magnetic sensor that indicates the opening and closing of the door. The input of the lock block is the command to open the lock (Open), coming from the oarlock and button blocks. The oarlock block consists of a reading device, a database, and a TimeMaster timer, which sets the maximum time for submitting a new key for writing to the database. Entrance to the block is carried out by presenting a key or a master. The button block consists of a button that can take 2 states (pressed/not pressed).

To read contactless cards, the “CP-Z Proxy Card Reader (em-Marin)” from IronLogic is used; its peculiarity is that it emulates an iButton (1-wire) if you bring a proxy card to it.. this allows you to simplify programming the lock. However, it should Please note that this version of the reader has its own underwater rake.

The operating principle is extremely simple. When you present the key, its code is read and compared with the base. If the key is found in the database, the lock receives the Open command. Here it is necessary to take into account the peculiarity of the lock: opening must be done with clicks (open-close-open). This provides protection against jamming of the lock. When the master is presented, the logic of the program changes. Its presence does not in any way affect the “mood” of the castle. He is seen as a Turkish Sultan, ready to register (write to EEPROM) the next wife (key). Those. when the key is presented (if it has not been previously recorded), its code is written to the EEPROM. Here it is necessary to take into account that the memory of the mikruha is non-rubber, and, for example, for ATmega 8 it is 512 bytes, which allows you to record a maximum of 255 keys (if you use 2 bytes to store 1 key, as in our case). The very first key presented is recorded as the master. Pressing the button also sends the Open command to the lock. The display system makes our project more colorful and informative. If the red diode is on, the passage is blocked; if it is green, you can go! When the master is brought up, both LEDs light up.

It is important to note that the lock opens when a logical unit (i.e. voltage) is applied to it, and is in a closed state if voltage is not applied. This allows you to block the passage if you forget to pay public utilities, and your electricity is cut off.

The appearance of the entire device is shown in Figure 2. Everything is quite neat and beautiful.

You can download the sources

The device is designed to protect premises, cabinets and safes from unauthorized opening. All settings and code are stored in the non-volatile memory of the microcontroller. The device is based on a microcontroller PIC16F628A(DD1 in the diagram in Fig. 1). After power is applied, the microcontroller program configures its ports and also turns off the reference voltage source, comparison capture PWM module, timers, comparators and hardware USART - these modules are not needed for the lock to operate. Then the keyboard polling begins.

It consists of two parts. The first - buttons SB3-SB 14 - is located outside the protected object. The second - buttons SB1, SB2 and switch SA1 - is located indoors. Buttons SB3-SB 13 of the first part of the keyboard are combined into a matrix. The SB 14 button is not included in the matrix; it is intended to restart the microcontroller in the event of any failure in the program, as well as in a number of other cases, which will be discussed below.

Button SB1 "Open" is installed indoors near the door. By pressing it you can open the door from the inside without entering a code. SB2 - program restart button; buttons SB2 and SB 14 are connected in parallel. The matrix buttons are assigned the following designations: SB3 - "1", SB4 - "4", SB5 - "7", SB6 - "Open", SB7 - "2", SB8 - "5", SB9 - "8", SB 10 - "0", SB11 - "3", SB 12 - "6", SB 13 - "9". Toggle switch SA1 selects the lock closing mode. The code is entered by alternately briefly pressing the number buttons. To confirm the press, a short tone will sound from the piezo emitter HA1, controlled by transistor VT2.

Before opening the door, enter a four-digit code with pauses between adjacent presses of no more than 3 s, and then within 3 s you must briefly press the SB6 button. After 2 s, DD1 microcontroller output RA0 will be set to high level, transistor VT1 will open and an electromagnet will operate, which will move the lock bolt, compressing its spring, and the door will open. If the pause between adjacent presses exceeds 3 s, a signal will sound with decreasing frequency. This means that the program has started to run again and the code must be entered all over again. Diode VD1 is designed to protect transistor VT1 from a surge in the self-induction voltage of the electromagnet winding Y1. Before the electromagnet is triggered, a signal of the same frequency as when pressing the numeric keys, but of longer duration, will sound, which signals that the door is opening.

When the SA1 switch contacts are open, the electromagnet will close the lock after a certain time (default - 12 s). This time is set when programming the microcontroller. In the program that will need to be loaded into the controller, in the field for working with EEPROM, in the cell with address 0x06 (seventh in a row), you need to insert a number from 0x01 to OxFF, at the rate of 1 unit = 2.5 s. The minimum possible pause is 2.5 s, the maximum is 10 minutes.

If the contacts of the switch SA1 are closed, i.e., DD1 is installed at the RA4 input of the microcontroller low level, then the lock is closed after pressing button SB 14 or SB2. After transistor VT1 closes, the electromagnet will be de-energized and the lock spring will push the bolt back - the door will be locked again.

To open the door from inside the room, press the SB1 button and hold it until the electromagnet is activated, which is indicated by a tone signal lasting 2 seconds. You can open the door from the inside at any time. If the door does not open, you must press the SB2 button (restart the program) and press the SB1 button again.

When it is necessary to change the code, first enter the old one in the same way as when opening the door, but then press the SB6 button not briefly, but hold it until three tones sound. Then you must immediately release the SB6 button, enter a new four-digit code and immediately press the SB6 button again to confirm the entry. Next, a signal will sound with increasing frequency, which will notify that the new code has been accepted. It is stored in the first four cells of the non-volatile memory of the DD1 microcontroller.

The device is equipped with a locking system. Each time an incorrect code is entered, the lock will emit two beeps at 1000 Hz and one at 500 Hz. The controller considers it an error to press the SB6 button while there is an incorrect code in the working registers and to enter five digits of the code. After three errors in a row, microcontroller DD1 will set output RA2 to a high level. This will open transistor VT3, which will turn on the alarm device. This device can be a siren or a telephone dialer.

At the same time, the HL1 LED installed on the keyboard panel will turn on, which will show that the keyboard polling (except for the SA1 toggle switch and the SB1, SB2, SB 14 buttons) is disabled. This is followed by a ten-minute pause, during which the alarm device operates and the HL1 LED is turned on. During this time, the lock can only be opened from the inside. If you press the SB 14 and SB2 buttons (microcontroller program restart buttons), the ten-minute countdown will begin again. After the pause, the controller will provide only one opportunity to enter the code, and if it is incorrect, the ten-minute pause with the activation of the alarm device will be repeated again. And this will continue until the correct code is entered. Each time the code is entered correctly, the error counter is reset to zero.

class="eliadunit">

The device is powered by a DC source with a voltage of 10... 15 V. When the power is cut off in the 220 V network, the lock continues to operate from the battery. The diagram of the simplest version of such a power supply is shown in Fig. 2. Transformer T1 steps down mains voltage 220 V to 15...20 V. The maximum current of the secondary winding of the transformer should not be less than 1.5 A DA1 - adjustable voltage stabilizer. By changing the resistance of the construction resistor R2, a voltage is set at the output of the stabilizer DA1 at which the charging current of the charged battery GB1 does not exceed 100...200 μA. Moreover, during high current consumption, when electromagnet Y1 is triggered, the main part of the current comes from accumulator battery, which allows you not to overload the DA1 stabilizer. The VD5 diode is designed to protect the DA1 stabilizer in the event of a lack of voltage at its input.

The battery must provide a current of 300...600 mA (capacity - 7 Ah). Stabilizer DA1 should be installed on a heat sink with an area of ​​30...40 cm 2. The keyboard can be made from individual buttons. Suitable, for example, DIPTRONICS DTSMW-66N. But you can also use a ready-made keyboard from a push-button telephone or calculator. As a rule, you can easily connect such a keyboard to a device by assembling the buttons in a suitable matrix. It is also necessary to place the HL1 LED on the keyboard panel.

The piezo emitter is suitable for any of the ZP series. Electromagnet Y1 is used from the tape drive mechanism of the tape recorder, but any other one is suitable, suitable in size and with a maximum winding current of no more than 1.3 A. If the current consumed by the electromagnet is more than 1 A, then transistor VT1 should be installed on a heat sink with an area of ​​30.. .40 cm 2 .



What else to read