Keypad- Password Varification

About the Project

In this project, we will learn how to verify a password entered via the keypad and display whether it is correct or incorrect on the Serial Monitor.

Keypad

A basic keypad consists of button switches arranged in a matrix of rows and columns. When a key is pressed, it creates a connection between a specific row and column, which the microcontroller detects to determine which key was pressed.

Circuit Wiring

Installation of Keypad Library

Before uploading the program to the Arduino board, we need to install the Keypad library and include the corresponding header file in the program. The screenshot below illustrates this process.

  • Click on the Sketch menu at the top.
  • Select Include Library from the dropdown menu.
  • Click on Manage Libraries… at the top of the list. This will open the Library Manager.
  • In the Library Manager window, there is a search bar in the upper right corner.
  • Type Keypad in the search bar and press Enter.
  • Look for the library named “Keypad by Mark Stanley, Alexander Brevig”.
  • Click on the “Install” button next to the library. The installation process will start.
  • Once installed, you will see “INSTALLED” next to the Keypad library.

Program Code

C++
// www.matthewtechub.com
//  keypad- password
#include <Keypad.h>

// Define the number of rows and columns in the keypad
const byte ROWS = 4;
const byte COLS = 4;

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

// Connect keypad ROW0, ROW1, ROW2, ROW3 to these Arduino pins
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

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

// Predefined password
String correctPassword = "2586";

// Variable to store the entered password
String enteredPassword = "";

void setup() {
  Serial.begin(9600); // Start serial communication
  Serial.println("Enter your password:"); // Prompt the user to enter the password
}

void loop() {
  char key = keypad.getKey(); // Get the key pressed

  if (key) { // If a key is pressed
    if (key == '*') { // If '*' is pressed, clear the entered password
      enteredPassword = "";
      Serial.println("Password cleared. Enter your password:");
    } else {
      enteredPassword += key; // Append the pressed key to the entered password
      Serial.print(key); // Display the pressed key

      if (enteredPassword.length() == 4) { // Check if the entered password length is 4
        if (enteredPassword == correctPassword) {
          Serial.println("\nCorrect password"); // Password matched
        } else {
          Serial.println("\nIncorrect password"); // Password did not match
        }
        enteredPassword = ""; // Clear the entered password for the next attempt
        Serial.println("Enter your password:"); // Prompt for another attempt
      }
    }
  }
}

The code sets up a matrix keypad with Arduino, allows the user to input a 4-digit password, and verifies it against a predefined password. It also provides feedback via the Serial Monitor, showing whether the entered password is correct or incorrect.

Code Explanation

C++
#include <Keypad.h>

This line includes the `Keypad` library, which provides functions for reading input from a matrix keypad.

C++
const byte ROWS = 4;
const byte COLS = 4;
  • `ROWS` and `COLS` specify the number of rows and columns in your 4×4 keypad.
C++
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
  • `keys` is a 2D array defining the layout of the keypad. Each element corresponds to a key on the keypad.
C++
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
  • `rowPins` and `colPins` specify which Arduino pins are connected to the rows and columns of the keypad.
C++
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  • This creates a `Keypad` object with the specified keymap and pin connections.
C++
String correctPassword = "2586";

String enteredPassword = "";
  • `correctPassword` stores the predefined password.
  • `enteredPassword` is used to collect the user input.
C++
void setup() {
  Serial.begin(9600); // Start serial communication at 9600 baud
  Serial.println("Enter your password:"); // Prompt the user to enter the password
}
  • Initializes serial communication for debugging and user interaction.
  • Prints a prompt for the user to enter the password.
C++
void loop() {
  char key = keypad.getKey(); // Read the key pressed
  • Continuously checks for any keypress on the keypad.
C++
 if (key) { // If a key is pressed
  • Checks if a key was actually pressed.
C++
    if (key == '*') { // If '*' is pressed, clear the entered password
      enteredPassword = "";
      Serial.println("Password cleared. Enter your password:");
    } else {
      enteredPassword += key; // Append the pressed key to the entered password
      Serial.print(key); // Display the pressed key
  • If * is pressed, it clears the enteredPassword and prompts the user again.
  • Otherwise, it appends the pressed key to the enteredPassword and prints the key.
C++
      if (enteredPassword.length() == 4) { // Check if the entered password length is 4
        if (enteredPassword == correctPassword) {
          Serial.println("\nCorrect password"); // Password matched
        } else {
          Serial.println("\nIncorrect password"); // Password did not match
        }
        enteredPassword = ""; // Clear the entered password for the next attempt
        Serial.println("Enter your password:"); // Prompt for another attempt
      }
    }
  }
}
  • Once four keys are entered, it checks if the enteredPassword matches the correctPassword.
  • Prints “Correct password” or “Incorrect password” based on the match.
  • Clears enteredPassword for a new attempt and prompts the user again.

Screenshot

Try Yourself

Modify the program code so that there is no option to delete characters while entering the password. Note that in the current program, there is a delete option (using the ‘*’ key), allowing you to delete password characters even after entering them.

Leave a Reply

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

error: Content is protected !!