We know that security is the key point of every project. So, if you want to give security to your system keypad is the cheapest way to provide password protection. In this tutorial first, we will learn how to interface a keypad with Arduino. Then we will code to ON an LED after successfully entering the correct password   


You can use this tutorial to build your project which needs to integrate password protection.

Projects that you can build with:

  • Password Protected DC motor control.
  • Password Protected device control.
  • Door Lock System with Arduino.
  • Electronic Safe with Arduino. etc.


4x4 Membrane KeyPad:


Hardware Requirements:

  • Arduino UNO
  • 4x4 keypad.
  • 16x2 LCD
  • I2C LCD module

 

Software Requirements:

  • Arduino IDE (You can download it from HERE) 
  • Keypad Library (Download it HERE)
  • I2C library (Download it HERE)
  • Wire Library (inbuilt within the IDE).

Block diagram:



Circuit diagram: Follow the circuit diagram and the below table to connect component correctly.


Connection Keypad and Arduino:

KeypadArduino UNO
C1, C2, C3 and C47, 6, 5 and 4 respectively
R1, R2, R3 and R48, 9, 10 and 11 respectively

 

Connection I2C LCD and Arduino:

I2C ModuleArduino UNO
VCC5v
GND GND
SDAA4 (SDA)
SCL A5 (SCL)

 

Full source code:

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h> 

#define Password_Lenght 5 // Give enough room for FOUR chars + NULL char

LiquidCrystal_I2C lcd(0x3F,16,2);
char Data[Password_Lenght]; // 4 is the number of chars it can hold + the null char = 5
char Master[Password_Lenght] = "5599"; 
byte data_count = 0, master_count = 0;
char customKey;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad 

void setup()
{
  lcd.begin();// initialize the lcd 
  lcd.backlight();
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}

void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Enter Password");
  customKey = customKeypad.getKey();
  digitalWrite(13,LOW);
  delay(50);
  if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  {
    Data[data_count] = customKey; // store char into data array
    lcd.setCursor(data_count+3,1); // move cursor to show each new char
    lcd.print("*"); // print * in place of actual char at said cursor
    data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
  }

  if(data_count == Password_Lenght-1) // if the array index is equal to the number of expected chars, compare data to master
  {
    lcd.clear();
    if(!strcmp(Data, Master)){ // equal to (strcmp(Data, Master) == 0)
       lcd.setCursor(0, 0);
       lcd.print(" Welcome to My  ");
       lcd.setCursor(0, 1);
       lcd.print(" YT Channel MES ");
       digitalWrite(13,HIGH);
    }
    else{
       lcd.setCursor(0, 0);
       lcd.print(" Access Denied  ");
       lcd.setCursor(0, 1);
       lcd.print("    SORRY!!!    ");
       digitalWrite(13,LOW);
      }
    delay(3000);// added 1 second delay to make sure the password is completely shown on screen before it gets cleared.
    lcd.clear();
    clearData();   
  }
}

void clearData()
{
  while(data_count !=0)
  {   // This can be used for any array size, 
    Data[data_count--] = 0; //clear array for new data
  }
  return;
}

 

Youtube video tutorial: