3x4 keypad connected to arduino

Keypad Input and Access Control for Locks with LCD | Arduino Tutorial

Keypad access control is an easy means to restrict access to unwanted visitors and has the advantage of not requiring a physical key. Just don’t forget the pin code.

Today we will build a keypad access control with an LCD feedback. We will briefly look at the types of keypads available, then we’ll build a keypad access system using an Arduino board and a few extra components. So let’s dive in.

 

 

What is Keypad Access Control ?

Keypad access control is commonly found to control electric door locks, safe doors, electric gates, alarm system panels and anywhere where keyless entry is required.

Generally a 4 digital code is required but for further security you can have a larger pin code with 6 or however many digits you can remember! The more pins number you use, the more secure.

Keypads generally come in two flavors , a series of direct switches or a switch matrix. They both have their pros and cons which we will go through now.

 

Types of Keypad – Switches Vs Scan Matrix Keypads ?

 

A keypad is just a series of switches, however, the simplest form has one wire per switch. So a 3×4 keypad,  that is 3 columns by 4 rows will use 13 wires including the earth wire.A 4×4 keypad will use 17 wires including the earth wire.

 

keypad diagram with wire connections

 

The other type of keypad is the scan matrix, which is also a series of switches but requires extra code to scan the rows and columns. This has the advantage of using less wires, so a 3×4 key pad will only require 7 wires (4×4 keypad will require 8 wires), but require more code for the scanning algorithm.

 

scan matrix keypad

 

Also, with the two types of keypads, we can use a BCD encoder chip or binary coded decimal encoder. This saves on code for both types of keypads as the chip does the encoding of button presses into a binary number, but like with all things, the trade off is that more hardware is required.

Diagram how a BDC encoder connects between a microcontroller and a keypad

Build a Keypad Access Control with LCD using Arduino Board

So now that you have a better idea of the keypad types and requirements, we will build a keypad access system that uses a simple direct switch keypad to input a 4 digit pin. We also make the code robust so that if the wrong pin is entered 3 times in a row the user is locked out for a period of time before the pin can be re-entered.

 

Components Required to build a Keypad Access Control with LCD

  • LCD display
  • Direct switch 3×4 keypad
  • Arduino uno
  • Bread board
  • 10x 38k ohm resistor (can be any value between 10k – 100k)
  • 10k variable resistor
  • hookup wire.

 

Building the circuit

 

We wire up the circuit as per the schematic diagram below.

 

schematic of arduino, lcd and keypad

 

Programming the Keypad Control using an Arduino Uno Board

As we are using a direct switch keypad and we are only using the 0-9 buttons and not the * or # buttons we only require 11 wires including the ground wire.

We use a scanning function called getNumber() to detect button presses, where each button press is stored in a variable. We then use an if statement to determine if the code entered is correct. If the entered code is infact correct a message is display that says “Correct Code”.

On the other hand, if the code entered is incorrect, an error message is display and the number of incorrect entries is counted. Once the code has been entered incorrectly 3 times in a row the user is locked out from entering the code again for a period of time. Once the correct code is entered the incorrect code counter is reset back to 0.

 

#include <LiquidCrystal.h>

int input0 = A5;
int input1 = A4;
int input2 = 2;
int input3 = 3;
int input4 = 4;
int input5 = 5;
int input6 = 6;
int input7 = 7;
int input8 = 8;
int input9 = 9;
int figure = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int code1 = 1;
int code2 = 2;
int code3 = 3;
int code4 = 4;
int attempts = 0;

LiquidCrystal lcd(A1,A0,10,11,12,13);

void setup() {
lcd.begin(16,2);
digitalWrite(input0, LOW);

pinMode(input0, INPUT);
pinMode(input1, INPUT);
pinMode(input2, INPUT);
pinMode(input3, INPUT);
pinMode(input4, INPUT);
pinMode(input5, INPUT);
pinMode(input6, INPUT);
pinMode(input7, INPUT);
pinMode(input8, INPUT);
pinMode(input9, INPUT);

}

int getNumber()
{
  a: if (digitalRead(input0) == HIGH)
          {
           return 0;
          }       
          
     if (digitalRead(input1) == HIGH)
          {
            return 1;
          }
              
    if (digitalRead(input2) == HIGH)
          {
            return 2;
          }
          
    if (digitalRead(input3) == HIGH)
          {
            return 3;
          }
         
    if (digitalRead(input4) == HIGH)
          {
            return 4;
          }
          
    if (digitalRead(input5) == HIGH)
          {
            //Serial.print(“*” );
            return 5;
          }
    
    if (digitalRead(input6) == HIGH)
          {
            return 6;
          }

    if (digitalRead(input7) == HIGH)
          {
            return 7;
          }

    if (digitalRead(input8) == HIGH)
          {
            //Serial.print(“*” );
            return 8;
          }
          
    if (digitalRead(input9) == HIGH)
          {
            return 9;
          }          
          
    delay(200);
    
    goto a;  
}

void loop() {

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Enter Code:”);

delay(200);
num1 = getNumber();
lcd.setCursor(6,1);
lcd.print(“*”);
delay(500);
num2 = getNumber();
lcd.print(“*”);
delay(500);
num3 = getNumber();
lcd.print(“*”);
delay(500);
num4 = getNumber();
lcd.print(“*”);
delay(500);

if (num1 == code1 && num2 == code2 && num3 == code3 && num4 == code4)
  {
    attempts = 0;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(“Correct Code !”);
    delay (1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(“Entry Authorized”);
    delay (5000);
  }

else

  {
    Serial.print (“incorrect \n”);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(“**INVALID CODE**”);
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(“Enter Code:”);
    attempts ++;
  }

if (attempts >= 3)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(“ERROR-Locked Out”);
        
    for (int i = 0; i <= 5; i++)
      {
      lcd.setCursor(7,1);
      lcd.print(i);
      delay(1000);
      }
      
    attempts = 0;  
   
  }

delay (500);
 
  }

 

Keypad Input and Access Control for Locks with LCD| Summary

So, there you go, we’ve built a keypad access control system. If you wanted to, you could further develop it to switch a relay from a pin output that would unlock an electric door lock when the correct code is entered.

I hope this tutorial was useful to you.

 

 

If you liked the tutorial, Please share it with a friend !

 

Please also Subscribe to the Smart Microcontroller Channel to be the first to know when my latest electronics videos are released !