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 how to interface a keypad with an Arduino. Keypads are essential input devices commonly used in applications such as security systems, automated gates, and user interfaces for various electronic projects. By interfacing a keypad with an Arduino, you can capture user input and trigger specific actions based on the entered data. This tutorial will guide you through the process of connecting a keypad to an Arduino, writing the necessary code, and understanding how to handle the keypad input effectively.
Project: In this project, we will create a simple security system using a 4x4 keypad and an Arduino. The objective is to enter a predefined security code using the keypad, and upon entering the correct code, an LED will light up to indicate successful access. If the wrong code is entered, the system will reset, and the user will have to try again. This project will help you understand how to read inputs from a keypad and use them to control other components.
Components List:
Examples:
Here is the code to interface a 4x4 keypad with an Arduino and implement a basic security system:
#include <Keypad.h> // Include the Keypad library
// Define the number of rows and columns in the keypad
const byte ROWS = 4;
const byte COLS = 4;
// Define the keymap for 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};
// Connect keypad COL0, COL1, COL2, COL3 to these Arduino pins
byte colPins[COLS] = {5, 4, 3, 2};
// Create the Keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const String correctCode = "1234"; // Define the correct security code
String inputCode = ""; // Variable to store the entered code
const int ledPin = 13; // Pin for the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.begin(9600); // Initialize serial communication
}
void loop() {
char key = keypad.getKey(); // Get the key pressed on the keypad
if (key) { // If a key is pressed
Serial.println(key); // Print the key to the serial monitor
inputCode += key; // Append the key to the input code
if (inputCode.length() == 4) { // If 4 digits have been entered
if (inputCode == correctCode) { // Check if the entered code matches the correct code
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Access Granted");
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("Access Denied");
}
inputCode = ""; // Reset the input code
}
}
}
Explanation:
Keypad.h
library is included to handle the keypad interface.Keypad
object is created using the defined keymap and pin assignments.keypad.getKey()
function is used to detect key presses. The entered key is appended to the inputCode
string. When four digits are entered, the code is checked against the predefined correctCode
. If they match, the LED turns on, indicating access granted; otherwise, it remains off, indicating access denied. The inputCode
is then reset for the next attempt.