You are on page 1of 11

25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

A Gadgetronicx > Microcontroller > PIC > PIC Projects > Electronic Code locking system using PIC 16F877 Mircocontroller
Electronic Code locking system using PIC 16F877
Mircocontroller
l Frank Donald September 10, 2014 v 87 Comments

m PIC Projects locks

Electronic Combination lock PIC Mircocontroller

Electronic code locking system is extremely useful in protecting our precious possessions and can be installed anywhere with bit of
engineering in it. We are widely familiar with the Password based e-locks and might have installed in our house. But we are going
for Electronic locks made by any company when you can make one by your own.

This project demonstrates you how to make a PIC microcontroller based simple digital lock and also explains the programming
behind it.

WHAT YOU NEED:


1. PIC 16F877 Microcontroller
2. 4×3 Keypad – Key Input Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 1/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

3. 16×2 LCD – Status Indicator


4. 5V Relay – Activator

Additionally you need a POT which is used to adjust the contrast of the LCD, A transistor to drive the relay since the current
obtained from a Pin of Microcontroller is very less. A diode to prevent the reverse flow of current which might damage the
controller.

DESIGN OF ELECTRONIC CODE LOCKING SYSTEM:


The design of the above Embedded Project is pretty straight forward, you need to interface a LCD to PORT B and Keypad to the
PORT D of the Controller. If you are not familiar with it then i suggest you to go through this Interfacing LCD and Keypad with PIC
Microcontroller. Now lets see how the system is designed to work as a Locker.

“*” Key – This key is meant to initialize the system, when the system is turned ON the Controller will scan only this key and
pressing this key will enable you to enter the password for your locker.
“#” Key – When you are done with your locker , you should press this key which will turn off the system and turning off the relay
as well.

The remaining keys are meant to feed the character values to the Microcontroller and in turn the Microcontroller will analyze the
characters. Based on the Pre defined password in the Controller it will compare the input with it. Thereby it will recognize the
correct or incorrect password input.

Circuits Library - 220+ practical circuits

CODE:
The below code was built using CCS compiler for the PIC Microcontroller
#include<16F877.h>
#include<stdio.h>
#bit led=0x05.0
#bit TRIS_led=0x85.0
#byte lcd=0x06
#byte TRIS_lcd=0x86
#bit rs=0x07.0
#bit TRIS_rs=0x87.0
#bit en=0x07.1
#bit TRIS_en=0x87.1
#bit relay=0x07.2
#bit TRIS_relay=0x87.2
#bit C1=0x08.0
#bit C2=0x08.1
#bit C3=0x08.2
#bit R1=0x08.3
#bit R2=0x08.4
#bit R3=0x08.5
#bit R4=0x08.6
Privacidade - Termos de

#bit TRIS_C1=0x88.0
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 2/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

#bit TRIS_C2=0x88.1
#bit TRIS_C3=0x88.2
#bit TRIS_R1=0x88.3
#bit TRIS_R2=0x88.4
#bit TRIS_R3=0x88.5
#bit TRIS_R4=0x88.6
void display(unsigned char a,int b); //LCD subroutine
char keypad(); //Keypad Subroutine
void check(); //Password check routine
char password[5]={"7196"}; //Predefined password
char pswd[5];
unsigned char open_msg[15]="Enter Password";
unsigned char welcome_msg[8]="Welcome";
unsigned char close_msg[15]="Wrong Password";
char c;
int flag,i,count,j;
void main()
{
TRIS_lcd=TRIS_en=TRIS_rs=TRIS_led=TRIS_relay=0; //Directions set
TRIS_R1=TRIS_R2=TRIS_R3=TRIS_R4=count=0;
TRIS_C1=TRIS_C2=TRIS_C3=1;
while(TRUE)
{
c=keypad();
{
if(c=='*') //Initialize condition
{
flag=1; //Flag set to scan other keys
count=0;
display(0x01,0);
display(0x38,0);
display(0x0f,0);
display(0x80,0);
for(i=0;i<=13;i++)
{
display(open_msg[i],1);
}
display(0xc0,0);
}
else if(c=='#') //Turning off condition
{
count=0;
relay=0;
display(0x01,0); Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 3/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

display(0x0c,0);
}
else
{
display('*',1);
pswd[count]=c; //Storing input in new arrays
count=count+1;
check();
}
}
}
}
void display(unsigned char a,int b)
{
lcd=a;
rs=b;
en=1;
delay_ms(10);
en=0;
delay_ms(10);
}
char keypad()
{
if(flag==0) //Waiting for Initialization
{
while(TRUE)
{
R4=1;
R1=R2=R3=0;
if(C1==1)
{
while(C1==1);
count=0;
return '*';
}
if(C3==1)
{
while(C3==1);
count=0;
return '#';
}
}
}
else if(flag==1) Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 4/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

{
while(TRUE) //Keypad scan
{
R1=1;
R2=R3=R4=0;
if(C1==1)
{
while(C1==1);
return '1';
}
if(C2==1)
{
while(C2==1);
return '2';
}
if(C3==1)
{
while(C3==1);
return '3';
}
R2=1;
R1=R3=R4=0;
if(C1==1)
{
while(C1==1);
return '4';
}
if(C2==1)
{
while(C2==1);
return '5';
}
if(C3==1)
{
while(C3==1);
return '6';
}
R3=1;
R1=R2=R4=0;
if(C1==1)
{
while(C1==1);
return '7';
} Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 5/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

if(C2==1)
{
while(C2==1);
return '8';
}
if(C3==1)
{
while(C3==1);
return '9';
}
R4=1;
R1=R2=R3=0;
if(C1==1)
{
while(C1==1);
return '*';
}
if(C2==1)
{
while(C2==1);
return '0';
}
if(C3==1)
{
while(C3==1);
return '#';
}
}
}
}
void check()
{
if(count>3) //Input exceeds count 3 will execute comparison
{
flag=count=0;
j=strcmp(pswd,password); //Comparison of input and Predefined pswd
if(j==0)
{
relay=1; //Turning relay on
display(0x01,0);
display(0x80,0);
for(i=0;i<=6;i++)
{display(welcome_msg[i],1);}
} Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 6/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

else
{
relay=0;
display(0x01,0);
display(0x80,0);
for(i=0;i<=13;i++)
{display(close_msg[i],1);}
}
}
}

NOTE:
You can add additional security by adding trial above code.
It can be done by making the trial value to increase for every wrong password input and checking the condition whether it
exceeds the desired trial value.
Once it exceeds the desired trial value disable the keypad scan and display message “No more trials” in the LCD.
You can also add speaker to sound a alarm once the trial exceeds to alert the people nearby.

JLCPCB - Only $2 for PCB Prototype (Any Color)


24 Hours fast turnaround, Excellent quality & Unbeatable prices

Up to $20 shipping discount on first order now: https://jlcpcb.com/quote

Related content

Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 7/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

Electronic code Locker 3.0 KB


main.hex 1447 Downloads
Version: 1 DETAILS

Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 8/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

← Previous post Next post →

87 Comments

hari October 13, 2019 at 11:17 pm

can u please help me resetting the password and user defining a new password for this project

Frank Donald (Post author) October 18, 2019 at 10:29 am

Resetting has to be done in the code. Including the feature in the code can make the code bulky and cumbersome.

yassine bellil February 27, 2019 at 8:44 pm

can you give me bluetooth code to send e msg to my cell phone when the ultrasonic detect something !!!

Frank Donald (Post author) February 27, 2019 at 10:43 pm

What you need requires BT module, GSM module and Ultrasonic module. A whole new circuit and code is
required to do that.

Kwaku December 26, 2018 at 9:34 pm

hello frank, at what frequency is the micro controller working at ? 8MHz or 4MHz

Frank Donald (Post author) December 27, 2018 at 7:51 am

Kwaku,
8Mhz

Kwaku December 27, 2018 at 3:40 pm

I have connected everything but nothing is showing on my LCD.. can you please share the proteus drawing

Frank Donald (Post author) December 27, 2018 at 3:54 pm

Not sure what the problem is, Have you programmed your MCU successfully?

Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 9/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

Kwaku December 27, 2018 at 4:30 pm

yeah , I copied and pasted the code on the website, it compiled successfully and i
proceeded to draw the circuit on proteus.. yet when I run the simulation on proteus I
get nothing on the LCD although I can observe changes on the terminals of the LCD

Kwaku December 29, 2018 at 3:25 am

it working.. thanks for the help Frank

Frank Donald (Post author) December 29, 2018 at 8:20 am

Thanks, Glad you got it figured out

Riad March 7, 2019 at 3:17 pm

dear kwaka

i have faced same problem, can you please help me. display didnt show anything.

rabah August 7, 2018 at 6:56 pm

hi Donald
I need help to add a password on my projects;;;; keypad and LCD interfacing a 16F877A PIC;;;;; with Micro C pro for PIC language

Frank Donald (Post author) August 9, 2018 at 7:55 am

Rabah,
Please go ahead, what help you need?

rahul May 19, 2018 at 9:41 pm

sir, am getting errors if I run it in MPLABx software, could you tell me any other software in which i can run this.

Frank Donald (Post author) May 21, 2018 at 9:31 am

Rahul,
Use CCS compiler

hamza May 13, 2018 at 7:55 pm


Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 10/11
25/11/2020 Electronic Code locking system using PIC 16F877 Mircocontroller - Gadgetronicx

Frank I Need your help on a project. contact me hamzainam459@gmail.com

Woon February 3, 2018 at 6:49 am

May i get assembly language of this program please.

Frank Donald (Post author) February 3, 2018 at 8:57 am

Woon,
Am sorry, am not familiar with Assembly code writing.

hannah January 16, 2018 at 3:03 pm

hi..can i know what type of diode did yo used??

Frank Donald (Post author) January 21, 2018 at 8:35 am

1N4001 will fit the purpose

← Older Comments

Privacidade - Termos de
Utilização

https://www.gadgetronicx.com/electronic-code-locking-system-pic-microcontroller/ 11/11

You might also like