Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
In this article, we will explore the concept of keypad interface and its importance in various electronic projects. Keypads are widely used in applications that require user input, such as security systems, access control systems, and digital locks. By connecting a keypad to an Arduino, we can easily read user input and perform actions based on the entered values.
To align the keypad with the Arduino environment, we will utilize the Keypad library. This library provides a simple and efficient way to interface with a keypad, allowing us to easily read key presses and perform actions accordingly.
Project: For this example project, we will create a simple door lock system using a 4x4 matrix keypad and an Arduino. The objective of this project is to allow users to enter a predefined passcode using the keypad. If the entered passcode matches the predefined one, the Arduino will activate a relay module to unlock the door. Otherwise, an incorrect password message will be displayed.
Components List:
Examples:
Wiring the Components: Before we start coding, let's connect the components. Connect the rows of the keypad to digital pins 2 to 5 of the Arduino, and connect the columns to digital pins 6 to 9. Connect the relay module to a suitable digital pin of the Arduino.
Installing the Keypad Library: Open the Arduino IDE, go to "Sketch" -> "Include Library" -> "Manage Libraries". Search for "Keypad" and click on "Install" to install the library.
Initializing the Keypad: To use the keypad, we need to include the Keypad library and define the keypad layout. Here's an example code snippet:
#include <Keypad.h>
const byte ROWS = 4; // Number of rows in the keypad
const byte COLS = 4; // Number of columns in the keypad
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; // Connect the row pins to these Arduino pins
byte colPins[COLS] = {6, 7, 8, 9}; // Connect the column pins to these Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void loop() {
char key = keypad.getKey();
if (key) {
// Key is pressed
// Perform actions based on the pressed key
// For example, check if the entered passcode matches the predefined one
// If yes, activate the relay module to unlock the door
// If no, display an incorrect password message
}
}
#include <Keypad.h>
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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const String passcode = "1234"; // Predefined passcode
const int relayPin = 10; // Digital pin connected to the relay module
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
char key = keypad.getKey();
if (key) {
// Key is pressed
if (key == '#') {
// Check if entered passcode matches the predefined one
if (keypad.getPassword() == passcode) {
// Correct passcode, unlock the door
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
} else {
// Incorrect passcode, display error message
Serial.println("Incorrect password!");
}
}
}
}