4×4 keypad with Arduino password based security system.

4x4 keypad arduino door lock

Keypads are among the commonest ways of interacting with microcontrollers. They can be found in a majority of electronic devices like door locks, phones, calculators, microwave ovens and so many more. They help us to navigate menus, enter passwords and control games and robots.

Keypads come in wide variety of shapes and sizes. The most commons sizes for simple microcontroller projects are 3×4 and 4×4 and they come with words, letters and numbers written on the keys. You can even make your own keypad if you understand how these pads work.

In this tutorial we are going to show how the 4×4 keypad is interfaced with Arduino and how this keypad can be applied in making a simple password based door lock system.

You can also check out my other tutorials on Arduino based door lock systems using RFID and Fingerprints from the links below;

How keypads work.

Keypads are simply switches arranged in matrix form. This arrangement reduces the number of I/O pins needed for interfacing the switches. For example for a 4×4 keypad, if you use 16 individual push buttons, you would require 17 input pins (one for each key and a ground pin) in order to make them work. However, with matrix arrangement, you only need 8 microcontroller pins (4-columns and 4-rows) to scan through the pad.

Working Logic

The working principle is very simple. Pressing a button shorts one of the row lines to one of the column lines, allowing current to flow between them. A microcontroller can scan these lines for a button-pressed state.

Connecting 4×4 membrane keypad with Arduino.

The connection is very simple. Just connect the keypad output pins to any digital pins of the Arduino. However it’s better if the pins are connected in order for example I have used pin 2 to pin 5 for the columns and pin 6 to pin 9 for the rows of the keypad matrix.
An I2C LCD has also been connected to display a message whenever a key is pressed.

4x4 keypad with arduino schematic

Code for 4×4 keypad interfacing with Arduino

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
//I2C pins declaration
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//Create an object of keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  lcd.begin(16,2);//Defining 16 columns and 2 rows of lcd display
  lcd.backlight();//To Power ON the back light
}
  
void loop(){
  lcd.setCursor(0,0); //Defining positon to write from first row,first column .
  lcd.print("PRESS ANY KEY"); //You can write 16 Characters per line .
   
  char key = keypad.getKey();// Read the key
  lcd.setCursor(0,1);  //Defining positon to write from second row,first column .
  lcd.print("Key Pressed: ");
  // Print if key pressed
  if (key){
    lcd.print(key);
  }
}    

Keyword based Door lock system.

After learning how the 4×4  keypad with Arduino works, we can now demonstrate how this keypad can be used to make a simple keyword based door lock. The setup is as shown below.We have only added LEDs to show the state of the lock.

4x4 keypad arduino schematic

We have used EEPROM in the Arduino to store the password in it. The default password stored in it will be ‘1234’. When we enter a password, it will match it with the password stored in the Arduino EEPROM. If it is correct, then it will show ‘Passkey Accepted’. The green LED will also light. If the password is wrong, then it will show ‘Access Denied’ and the red LED will light.

For changing the passkey, we have to press ‘#’. When we press ‘#’, it will ask for current passkey. If we enter the correct password it will ask for new passkey and will save it in the EEPROM.

Code for working of password based 4×4 keypad door lock.

#include<Keypad.h>
#include<LiquidCrystal.h>
#include<EEPROM.h>
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);
char password[4];
char pass[4],pass1[4];
int i=0;
char customKey=0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {1,2,3,4}; //row pinouts of the keypad
byte colPins[COLS] = {5,6,7,8}; //column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int redLed = 10;
int greenLed = 11;
void setup()
{
lcd.begin(16,2);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
lcd.print(" WELCOME... ");
lcd.setCursor(0,1);
lcd.print(" Keypad Lock ");
delay(2000);
lcd.clear();
lcd.print("Enter password:");
lcd.setCursor(0,1);
for(int j=0;j<4;j++)
EEPROM.write(j, j+49);
for(int j=0;j<4;j++)
pass[j]=EEPROM.read(j);
}
void loop()
{
//digitalWrite(11, HIGH);
customKey = customKeypad.getKey();
if(customKey=='#')
change();
if (customKey)
{
password[i++]=customKey;
lcd.print(customKey);
}
if(i==4)
{
delay(200);
for(int j=0;j<4;j++)
pass[j]=EEPROM.read(j);
if(!(strncmp(password, pass,4)))
{
digitalWrite(greenLed, HIGH);
lcd.clear();
lcd.print("Passkey Accepted");
delay(2000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("PRESS # TO CHANGE PASSWORD.");
for(int PositionCount=0;PositionCount<80; PositionCount++)//loop for scrolling the LCD text
{
lcd.scrollDisplayLeft();//builtin command to scroll left the text
delay(150);// delay of 150 msec
}
delay(3000);
lcd.clear();
lcd.print("Enter Passkey:");
lcd.setCursor(0,1);
i=0;
digitalWrite(greenLed, LOW);
}
else
{
digitalWrite(redLed, HIGH);
lcd.clear();
lcd.print("Access Denied...");
lcd.setCursor(0,1);
lcd.print("#.Change Passkey");
delay(3000);
lcd.clear();
lcd.print("Enter right key:");
lcd.setCursor(0,1);
i=0;
digitalWrite(redLed, LOW);
}
}
}
void change()
{
int j=0;
lcd.clear();
lcd.print("Enter Current Key");
lcd.setCursor(0,1);
while(j<4)
{
char key=customKeypad.getKey();
if(key)
{
pass1[j++]=key;
lcd.print(key);
}
key=0;
}
delay(500);
if((strncmp(pass1, pass, 4)))
{
lcd.clear();
lcd.print("Wrong Passkey...");
lcd.setCursor(0,1);
lcd.print("Enter Right Key");
delay(1000);
}
else
{
j=0;
lcd.clear();
lcd.print("Enter New Key:");
lcd.setCursor(0,1);
while(j<4)
{
char key=customKeypad.getKey();
if(key)
{
pass[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;
}
}
lcd.print(" Done......");
delay(1000);
}
lcd.clear();
lcd.print("Enter your key:");
lcd.setCursor(0,1);
customKey=0;
}