0% found this document useful (0 votes)
61 views5 pages

Pic Based Lock

This document describes an electronic lock system using a PIC16F628A microcontroller. The system implements a keypad and LCD display to provide an interface for users. The firmware controls these components to allow users to open and close the lock, change passwords stored in EEPROM, and use a backspace function. The keypad scanning works by assigning port pins for rows and columns and detecting pressed keys. The system aims to provide a more secure option than a traditional lock.

Uploaded by

amshahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views5 pages

Pic Based Lock

This document describes an electronic lock system using a PIC16F628A microcontroller. The system implements a keypad and LCD display to provide an interface for users. The firmware controls these components to allow users to open and close the lock, change passwords stored in EEPROM, and use a backspace function. The keypad scanning works by assigning port pins for rows and columns and detecting pressed keys. The system aims to provide a more secure option than a traditional lock.

Uploaded by

amshahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SECURE ELECTRONIC LOCK USING PIC 16F628A

MICROCONTROLLER
Muhanad Hayder Mohammed
Lecturer in Computer Science Department, University of Kerbala Iraq
[email protected]

Abstract: The proposed system implements an computer designated to perform some specific tasks.
electronic embedded lock that provides a great benefit A Microcontroller program (firmware) is the one,
over a traditional lock, which uses only a manual key. which decides what functionality the microcontroller
If in case, the key is lost or stolen then anyone can provides to a user. A program that has the ability to
open the lock using the key. On the other hand, losing run on a microcontroller without the need of an
a long and complex password or getting it stolen is operating system is called as a firmware. That means,
harder as compared to a traditional key. Further, a a firmware has the privilege to access the hardware
combination of both, the manual key with a directly. This paper tries to explain an electronic lock
computerized password makes the system more firmware in detail. The basic idea of microcontroller is
secure. A longer password will reduce the possibilities to collect all the input and output peripherals in one
of getting the code broken and opening the lock. Our simple circuit, which represent the microcontroller
system comprises of a keypad and HD44780 20x2 instead of the large and sophisticated computer with
LCD along with a PIC16f628a microcontroller. The microprocessor and large numbers of peripherals [1].
firmware controls these components in such a way
that interaction with keypad is very easy and smooth. The firmware directly deals with peripherals and
The LCD provides user with messages and Input / Output ports to give complete functionality of
notifications to inform about the current system state. microcontroller.
User can perform operations such as opening and
closing the lock, changing the current password in the II. MICROPROCESSOR vs MICROCONTROLLER
microcontroller EEPROM and clearing a single digit Microprocessor differs from a microcontroller in
while entering the password when wrong digit entered many aspects. First and the most important aspect is
(back space). The proposed system’s firmware is its architecture. In order for a microprocessor to
developed using assembly language with MPLAB function properly, other components such as memory,
development environment. It is tested and peripherals and input output ports must be connected
implemented at the actual hardware level with proper to it. In short, we can say that a microprocessor is the
functioning of the system and it is completely bug free. heart of the computer and it works a group with other
peripherals / parts of the computer system. On the
Keywords: Electronic lock, embedded system, PIC other hand, a microcontroller is designed to be
microcontroller. comprised as a single unit which can perform
I. INTRODUCTION independently. No other external component is needed
for its application because all necessary peripherals
Today, we are living in the in the of embedded and ports are already built into it. Ultimately, it saves
systems surrounded by devices that based on the the time and space needed to construct devices [2].
embedded systems like cars, washing machine
microwave-oven, medical equipment's etc. III. SYSTEM IMPLEMENTATION
An embedded system is a computer system The system is implemented using assembly
designed for specific control functions often with real- language. The purpose of using this language is to get
time computing constraints. It is embedded as part of a a better picture and understanding of the PIC
complete device which often including hardware and architecture. Another positive aspect of using
mechanical parts. By contrast, a general-purpose assembly language is that it gives very wide and
computer, such as a personal computer (PC), is flexible way to interact with microcontrollers even
designed to be flexible to meet a wide range of end- though its implementation is harder than BASIC or C
user needs. Embedded systems control many devices languages, which are also used as a language for
in common use today [1]. programming microcontrollers. There are mainly two
One of prominent example of an embedded system parts of the system implementations:
is a microcontroller, which is a small and tiny
A. Keypad Implementation then both the pin voltage would drop to zero. This fact
could be used for designing the keypad.
The system dedicates PORTB for keypad
As mentioned above, PORTB is reserved for the
implementation in which 4 pins are reserved for
keypad and the most significant 4 bits are used for
columns and the remaining 4 pins for rows. Hence, the
column indexing, which are set as input port (for input
system uses these 8 pins for construction of 16 keys
port, we must set TRISB register to high [3]) to check
matrix, which makes an efficient use of the limited
whether they have dropped to 0 voltage when they are
ports provided in PIC16F628A. Following is the Table
attached with the selected row after pressing the key.
(1) PORTB pins assignments for keypad.
On the other hand, the least significant 4 bits of
Table1: PORTB pins assignments for keypad PORTB are set to output, and are used for row
indexing. Since they are the output ports, they can be
RB4(column0) RB5(column 1) RB6(column 2) RB7(column 3) set to high (1) or low (0) by the firmware. By setting
each row to zero for every key scan, the system can
RB0(row0) 1 2 3 A =Back space check each column, to see if any one of them is set to
RB1(row1) 4 5 6 B= lock the system
0. If so, this means that one key of that row has been
pressed. This process is repeated for all rows to check
RB2(row2) 7 8 9 C= Modify pass. all the keys. The functions responsible for scanning
RB3(row3) * 0 # D=Enter key are row_scan and col_scan.
1.1 Row scan
There is an important precaution which must be
taken while implementing the keypad because, the This function sets every row to zero at a time and
keypad processing rely on a mechanical process calls col_scan function to check if any column pins
(pressing and releasing keys). This phenomenon drop to zero voltage. This function then increments the
generates a spark, which influences the electrical key variable for each row assignments, to index the
properties of pin while pressing and releasing the key, row.
which causes the pin status to be unstable and can’t be
recognized properly whether it is 0 or 1. So, to 1.2 Col_scan
overcome this, a delay must be provided to give This function scans every column pin to check if
enough time for the pin status to be stable and can be any one of them is set to zero, if so then it calls
read correctly, this time could be 20ms or more. find_key function and finds the key value throw the
index constructed using the variable key and the hard
1. Key Scanning
coded value getting from working register w throw
The keypad design is influenced by electrical col_scan Function. Figure (1) show the keypad
phenomena, which states that, if we short circuit two structure connected to PIC 16F628A microcontroller
pins, one with zero and the other with high voltage, and LCD pins assignments.

Figure 1: Microcontroller pins assignment


B. LCD Implementation Then the user enters the password which is limited
to 10 digits and can combined with the special
The other main part of the system implementation characters ‘*’ and ‘#’ making the password more
is LCD implementation. The LCD configures to complex to be broken.
operate in 4 bits interface mode, because of
PIC16F628A ports limitations. Sending the command The following is the figure showing the user
0x20 then 0x28 will configure the LCD to work in 4 entering the password to the system (digits displayed
bits interface mode with two lines[4], which means as ‘*’ in LCD but they are displayed here as for clarity
that the maximum characters can be displayed are 40 to the readers).
characters for a 20x2 LCD used by the system.
The function send_word is responsible for the
implementation of sending a word to LCD in a 4 bit
interface mode. Following steps are performed by the
send_word function to send a single word in 4 bit
interface mode [4]:
1. Set the E line low. Figure3: User entered the password to open the lock
2. Set the RS line high for sending data, or low for
sending a command. After entering the password, user is required to
3. Set the E line high. press the Enter button. Then the function
4. Put the four most significant bits on DB4 to DB7. verify_password would be called to verify if the input
5. Set the E line low. password is similar to the one that is stored in the
6. Set the E line high. EEPROM. If the two passwords are same, then the
7. Put the four least significant bits on DB4 to DB7. lock will open and the message “verify successfully”
8. Set the E line low The E line should remain low would be displayed on LCD.
until clocking in another byte. Following are the steps for verify_password,
function which would make a comparison between two
IV. SYSTEM OPERATION arrays if they have the same value or not. Indirect
memory access is used for the comparison process [2].
When the system starts, it enters directly into the
main loop. In its entire operation time, it is 1. Initialize count with value 10.
interchanging only between two states. First state 2. Make FSR point to start address of the first array.
continuously scans the input key and the other state is 3. Store the value of location that FSR point to it in
go to sleep on PORTB, because PORTB is configured variable name data1(first array).
to PULL UP which makes the system to operate in 4. Increment FSR and save its value in save1
power saving mode. This saves battery power and variable.
improves the system operation lifetime, since the
5. Test if the function in the first step in the loop.
microcontroller doesn’t require execution of
6. If yes will make FSR point to start address of the
instructions continuously even if nobody interacts with
second array and go to step 8.
the system.
7. If no will put the value of variable save2 to FSR.
The system moves from sleep to wake up state 8. Store the value of location that FSR point to it in
while pressing any key, which change the status of the variable name data2 (second array).
PORTB. PIC 16F628a has the PULL UP on PORTB 9. Increment FSR and save the value of it in save2.
property for all the PROTB pins [3]. 10. Put the value of save1 in FSR.
When the system in a lock state, and user tries to 11. If data1 equal data2 then:
open the lock, the user have to enter ten digits that  Decrement count.
must conform to the ten digits stored in the EEPROM.  If count equal zero will go to step 13.
The following figure prompts to the user to enter the  Else will go to step 3.
password: 12. Else go to out of loop and assign false to flag.
13. Return and assign true to flag.
The next function is ReadEEPROM which is used
to read the stored password from to EEPROM to the
file register in array to further compare with the
password entered by the user.

Figure2: Message prompt to user to enter the Following are the steps performed by
password ReadEEPROM:
1. Initialize count with value 10.
2. Assign start address of EEPROM to specific
pointer.
3. Make FSR point to start address of array, which
will contain the value of EEPROM.
4. Increment FSR.
5. Read value of one location from EEPROM and
write it in the array.
6. Increment the pointer that point to EEPROM.
7. Decrement count. If equal to zero, it mean the
EEPROM password has been read completely then
return. Otherwise, go to step 4.
The other important function is WriteEEPROM Figure 4: Complete hardware components of the
function, which writes the new password to EEPROM system
when the user wants to change the password. Writing to
the EEPROM is critical operation which should not be It is evident from the figure, that there are only a few
performed accidentally [3], because the results would electronic components involved in the working of this
be permanent, unless and until we re-write the same system. This is really a great benefit of using an
location of EEPROM. For this reason, two values AAh embedded system, which makes the hardware
and 55h must be written to EECON2 register one after architecture very simple, but the complexity ultimately
another to be sure the program want to really write to moves to the firmware itself according to the concepts
EEPROM. of software and hardware boundary.

Following are the steps performed by the function V. CONCLUSION


WriteEEPROM:
Programming the LCD with 4 bits interface mode is
very efficient and the delay of sending a single byte in
1. Initialize count with value 10.
two steps was not noticeable, irrespective of reserving
2. Assign start address of EEPROM to specific
the 4 bins port in a limited ports microcontroller.
pointer.
3. Make FSR point to start address of array which The other issue is using PULL UP on PORTB,
contain the value that must be store in EEPROM which proved beneficial for the system, because it puts
4. Increment FSR. the system in the sleep mode within the infinite loop
5. Read value of one location from array to while waiting for a key press. So when there is no
temporary variable. interaction with the system, it saves batteries power and
6. Write the value 0xAA to EECON2. improves the functional life of the system. Hence there
7. Write the value 0x55 to EECON2. is no wastage of CPU cycles for unimportant functions.
8. Write the temporary variable to EEPROM. After implementation of the system using real
9. Increment the pointer that point to EEPROM. hardware components, there was one technical
10. Decrement count if equal zero that meaning all difficulty I encountered, and want to emphasize in this
password has finished write in all location of paper, was the implementation of keypad. As many
EEPROM and return, otherwise will go to step 4 researches state that, Columns has to be set as input to
The system has the buzzer to represent an alarm be read, to check their status to know which key has
when the user enters a password three times the buzzer been pressed, and the rows as output, which can be set
will give a high sound. This pin could be configured by firmware to 1 or 0 [5,6]. This approach did not work
with rely to switch a high power alarm when a security here because, if 4 pins are set to input simultaneously
breach occurs. their state would be influenced when one of them is
changed causing inaccurate key scan for reading the
The proposed system is very simple and has very pressed key. The solution found was to set every port
few components involved, because all the functions are pin of column to input then output every time we check
implemented in the firmware that is installed in the PIC the column status. In this case, only one pin would be
microcontroller. set as input at a time and the others are set to output,
which would not be influenced by changing state of that
Figure 4, shows us the complete hardware pin. This gives us an accurate and smooth keypad
components used in the system on the breadboard. operation.
The system is intact and sound, and with some
improvements and operational testing it can be
considered as a successful product and can be shipped
to the market.

VI. REFERENCES
[1] Milan, Verle, “PIC Microcontrollers”,
mikroElektronika; 1st edition, 2008.
[2] Nebojsa, Matic, ”PIC Microcontrollers on line”,
MikroElektronika, 2000.
[3] Microchip, “PIC16F628x Data Sheet” Microchip.

[4] Hitachi, “LCD HD44780 datasheet”, Hitachi.

[5] Peter, JAKAB, “Electronic Combination Lock based on


PIC”, [Online] Available:
http://jap.hu/electronic/combination_lock.html
[6] DAT, “Simple Combination lock Project with keypad and
LCD”, [Online] Available:
http://www.8051projects.net/download-d195-simple-
combination-lock-project-keypad-lcd.htm

How to cite
Muhanad Hayder Mohammed, "Secure Electronic Lock using PIC 16F628A Microcontroller". International Journal
of Research in Computer Science, 2 (5): pp. 43-47, September 2012. doi:10.7815/ijorcs.25.2012.047

You might also like