You are on page 1of 19

AVR Project ATmega8 Based Smart Code Lock

Here is a project for beginners using Atmel AVR ATmega8. The project uses some techniques that
are very useful for designers to learn and utilize.
Alphanumeric LCD Module Interfacing.
44 Keypad interfacing.
PWM Control of LED (Used to dim the back-light of LCD, like in some Nokia Phones)
Basic Circuit design for AVR MCUs
Connecting relays and other current demanding devices.
AVR Studio and project management.
compiling using free avr-gcc compiler.
Using eXtreme Burner AVR to burn hex code to AVR MCU.
NOTE:
Dont waste time finding each part running from shop to shop. Get all the parts with exact
specification in a ready to use kit! Purchase ATmega8 based smart code lock KIT.


Fig.: ATmega8 Based Code Lock

Fig.: ATmega8 Based Code Lock Project


Functions of the Project.
This device can be used to code protect any electrical device (like an electronic door, lock or safe).
The user must enter a correct password to gain access. The password is entered using the built in
44 keypad matrix. The main LCD Module is used to display messages to the user. As soon as
correct password is entered the relay is activated. This is indicated by a LED which is placed near the
relay. The relay remains on as long as the user wishes. You just need to press a key to deactivate the
relay.
After typing four digit password you must press the "OK" button (S8). At anytime you may press
the "Cancel" key (S12 )to clear the input (e.g. when you enter any incorrect digit)
The smart code lock has the feature to change the password too. For this enter a special password
which is 0000, as soon as the system receives this special password it switches to change password
mode. Here you need to enter the old password to gain permission, then enter the new password.
The smart code lock also support power saving feature. The backlight of LCD turns off automatically
when the system is idle for few seconds. The cool thing about this is that it uses PWM to control the
brightness of LCD Backlight. So the dimming is very smooth and it is like those used in many mobile
phones.
Making it yourself!
You can fabricate the circuit on any general purpose PCB or breadboard. The schematic and the
BOM (Bill of Material is given below). To save you from lots of trouble we have made a quality PCB of
the same! If you purchase the PCB, you just need to solder different components at their proper place
on the PCB and you are ready to go.
Burn the HEX file (given at the end of article) to the ATmega8 MCU using any suitable
programmer and plug it into the 28 PIN IC Socket.
After assembling the circuit power it on using a 12V 500ma Center Positive DC Wall adaptor (you can
use one that is used for mini TVs or Game Consols or Your DSL Modem). Adjust the variable resistor
RV1 (just below the LCD module, on the left side, yes the blue thing!) until the LCD Shows some text.
Schematic for ATmega8 based Smart Code Lock

Fig.: Complete Schematic (Click to Enlarge/Print)
Bill of Materials (BOM)
Part List
01 330 ohm resistor (2 Nos) R3, R5
02 4k7 Resistor (3 Nos) R2, R4, R6
03 200 Ohm Resistor R1
04 0.1uF Ceramic Disk Capacitor (4 Nos) C1, C3, C4, C5
05 1N4007 Diode (2 Nos) D1, D3
06
LED
5mm Any Colour
D4
07 ATmega8L-8PU General purpose 8 bit MCU U1
08
7805
Voltage Regulator
U2
09 Power Connector 2 Way CON1
10 PCB Mountable Relay RL1
11 On/Off Switch SW1
12 DC Socket X1
13 162 LCD Module LCD1
14 10K Preset (Blue Plastic POT) RV1
15 28 PIN Narrow IC Socket
16 BC548 Transistor(2 Units) Q1, Q2
17 Push Buttons (16 Units)
18 ATmega8 Based Smart Code Lock PCB

Firmware Source Code
The firmware source code is written in a professional was that may seems confusing to a n00b
programmer. The thing is that every piece of related functions are stored in a separate source files.
That means the the project is composed of several C source file. Each C file has set of related
functions (like LCD Interfacing Routines or EEPROM Access). You must use AVR Studio as a project
manager and WinAVR as compiler. Both the software must be installed in your PC. In the project
folder their is a file with name "code_lock.aps", this is the main AVR Studio Project file. Load it in
AVR Studio. You can see the various files that are part of the project in the left hand pane as shown
below. Double click any file to load in the editor. After a file has been loaded in editor you can view
and change it.

Fig.: AVR Studio Main Window
The highest level file which contains the main application logic is "code_lock.c". This file contains
the main() function. The rest files are lower level files that provide service to the higher level files. If
you want to customize the firmware you can easily do so by editing the file "code_lock.c", in most
cases you do not have to touch the other C files.
Building the Binary from the source code.
You need to convert the high level human readable source code into machine language so that the
AVR chip can execute it. The complete process of this conversion is called "Building" (yes,
compilation is one of the steps of building). After build is complete you get a file called
"code_lock.hex" which is placed inside a folder called "default" inside the project folder. You just need
to program this hex file to the ATmega8 MCU using anysuitable programmer .

Fig.: AVR Studio Build Button
You can build the project using the "Build Active Configuration" button as shown above. You can
also hit F7 button or Select Build from Build Menu.
The Fuse bits must be set as follows. Consult your programmer software for more info.
HIGH=D9
LOW=E1
Please note that the above is default for ATmega8, so if you purchased a new chip then you do
not need to change them.
If you are new to AVR Studio and WinAVR Please read the following article carefully.
Hello World Project with AVR Studio and WinAVR.
avr-gcc program for Code Lock

/**********************************************************************
********

Title:

ATmega8 Based Smart Code Lock.

Description:
A simple project for making a digital code lock. The main input
device is
a 4x4 Matrix Keypad. The main output is a 16x2 LCD Module.

The user has to enter a correct password to gain access. On receipt
of
correct password the relay is energized, which can be used to
operate

and device like an electronic door.

Their is facility for changing password.

For More information visit
http://www.eXtremeElectronics.co.in


Author:

Mandeep Tiwary under supervision of Avinash Gupta.

mandeep@eXtremeElectronics.co.in
avinash@eXtremeElectronics.co.in

Copyright:
eXtreme Electronics, India 2008- 2011

Notice:
No part of this work can be copied or published in electronic or
printed form without proper permission from the Original Creators.

ONLY INTENDED FOR EDUCATIONAL, HOBBY AND PERSONAL USE.
COMMERCIAL USE IS STRICTLY PROHIBITED.

Disclaimer of Warranty.

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER
PARTIES PROVIDE THE PROGRAM AS IS WITHOUT WARRANTY OF ANY KIND,
EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES

OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS
WITH YOU.
SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY
SERVICING, REPAIR OR CORRECTION.

***********************************************************************
*******/

#include <avr/io.h>

#include <inttypes.h>
#include <util/delay.h>

#include "lcd.h"
#include "keypad.h"
#include "user_interface.h"
#include "password_manager.h"


//LCD Backlight i/o configuration
#define LCDBL_PORT PORB
#define LCDBL_DDR DDRB
#define LCDBL_PIN PB1

//Output relay i/o configuration
#define RELAY_PORT PORTC
#define RELAY_DDR DDRC

#define RELAY_PIN PC0


//Simple Delay Function
void Wait(uint8_t n);

//Relay Control Functions
void RelayOn();
void RelayOff();


//System Functions
void SystemInit();

void main()
{
uint16_t password;

//Initialize the system
SystemInit();

while(1)
{
password=InputNumber("Enter Password");

//Match Password

if(password==ReadPassFromEEPROM())
{
LCDClear();
LCDWriteString("Access Granted");

//Now Activate Relay
RelayOn();

Wait(15);

LCDClear();
LCDWriteString("Press Any Key");

//Now wait for any key
while(GetKeyPressed()==255)
{
_delay_loop_2(10);
}

//Now DeActivate Relay

RelayOff();

Wait(2);

}
else if(password==0)
{
/*

If user enters 0000 as password it
indicates a request to change password


*/
LCDClear();
password=InputNumber("Old Password");

if(password==ReadPassFromEEPROM())
{
//Allowed to change password
uint16_t NewPassword;
NewPassword=InputNumber("New Password");
WritePassToEEPROM(NewPassword);

LCDClear();
LCDWriteString("Success !");

Wait(15);
}
else

{
//Not Allowed to change password
LCDClear();
LCDWriteString("Wrong Password !");

Wait(15);

}
}
else
{
LCDClear();
LCDWriteString("Access Denied");
RelayOff();

Wait(15);
}


}



}


void SystemInit()
{
//Set LCD Backlight Pin as output
LCDBL_DDR|=(1<<LCDBL_PIN);

//Set Relay Pin as output
RELAY_DDR|=(1<<RELAY_PIN);

//Wait for LCD To Start

_delay_loop_2(0);

//Now initialize the lcd module
LCDInit(LS_NONE);
LCDClear();
LCDWriteString(" Welcome !");

LCDBacklightOn();

//Check if the EEPROM has a valid password or is blank
if(ReadPassFromEEPROM()==25755)
{
//Password is blank so store a default password

WritePassToEEPROM(1234);
}

}

void Wait(uint8_t n)
{
uint8_t i;
for(i=0;i<n;i++)
_delay_loop_2(0);
}
void RelayOn()
{
RELAY_PORT|=(1<<RELAY_PIN);
}


void RelayOff()
{
RELAY_PORT&=(~(1<<RELAY_PIN));
}

PCB for AVR Based Smart Code Lock
We have made a high quality PCB complete with solder mask, component layout, tinning (for perfect
soldering) on a FR4 (highest quality) material. You can purchase the same from our store. Your
purchase help us create more interesting tutorials like this one and keep this site running.
You can purchase the PCB from the following link
PCB for AVR Based Smart Code Lock

Fig.: Code Lock PCB(Purchase Here)


Fig.: Code Lock PCB (Purchase Here)
Purchase Kit
Dont waste time finding each part running from shop to shop. Get all the parts with exact
specification in a ready to use kit! Purchase ATmega8 based smart code lock KIT. And help us
create more and more free AVR projects.
Downloads
Complete AVR Studio Project (ZIP File)
Precompiled hex file for burning to ATmega8 MCU
PCB Layouts in PDF, DOC and PNG Formats.