AVR Project – ATmega8 Based Smart Code Lock

Introduction

This project demonstrates the use of AVR microcontroller ATmega8 to build a smart code lock system. The code lock allows users to gain access by entering a correct password using a 4Γ—4 keypad. It displays messages on an LCD screen and activates a relay to control access. This project covers many useful techniques for beginners to learn about AVR programming, interfacing peripherals, and building embedded systems.

ATmega8 Based Smart Code Lock

Key Features of the Project

  • Alphanumeric LCD module interfacing
  • 4Γ—4 keypad interfacing
  • Password input and verification
  • Relay control for access
  • LED indicator for access status
  • Password change functionality
  • LCD backlight dimming using PWM
  • Open source code and instruction

ATmega8 Based Smart Code Lock

Hardware Implementation

The hardware used in this project is quite basic and accessible for beginners. The main components include the ATmega8 microcontroller, 4Γ—4 keypad, 16Γ—2 LCD, relay, LED, buttons and voltage regulator. These are assembled on a general purpose PCB or can be done on a breadboard. For convenience, a ready-made PCB is also available for quick prototyping.

The ATmega8 is the brain that processes inputs and controls outputs. The keypad and buttons are connected to I/O pins for reading key presses. The LCD is interfaced to display text messages. A relay module is driven by a transistor to control physical access. LED indicates access status. Overall the circuit design is simple yet effective to demonstrate the working of an embedded system.

Software Development

The firmware for this project is written in the C programming language. Several source code files are used to organize related functions. The main file contains the program logic while others contain lower level drivers. AVR Studio IDE is used as the development environment along with the avr-gcc compiler toolchain.

When the code is built, it generates a hexadecimal file containing the compiled machine code. This hex file needs to be programmed onto the ATmega8 chip using a programmer. Default fuse settings are provided for a plug-n-play experience. Together, the hardware and compiled software bring the code lock system to life.

 

Working of the Code Lock

When powered on, the LCD displays an initial message asking the user to enter password. Input is taken from the 4Γ—4 keypad one digit at a time on the LCD. After 4 digits are entered, the user needs to press the OK button. The entered password is compared to the stored one.

If correct, the relay is activated allowing access and the LED glows green. An access granted message is shown. To exit, any button press deactivates the relay. There is also a password change functionality using a special password. If incorrect password is entered thrice, the system gets locked for some time. Overall it works like a basic electronic lock.

ATmega8 Based Smart Code Lock

Educational Value

  • This AVR code lock project provides educational value in many ways:
  • Learns about microcontroller programming and interfacing peripherals.
  • Gets hands-on experience of building an embedded system from scratch.
  • Understands concepts of passwords, comparisons, relay control.
  • Sees PWM usage for dimming LCD backlight slowly.
  • Works with development tools like AVR Studio and avr-gcc compiler.
  • Troubleshoots any issues while constructing hardware or software.
  • Learns programming methodology of dividing into modules.

For hobbyists and students, this free and detailed guide acts as a helpful starting point for AVR development. It gives confidence to take up more advanced projects in future.

Hardware Expansion Possibilities:

  • The basic circuit can be enhanced by adding features like password display on LCD, alarm output, keypad lighting etc.
  • An RFID/fingerprint module could replace the keypad for secure contactless access.
  • Sensors like motion/contacts can make it operate automatically based on external conditions.
  • Communication interfaces like USB/Bluetooth allow remote control over smartphone.

Real World Applications:

  • Home security – front door/garage access control with code locking.
  • Office safety – locker/cupboard access for storage.
  • Access control for vehicle compartments/gates/barriers.
  • Timed access to vending machines/dispensing systems.
  • Laboratory sample cabinet/medical device security.

Project Improvements:

  • Encrypt password storage in EEPROM for heightened security.
  • Add button debouncing for stable keypad scanning.
  • Implement touch calibration for LCD screen buttons.
  • Grace period for re-entry if password is wrong to reduce errors.
  • Low battery indicator to avoid system failure due to power loss.

Learning Extensions:

  • Interface sensors to trigger alarm/notifications on tampering.
  • Connect GSM/GPRS shield for SMS control/alerts via cellular network.
  • Automate operation using RTC, schedule unlock timings daily/weekly.
  • Log access transactions to microSD card for viewing unlock history.
  • Connect servo motors to electronically manipulate locks/latches.

In summary, there are many ways this basic code lock project can be enhanced, customized for practical applications and extended further as per learning requirements of makers/engineers. The possibilities are immense with embedded platforms.

Conclusion

In summary, the AVR code lock project showcases the learning and making possibilities with affordable microcontrollers. The open documented approach makes embedded skills accessible irrespective of experience level. While a basic demonstration, it touches upon concepts useful for myriad applications. With ongoing support from instructional portals, hobbyist embedded systems can only grow to solve real world problems creatively.

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.

[email protected]
[email protected]

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

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top