Security Access control with Arduino, keypad and Servo motor

Security Access control with Arduino, keypad and Servo motor

We are going to simulate a security door access control in this tutorial. This tutorial should be very simple like the other ones, you can check them out:

 

This is the materials we will be using for this tutorial, you can get all of them from our STORE:

 

Circuit Diagram and Setting up the Arduino

the circuit diagram was achieved with proteus software but you can make use of fritzing:

 

keypad_arduino - steinacoz
keypad_arduino – steinacoz

i coouldn’t get the proteus library for the actual 4×4 keypad so in your actual circuit, just complete the missing D2 on the arduino board. Connect your arduino board to your PC and upload the following lines of code to it:

#include <Keypad.h> //include keypad library
#include <Servo.h> //include servo library


const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char original_pass[15]  ;
char entered_pass[15];




int angle = 0; // initialise the servo start angle
int i = 0;
Servo servo_test; //create servo object


//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

int ledPinSuc = 10; //led pin for successful authenication
int ledPinErr = 11; //led pin for failed authenication
int buzzerPin = 13; //buzzer pin for notification; beeps 2x for failed authenication and one long beep for successful authenication

void setup()
{
  servo_test.attach(12); //attach the servo to arduino pin 12
  
  pinMode(ledPinSuc, OUTPUT);
  pinMode(ledPinErr, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  i= 0 ;
  Serial.begin(9600);
}


void loop()
{
 strcpy(original_pass, "11111111"); //set password or default password
 char keypressed = myKeypad.getKey(); //get each charater pressed in the keypad
  
  if (keypressed != NO_KEY )//execute if no key have been pressed
  { 
    if(keypressed != '#')//execute as far as # key have not been pressed
    {
 
      entered_pass[i] = keypressed ;
      i++;
      Serial.print(keypressed);
    }
    else if (keypressed == '#') //you should press the # key after entering the password to start authenication
    { delay(1000);
      Serial.println(entered_pass);
      delay(1000);
      Serial.println(original_pass);
      delay(1000);
      Serial.println("checking....");
      delay(2000);
      if( strcmp(entered_pass,original_pass) == 0) {
        Serial.println("success 😎 ");
    
        digitalWrite(ledPinSuc, HIGH);
        digitalWrite(buzzerPin, HIGH);
        
        // command to move from 0 degrees to 180 degrees
        for(angle = 0; angle < 180; angle += 1){
          servo_test.write(angle);
          delay(15);
        }
        delay(2000);
        digitalWrite(ledPinSuc, LOW);
        digitalWrite(buzzerPin, LOW);
        
        // command to move from 180 degrees to 0 degrees
        for(angle = 180; angle >= 1; angle -= 5){
          servo_test.write(angle);
          delay(5);
        }
      }else{ //failed authenication, flash the error LED, beep buzzer 2x 
        Serial.println("wrong 😉 ");
        digitalWrite(ledPinErr, HIGH);
        digitalWrite(buzzerPin, HIGH);
        delay(55);
        digitalWrite(buzzerPin, LOW);
        delay(55);
        digitalWrite(buzzerPin, HIGH);
        delay(55);
        digitalWrite(buzzerPin, LOW);
        delay(2000);
        digitalWrite(ledPinErr, LOW);
      }
    }
  else Serial.println("no action");
} //if not no_key


delay(100);

} //loop

 

Explanation

  • the 8 pin outs from the keypad is connected to pins D2 to D9 on the Arduino
  • the servo motor is connected to pins D12 (for data), 5V and GND on the Arduino
  • the two leds (green and yellow) is connected to D10 and D11 on the Arduino
  • the Buzzer is attached to pin D13
  • the green led indicates successful authentication and yellow indicates failed/wrong authentication.
  • the buzzer gives one long beep on successful authentication and servo motor moves from 0deg to 180deg (indicating opened door, after 2 seconds it returns to 0deg (indicating closing).
  • the buzzer gives two short beep on failed authentication or wrong password.

 

keypad_arduino_serial - steinacoz
serial monitor

Feel free to add your comments about these tutorial and anticipate for the next tutorial.

 

Leave a Reply

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