Introduction: Arduino Lock Mechanism

This is a simple Arduino project that emulates a home security system.

It is fully simulated on Proteus though you should not expect much difference on a real environment.

You'll require:

  1. Proteus
  2. Atmel Studio
  3. Visual Micro for Atmel Studio
  4. Arduino IDE

You can just use Arduino IDE with Proteus but I prefer the complete set up to take advantage of:

  1. The .hex file is stored within the project folder
  2. Atmel Studio has compelling auto-complete features

Basically when the project loads you shall see a such a set-up

Step 1: Set-up, Code

#include 
#include 

#include 
#include 
int greenLed=22;
int redLed=23;
int servoPin=24;// Servo
int piezoPin=25;
char* ourCode="1234";
int currentPosition=0;
Servo myservo;
int pos = 0;
const byte rows=4;
const byte cols=3;
char keys[rows][cols]={
	{'1','2','3'},
	{'4','5','6'},
	{'7','8','9'},
	{'*','0','#'}
};
byte rowPins[rows]={13,10,9,8};
byte colPins[cols]={7,6,1};
Keypad keypad=Keypad(makeKeymap(keys),rowPins,colPins,rows,cols);
LiquidCrystal Lcd (12,11,5,4,3,2);
void setup()
{
	Lcd.begin(16,2);
	myservo.attach(24);
	displayCodeEntryScreen();
	pinMode(piezoPin,OUTPUT);
	pinMode(servoPin,OUTPUT);
	pinMode(redLed,OUTPUT);
	pinMode(greenLed,OUTPUT);
	digitalWrite(redLed,LOW);
	digitalWrite(greenLed,LOW);
}
void loop() {
	int l;
	char key=keypad.getKey();
	Lcd.setCursor(0,0);
	Lcd.print(" ENTER PASSWORD ");
	
	if(int(key)!=0){
		Lcd.setCursor(4,1);
		for(l=0;l<=currentPosition;++l)
		{Lcd.print("*");}
		if(key==ourCode[currentPosition])
		{
			++ currentPosition;
			if(currentPosition==4)
			{unlockDoor();
				currentPosition=0;
			}
		}
		else{invalidCode();
			currentPosition=0;
		}
		
	}
}
void invalidCode()
{
	digitalWrite(redLed,HIGH);
	digitalWrite(servoPin,LOW);
	digitalWrite(piezoPin,HIGH);
	clearScreen();
	Lcd.setCursor(0,0);
	Lcd.print("ACCESS DENIED!");
	Lcd.setCursor(0,1);
	Lcd.print("INVALID CODE ");
	delay(5000);
	digitalWrite(redLed,LOW);
	digitalWrite(piezoPin,LOW);
	displayCodeEntryScreen();
}
void turnHandle()
{
	for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
	{                                  // in steps of 1 degree
		myservo.write(pos);              // tell servo to go to position in variable 'pos'
		delay(15);                       // waits 15ms for the servo to reach the position
	}
	for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees
	{
		myservo.write(pos);              // tell servo to go to position in variable 'pos'
		delay(15);                       // waits 15ms for the servo to reach the position
	}
}
void unlockDoor()
{
	digitalWrite(greenLed,HIGH);
	clearScreen();
	Lcd.setCursor(0,0);
	Lcd.print(" ACCESS GRANTED ");
	Lcd.setCursor(0,1);
	Lcd.print(" WELCOME!! ");
	digitalWrite(servoPin,HIGH);
	turnHandle();
	delay(500);
	digitalWrite(greenLed,LOW);
	digitalWrite(servoPin,LOW);
	displayCodeEntryScreen();
}
void displayCodeEntryScreen()
{
	clearScreen();
	Lcd.setCursor(0,0);
	Lcd.print("   HELLO AND    ");
	Lcd.setCursor(0,1);
	Lcd.print("    WELCOME!    ");
	delay(3000);
	clearScreen();
}
void clearScreen()
{
	Lcd.setCursor(0,0);
	Lcd.print("                ");
	Lcd.setCursor(0,1);
	Lcd.print("                 ");
	
	
}

Step 2: Welcome Screen

On a successful build the file should be stored in the following file directory eg :C:\Users\Username\Documents\Atmel Studio\6.2\ProjectFolder\ProjectName\Debug

The welcome screen is as shown...

Step 3: Password Request

Enter your password

In our Case 1234

Step 4: Access Granted

On successful entry

Step 5: Closing

After the motor moves to simulate door unlocking it will lock again and display home screen