Combination lock on atmega8 microcontroller. Combination lock on PIC16F628A microcontroller

Scheme and program of a digital combination lock with an infrared key on a microcontroller

Good day dear radio amateurs!
I welcome you to the site ""

Good day dear radio amateurs! Today in the section “Radio amateur circuits on microcontrollers” we will consider a simple scheme - digital combination lock with IR key.

This combination lock is installed on any entrance door of a room with limited access. An IR key is used to open the door. Thanks to this, there is no need for an external keypad to enter the code, and in addition, it is very difficult to intercept the signal in the IR range. Code lock scheme:

The code lock is in standby mode until an IR signal is received by the VD1 photodiode, which is converted into electrical impulses. The key is an IR signal transmitter and is powered by a single galvanic cell. A voltage converter of 1.5 volts to 5 volts is assembled on the microcircuit. This part of the key circuit with a voltage converter can be ignored, but for this you will need to select a suitable power source for the key with a voltage of 5 volts. The key is activated by supplying voltage to switch SA1. When replacing “secret” words, it is necessary to change their codes in the program texts of both the lock and the key. The lock is powered by a 12 volt power supply. It is desirable to supplement it with a battery to power the solenoid in the absence of mains voltage.

It was in the evening when a persistent oversized woman appeared on the threshold of the office, offering to buy dishes of a famous brand. The next day, I received from the boss (aka) the task to protect his creative nature from the attacks of sales representatives. This is how the idea of ​​creating a project codenamed Hungry _ Wall was born. Of course, now there are many services that control access to the premises. But it is 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 TOR, i.e. what we want to get as a result.

  1. Create a key recognition system.
  2. Compare the key with the base, and if the code of the presented key matches one of those recorded in the base, open the lock.
  3. Take readings of the magnetic sensor, allowing you to identify the state of the door, and if the door is open, close the lock.
  4. Use a timer, after which the lock closes, in the event that we change our minds about entering / exiting. This is to ensure that the "enemies" do not get into the secret lair, taking advantage of our change of mood.
  5. Ensure the opening of the door using the button located inside the room.
  6. Recording a new key in the database after presenting the master key and, of course, recording the master himself.
  7. Removing a key from the database (feature).
  8. Display system for more attractiveness.

Half the work is done, the least is left - to implement the plan in hardware and software. For this you need:

  1. Electric lock
  2. Proxy (em-Marin) card reader "CP-Z" from IronLogic
  3. Keys or cards for entry into the database
  4. Button
  5. Power supply 12V
  6. Body (so that everything is neat and beautiful)
  7. Electronics - ATmega 8 microcontroller, “bedX28”, KR1158EN5V stabilizer, IRLU 024 N transistor, 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 insides of the board are shown in Figure 1.

To solve the tasks, all used devices are divided into logical blocks, presented in diagram 2.

The lock block includes an electric lock itself, a TimeOpen timer that allows you to set the maximum time for the lock to be open, 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 blocks of the oarlock and button. The oarlock block consists of a reader, a database, a TimeMaster timer that sets the maximum time to bring a new key to write 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 on 2 states (pressed/not pressed).

To read contactless cards, the "Proxy (em-Marin) "CP-Z" card reader from IronLogic is used; its feature is that it emulates iButton (1-wire) if you bring a proxy card to it .. this allows you to simplify the programming of the lock. however, it should take into account that this version of the reader has its own underwater rake.

The principle of operation is extremely simple. When the key is presented, its code is read and compared with the base. If the key is found in the database, the lock receives an Open command. Here it is necessary to take into account the peculiarity of the lock: the opening must be done with clicks (open-close-open). This provides protection against jamming of the lock. When the wizard is presented, the logic of the program changes. Its presence does not affect the "mood" of the castle. He is regarded as a kind of Turkish sultan, ready to register (write in EEPROM) another wife (key). Those. when a key is presented (if it has not been previously recorded), its code is written to EEPROM. Here it is necessary to take into account that the memory of mikruha is non-rubber, and, for example, for ATmega 8 it is 512 bytes, which allows you to write a maximum of 255 keys (if 2 bytes are used to store 1 key, as in our case). The very first key presented is recorded as the master. Pressing the button also sends an 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 presented, both LEDs are lit.

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

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

You can download the source

The actuator in the electronic lock, the scheme of which is shown in fig. 1, the electromechanical lock ZNEM-1-2 is used, which opens when a constant voltage of 12 V is applied to the electromechanical Y1 built into it. The logical part of the electronic lock is built on the PIC16F630-I / R microcontroller. The buttons SB1 and SB2 are designed to enter the code that opens it. LEDs HL1-HL3 of different colors of glow signal the status and mode of operation. The field-effect transistor VT1, according to the signal generated by the microcontroller at the output of the RSZ, controls the electromagnet Y1.

Rice. 1

The device is powered by a galvanic or rechargeable battery with a voltage of 12 V. This voltage is necessary for reliable operation of the electromagnet Y1. It can be supplied both from a galvanic or rechargeable battery, and from a mains power supply. The battery guarantees the ability to open the lock in the absence of mains voltage, but you will have to constantly monitor its charge.

The voltage of 5 V (required to power the microcontroller) is obtained from 12 V using the integrated regulator DA1. If you use an actuator for a different voltage or a separate power source, the voltage supplied to the input of the stabilizer can be reduced to 7 V or increased to 15 V.

The current consumed by the lock when it is closed is very small and does not exceed a few milliamps. In the process of typing the code, it increases to tens of milliamps, depending on the number of LEDs on, and when the electromagnet is triggered, it rises to approximately 1 A.

Rice. 2a. Component side view

Rice. 2b. View from the paths

The lock is assembled on a printed circuit board. The location of the elements and the drawing of printed conductors on it are shown in fig. 2. LEDs HL1-HL3 and buttons SB1, SB2 are installed separately on the frame of the locked door. To the person who opens the lock, the LEDs should be visible, and the buttons should be available for pressing. LEDs, the types of which are indicated in the diagram, have a housing diameter of 10 mm and increased brightness. However, you can apply other, suitable glow colors.

The microcontroller program was created in the "PIC Simulator IDE v6.91" environment. The opening code is a combination of eight presses in a certain order on the buttons SB1 and SB2. In the program, pressing the SB1 button is represented by a logical zero in the bit of the memory cell corresponding to the serial number of pressing, and pressing the SB2 button is represented by a logical unit in such a bit. The total number of possible combinations is 256.
Pressing any button is confirmed by turning on the HL1 LED, which allows you to visually control them. When dialing the code, do not press both buttons at the same time. This will cancel the dialing attempt and block the lock for 4 seconds. In case of too long pause between pressing the buttons or too long (more than 3 s) holding the button down, the program turns on the HL3 LED and also cancels the entry attempt, blocking the lock for 4 s.

If the code is dialed to the end, but does not match the sample stored in the microcontroller's memory, the lock is blocked for 4 s, but the HL3 LED is blinking. Three incorrect code entries will block the lock for a minute, which is accompanied by the inclusion of all three LEDs. Until the end of the blocking that occurred for any reason, pressing the buttons does not have any effect.

An exemplary code combination is stored in the EEPROM of the microcontroller at address 1. At the beginning of its work, the program reads the contents of this cell and assigns it to the code variable. Initially, the code is entered into EEPROM at the stage of programming the microcontroller. Means for this are available in the software of any programmer. For example, in the main window of the programming environment "PIC Simulator IDE v6.91" it is enough to open the menu item "Tools- * EEPROM Memory Editor" before loading the program into the microcontroller and in the window with the EEPROM image write the desired code in the cell at the above address. The contents of the memory are represented here in hexadecimal, so, for example, the code 00110011 looks like 33.

During the operation of the lock, you can change the code without reprogramming the microcontroller. To do this, turn on the lock and enter the correct code that is valid at the moment. The HL2 LED should turn on, and the lock should open. While it is open, press both buttons at the same time.

The HL2 LED will start blinking and HL3 will turn on. Release the buttons and after the HL3 LED goes out, start entering a new combination - If the requirements for the duration of pressing the buttons and pauses between them when entering are not violated, the HL2 LED will continue to blink, and the HL3 LED will turn on again - After the HL3 LED is again goes out (to do this, both buttons must be released), dial the same combination again. If it is identical to the first one, the program will accept it and write it to EEPROM.

The source text of the BASIC program attached to the article contains descriptions of all variables used and comments on the most important lines. The microcontroller's TMR0 timer is configured to overflow with a period of about 65.5 ms, each time generating an interrupt request. Processing these requests, the microcontroller determines the state of the buttons and counts the required time intervals. For example, approximately one minute lock blocking is based on a count of 1000 interruptions. Their count in this case is kept in the den_p variable. Since it is of type long and takes up four bytes of memory, it can take values ​​from 0 to 232-1 (4294967295). If, for example, you set the count result limit to 3600/0.0655^56000, the blocking time will increase to an hour.

The code lock on the microcontroller assembled on a fairly simple AVR ATtiny13 microcontroller. It can be used to restrict access to various storage areas, lock the garage door and the door of the house, as well as to turn on various devices that need to be turned on.

Principle of operation

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

In the event that the entered sequence of these three numbers matches the three numbers that are entered in 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 work relay K1 controlling the actuator of the lock. After 15 seconds, a log will appear at pin 4 of the microcontroller. 0 and the lock will return to the initial wait state.

The operation of the code lock is controlled by only two buttons, guided only by the indication of the LEDs. Moreover, the secret code is entered with only 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.

Steps to manage a code lock

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

Let's look at this with a 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 that, release the SB1 button, and as soon as the HL1 LED starts flashing, release the SB2 button. After these manipulations, the HL1 LED will be on constantly, and the HL2 and HL3 LEDs will not light up. This state of the LEDs indicates that the device has entered programming mode.

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

We proceed in exactly the same way as with writing the first number: we 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 input by 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 have written all three numbers into the memory of the microcontroller and to exit the programming mode, you need to press the SB1 button.

  • Secret code set

Let's also consider this with an example. Before that, 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 transferring our lock to the code entry mode. The glow of the HL1 LED indicates that you need to enter the first digit. The procedure for entering numbers is the same as entering numbers when programming. That is, by pressing the SB1 button, we count the required amount, after which we release the button and observe the confirmation of the dialed digit by flashing the LEDs. Then we move on to the second and third digits.

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

You have three attempts 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 again be ready to enter the code.

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

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

(1.3 Mb, downloaded: 1 566)

A code lock will help to restrict access of unauthorized persons to rooms with valuables. One of the options for implementing a code lock on the PIC16F628A microcontroller is given in this article.

The figure below shows a diagram of a code lock. The core of the circuit is the PIC16F628A microcontroller. The algorithm for executing the main commands is shown in Figure 2. The program code is written in assembly language, see the listing in the CL \ 16F628ATEMP.ASM folder of the archive with the project. The device is controlled by one button. Pressing the button achieves a sequential change in the operating modes of the device. The sound accompaniment of pressing the button is provided by a piezo sound emitter. A display with a built-in controller is used for visual display of information.

A full cycle of in-circuit programming and debugging of the PIC16F628A microcontroller was carried out using MPLAB IDE v8.15 (integrated development environment), MPASM v5.22 compiler (included in MPLAB IDE v8.15) and MPLAB ICD 2 (in-circuit debugger). For those who do not have the above tools, but have their own program for working with HEX files and another programmer, you can find the 16F628ATEMP.HEX file in the corresponding project.

The DD1 microcontroller has functional outputs RA0, RB0 - RB7, CCP1, which are used to input and output information. The DD1 microcontroller does not have a forced reset function, the reset pin is connected through a resistor R1 to the positive power potential. An on-chip RC oscillator is used to generate the clock frequency.

The clock button SB1 is connected to the output RA0 through the current-limiting resistor R3. In the depressed position of the clock button SB1, the resistor R7 simulates a low logic level. The DD1 microcontroller recognizes three states of the SB1 clock button:

  1. not pressed;
  2. Pressed briefly (less than 1 s);
  3. Pressed and held (more than 1 s).

Piezo sound emitter P1 helps to distinguish the states of the tact button SB1. So, in state 1, no sound is generated, in state 2, sound is generated until the microcontroller recognizes state 3, and in state 3, no sound is generated.

To display information, a liquid crystal display HG1 is used. The technical specification of the display can be found on the website. It has a controller that implements the character generation function. Displays two lines of sixteen characters each. The display is controlled through the microcontroller pins RB0, RB1, RB4 - RB7. Data loading occurs in nibbles, through pins RB4 - RB7. "Latch" - RB1. The choice of the signal register is formed at the output RB0. Resistors R5 and R6 set the contrast of the display HG1. The backlight of the display is connected to the power supply through the current-limiting resistor R4. The HG1 display is screwed to the board with 3 x 15 mm brass standoffs and 3 x 6 mm screws.

By forming logic on RB2, they achieve the opening or closing of the field effect transistor VT1, which turns on and off the electric lock connected to the X1 terminal block. The electric lock must be designed for an operating voltage of 9-15 V and consume a current of no more than 1 A. When voltage is applied to the electric lock, it must open, in the absence of voltage it blocks (closes).

To the output of CCP1 (hardware implementation of PWM, frequency 4 kHz, duty cycle 2) through the current-limiting resistor R2, a piezo sound emitter P1 with an operating frequency of sound generation of 4 kHz is connected.

The device is powered by an AC or DC voltage source connected to connector X2. The rated voltage of the power supply is 9 - 15 V. The rated current of the power supply is 1 A. To stabilize the power supply, a conventional circuit is used from a diode bridge VD1, a linear stabilizer DA1, filter capacitors C1 - C4.

The device can be operated in the temperature range from -20 °С to +70 °С.
The microcontroller is programmed in such a way that it has eleven operating states.

  1. When the device is turned on, the non-volatile data memory EEPROM is read, where the data of the lock status and code are uploaded. The device opens or closes the electric lock according to the read status register of the lock. The device goes to the state where it displays the encoding statistics, i.e. 2.
  2. In this state, the device in the upper line displays the inscription “Stat. Stat." and on the bottom line displays encoding statistics, namely the number of encodings and the number of decodings*. After a short or long press of the tact button, the device, guided by the lock state register, enters the coding state if the lock is open, i.e. 3 and enters the decoding state if the lock is closed, i.e. 4.
  3. The device displays the inscription “Code Code” in the upper line of the display and switches to the state where the code is entered (subprogram “Code Entry”), i.e. 5. Increment the counter of the number of encodings. The device goes into a state where it changes the state of the lock, closing it, i.e. 9.
  4. The device displays the inscription “Decode D.code” in the upper line of the display and goes to the state where the code is entered (subprogram “Code Entry”), i.e. 5. Increment the counter of the number of decodes. The device compares the entered code with the code stored in non-volatile EEPROM memory. If the code matches, then the device goes into a state where it changes the state of the lock, opening it, i.e. 10, and if the code does not match, it goes to the state where it displays information about the error, i.e. eleven.
  5. The first digit of the four-digit code is highlighted in square brackets on the bottom line of the display. By briefly pressing the clock button, the register of the entered digit is incremented**. If the tact button is pressed and held for more than 1 s, the device switches to the state where the second digit of the code is selected, i.e. 6.
  6. The second digit of the four-digit code is highlighted in square brackets on the bottom line of the display. By briefly pressing the clock button, the register of the entered digit is incremented**. If the tact button is pressed and held for more than 1 s, the device switches to the state where the third digit of the code is selected, i.e. 7.
  7. The third digit of the four-digit code is highlighted in square brackets on the bottom line of the display. By briefly pressing the clock button, the register of the entered digit is incremented**. If the tact button is pressed and held for more than 1 s, the device switches to the state where the fourth digit of the code is selected, i.e. 8.
  8. The fourth digit of the four-digit code is highlighted in square brackets on the bottom line of the display. By briefly pressing the clock button, the register of the entered digit is incremented**. If the tact button is pressed and held for more than 1 s, then the device switches to the state from where the “Code Entry” subroutine was requested, i.e. 3 or 4.
  9. The device closes the lock and saves the state of the lock and the code. In the top line displays the inscription "Saving Saving." and a four-digit code on the bottom line. Next, the device goes to the state where it displays the encoding statistics, i.e. 2.
  10. The device opens the lock and saves the lock state and code. In the top line displays the inscription "Saving Saving." and a four-digit code on the bottom line. Next, the device goes to the state where it displays the encoding statistics, i.e. 2.
  11. In the top line displays the inscription "Error Error" and in the bottom line a four-digit code. (Photo 4) After a short or long press of the tact button, the device switches to the state where it displays the encoding statistics, i.e. 2.

*After the counter overflows (greater than 65535), it resets to zero and the count starts again, which leads to a failure in statistics, in the sense that the number of encodings can be greater than the number of decodings. Therefore, it is recommended to de-energize the device to reset the counters.

** When incrementing the number 9, zeroing occurs.

Since the EEPROM memory (set in the configuration) is protected from in-circuit reading of the microcontroller, it will not be possible to read and find out the password in-circuit, and therefore turn on the electric lock. There remains an easier way to open it - directly apply voltage to the electric lock directly. I conclude that the “combination lock” device and the electric lock must be reliably protected from the penetration of unauthorized persons. The button and display must be freely accessible.

It should be noted that the device can be de-energized, anyway, after entering the code, the state of the lock and the code are stored in the non-volatile EEPROM memory. It is forbidden to de-energize the device while saving the code in the non-volatile EEPROM memory.

It is worth paying attention to one important detail in the operation of the device. When the device is turned on, it can open the electric lock for a short time (for a time less than 1 s), despite the fact that the closed state of the electric lock is stored in the non-volatile EEPROM memory. When simulating the execution of the program code in the MPLAB IDE environment, this error was not detected by me. In case of an unexpected power failure of the device while saving the code in EEPROM memory, the code can be saved incorrectly and it will not be possible to restore it, which will lead to re-programming of the microcontroller. This leads to a recommendation about the need for a stable and (or) backup power supply to the device. GB1 - backup power.

See the files for the manufacture of printed circuit boards in the folder.

The following parts can be replaced on this unit. Microcontroller DD1 from the PIC16F628A-I / P-xxx series with an operating clock frequency of 20 MHz in a DIP18 package. The HG1 display will fit any of the WH1602x series. Voltage stabilizer DA1 domestic KR142EN5A (5 V, 1.5 A). Field MOSFET transistor VT1 (N-channel) in the I-Pak package (TO-251AA), an analog of the rating indicated on the diagram is suitable. Piezo sound emitter P1 with an operating frequency of sound generation of 4 kHz. Diode bridge VD1 can be applied to any of the 2Wxx series. Power connector X2 similar to that shown in the diagram with a central contact d=2.1 mm. Non-polar capacitors C1 and C2 with a nominal value of 0.01 - 0.47 µF x 50 V. Electrolytic capacitors C3 and C4 have the same capacitive rating, and the voltage is not lower than that indicated in the diagram.

Download archive with code lock project: 16F628Code_Lock.rar

Loading...Loading...