Friday, 24 October 2014

Password Protected System Using Arduino

Password Protected System Using Arduino

Hello readers, in this article I am going to discuss with you the fundamentals and programming art involved behind the design of password protected systems. These systems are very commonly used in our day to day life like in ATM machines, Electronic safe lock, door lock etc. These systems in common involve keypad input unit for entering the password and a display unit (generally an lcd display unit) for visual display of information, some switching mechanism to activate the motors in case of locks and a programmed microcontroller that will process the input information and take the appropriate action. So in order to design these systems the designer must have a good knowledge of programming for interfacing these peripherals with the microcontroller. So before I start explaining you all these concepts lets define an aim that is required to be achieved.

Aim-

To design a password protected system based on Arduino. The system will have a 16*2 lcd display for visual display of information, an led for indication and a 4*3 keypad for entering the information. If correct password is entered by the user then the lcd should display correct password and led should glow for 3 seconds. If user enters an incorrect password then lcd should show access denied and if user enters an incorrect password 3 times consecutively then system should lock.

Now the aim is quite clear and for achieving it the first step is to understand how 4*3 keypad works.


Fig. below shows how a 4*3 keypad can be constructed using micro switch:-




It has 4 rows denoted here by r1, r2, r3, r4 and 3 columns denoted here by c1, c2, c3.

Pictorial view of 4*3 keypad used in proteus is shown below-




Every number has a unique column number and a unique row number for example digit ‘9’ has row number r3 and column number c3, similarly for digit ‘4’ row number is r2 and column number is c1. Whenever a key is pressed the row and column number corresponding to that digit will get shorted and so if we want to identify that which key is pressed, we just need to identify the row and column numbers that gets shorted. Fig. below shows one such case-





The circuit diagram drawn in proteus is shown below-




In programming declare all the column pins as inputs and all row pins as output.

pinMode(r1,OUTPUT); pinMode(r2,OUTPUT); pinMode(r3,OUTPUT);
pinMode(r4,OUTPUT); pinMode(c1,INPUT); pinMode(c2,INPUT);
pinMode(c3,INPUT);

Ground all the rows and make all column pins high. Then keep on checking the status of all column pins and if any one of them goes low then check which one goes low.

while(digitalRead(c1)==HIGH && digitalRead(c2)==HIGH && digitalRead(c3)==HIGH);

if(digitalRead(c1)==LOW)
{
col1();
}

if(digitalRead(c2)==LOW)
{
col2();
}

if(digitalRead(c3)==LOW)
{
col3();
}

Subroutines col1(), col2() and col3() contains codes for checking the particular row number due to which the column has become Low and in those subroutines ground one row at a time and then check the status of the column pin to detect the unique row number.

The complete code is given below for your reference.   

Code-

#include<LiquidCrystal.h>

/*LCD rs=12, en=11, D4=10, D5=9
D6=8,  D7=7
*/

LiquidCrystal lcd(12,11,10,9,8,7);

//defining pins used for rows and columns

int c1=2;
int c2=3;
int c3=4;
int r1=5;
int r2=6;
int r3=A0;
int r4=A1;

//attempt is a variable used to keep a check on number of times
//wrong password is entered

int attempt;

//temp[] array stores the 3 digit password entered by user
char temp[3];
int m;
void col1() //routine for checking column1
{
  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  if(digitalRead(c1)==LOW) // r1 and c1 shorted so '1' pressed
  {
    lcd.print("1");
    temp[m]='1';
    delay(100);
  }
 
  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  if(digitalRead(c1)==LOW) // r2 and c1 shorted so '4' pressed
  {
    lcd.print("4");
    temp[m]='4';
    delay(100);
  }
 
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,LOW);
  digitalWrite(r4,HIGH);
  if(digitalRead(c1)==LOW)
  {
    lcd.print("7");
    temp[m]='7';
    delay(100);
  }
 
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,LOW);
  if(digitalRead(c1)==LOW)
  {
    lcd.print("*");
    temp[m]='*';
    delay(100);
  }
  digitalWrite(r4,HIGH);
  delay(200);
}

void col2() // routine for checking column2
{
  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  if(digitalRead(c2)==LOW) // r1 and c2 shorted so '2' pressed
  {
    lcd.print("2");
    temp[m]='2';
    delay(100);
  }
 
  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  if(digitalRead(c2)==LOW)
  {
    lcd.print("5");
    temp[m]='5';
    delay(100);
  }
 
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,LOW);
  digitalWrite(r4,HIGH);
  if(digitalRead(c2)==LOW)
  {
    lcd.print("8");
    temp[m]='8';
    delay(100);
  }
 
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,LOW);
  if(digitalRead(c2)==LOW)
  {
    lcd.print("0");
    temp[m]='0';
    delay(100);
  }
  digitalWrite(r4,HIGH);
  delay(200);
}

void col3() // routine for checking column3
{
  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  if(digitalRead(c3)==LOW) // r1 and c3 shorted so '3' pressed
  {
    lcd.print("3");
    temp[m]='3';
    delay(100);
  }
  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  if(digitalRead(c3)==LOW)
  {
    lcd.print("6");
    temp[m]='6';
    delay(100);
  }
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,LOW);
  digitalWrite(r4,HIGH);
  if(digitalRead(c3)==LOW)
  {
    lcd.print("9");
    temp[m]='9';
    delay(100);
  }
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,LOW);
  if(digitalRead(c3)==LOW)
  {
    lcd.print("#");
    temp[m]='#';
    delay(100);
  }
 digitalWrite(r4,HIGH);
 delay(200);
}

void scan_choice() // routine for scanning the input
{
  digitalWrite(r1,LOW); 
  digitalWrite(r2,LOW); 
  digitalWrite(r3,LOW); 
  digitalWrite(r4,LOW); 
  digitalWrite(c1,HIGH);
  digitalWrite(c2,HIGH);
  digitalWrite(c3,HIGH);
  while(digitalRead(c1)==HIGH && digitalRead(c2)==HIGH && digitalRead(c3)==HIGH);
 
  if(digitalRead(c1)==LOW)
  {
    col1();
  }
  if(digitalRead(c2)==LOW)
  {
    col2();
  }
  if(digitalRead(c3)==LOW)
  {
    col3();
  }
}


void next() // subroutine called for begining next cycle
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter Password:");
  lcd.setCursor(0,1);
}

void setup()
{
  lcd.begin(16,2); // lcd initialized 16*2
  lcd.print("Enter Password:");
  lcd.setCursor(0,1);
  pinMode(r1,OUTPUT);
  pinMode(r2,OUTPUT);
  pinMode(r3,OUTPUT);
  pinMode(r4,OUTPUT);
  pinMode(c1,INPUT);
  pinMode(c2,INPUT);
  pinMode(c3,INPUT);
  pinMode(13,OUTPUT); //led connectd to pin 13
 
}

void loop()
{


for(m=0;m<3;m++)
{
  scan_choice();
}

if(temp[0]=='7' && temp[1]=='8' && temp[2]=='6') // correct password 786
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Access Granted");
  digitalWrite(13,HIGH); // led on
  delay(3000); // delay of 3 seconds
  digitalWrite(13,LOW); // led off
  next();
}
else
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Wrong Password");
  delay(1500);
  next();
  attempt=attempt+1; // variable increment by 1 on entering wrong password
  if(attempt==3) // if 3 wrong passwords entered consecutively
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("No More Trials");
    lcd.setCursor(0,1);
    lcd.print("Acess Denied");
    while(1); // stay here until system is reset again
  } 
}
}

This code is tested in proteus simulation software and also on the hardware and it worked perfectly in both the cases. Fig. below shows the run time snap shot when correct password was entered. Access Granted message is printed on the lcd display and led connected to pin number 13 remains on for 3 seconds and then it goes to off state.



When wrong password is entered 3 times then the system will be locked and user needs to press reset button so that system begins executing program from the start. The snap shot under no more trials condition is given below-



This project demonstrates the concepts used in designing password protected systems using Arduino. This idea can be extended to meet your demands and I hope that after reading this article you will be in a position to do just that and if not then don’t panic your help is just one click away.  

THANKS FOR READING………..
UMESH DUTTA


No comments:

Post a Comment