About the Project
In this project, we will learn how to verify a password entered via the keypad. When the correct password is entered, an LED will turn on, and the information will also be displayed 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
// www.matthewtechub.com
// keypad - password- led
#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 = "";
// Define the LED pin
const int ledPin = 13; // You can change this to any digital pin
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
digitalWrite(ledPin, LOW); // Ensure the LED is off at the start
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
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
Serial.println("\nIncorrect password"); // Password did not match
digitalWrite(ledPin, LOW); // Ensure the LED is off if the password is incorrect
}
enteredPassword = ""; // Clear the entered password for the next attempt
Serial.println("Enter your password:"); // Prompt for another attempt
}
}
}
}
This code reads input from a 4×4 matrix keypad, verifies if the entered password matches a predefined password, and controls an LED based on whether the password is correct. The status is also displayed on the Serial Monitor.
.
Code Explanation
#include <Keypad.h>
- The `Keypad` library is included to easily manage the input from the matrix keypad.
const byte ROWS = 4;
const byte COLS = 4;
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};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
- The keypad is configured with 4 rows and 4 columns.
- `keys` array maps the keypad buttons to their respective characters.
- `rowPins` and `colPins` define the Arduino pins connected to the keypad rows and columns.
- A `Keypad` object is created to manage keypad input.
String correctPassword = "2586";
String enteredPassword = "";
- `correctPassword` is the password the user must enter to turn on the LED.
- `enteredPassword` stores the user’s input.
const int ledPin = 13;
- The LED is connected to pin 13, but this can be changed to any digital pin.
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.println("Enter your password:");
}
- Serial communication is initialized for debugging and user interaction.
- The LED pin is set as an output, and the LED is initially turned off.
- A prompt is displayed on the Serial Monitor asking the user to enter the password.
void loop() {
char key = keypad.getKey();
- The `loop` function runs continuously, checking for key presses on the keypad.
if (key) {
if (key == '*') {
enteredPassword = "";
Serial.println("Password cleared. Enter your password:");
} else {
enteredPassword += key;
Serial.print(key);
- If a key is pressed, it is added to `enteredPassword`.
- If the `’*’` key is pressed, it clears the `enteredPassword` and prompts the user to enter the password again.
if (enteredPassword.length() == 4) {
if (enteredPassword == correctPassword) {
Serial.println("\nCorrect password");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("\nIncorrect password");
digitalWrite(ledPin, LOW);
}
enteredPassword = "";
Serial.println("Enter your password:");
}
- Once four keys are entered, the program checks if the `enteredPassword` matches the `correctPassword`.
- If the passwords match, it turns on the LED and displays “Correct password” on the Serial Monitor.
- If the passwords do not match, it turns off the LED and displays “Incorrect password.”
- After checking, the `enteredPassword` is cleared, and the user is prompted to enter the password again.
This program allows you to enter a password via a keypad. If the password matches the predefined one, an LED turns on, and a message is displayed on the Serial Monitor. If the password is incorrect, the LED remains off, and an error message is shown.