CIPAD en pause. Sujet Clavier matriciel, mots de passe multi usagers
Table of Contents
Introduction
This tutorial aims to guide you through the basics of using a 4x4 matrix keypad with an LCD display in Arduino projects. It covers three distinct projects, including password protection and user management, which are essential for creating interactive electronic systems.
Step 1: Setting Up the Matrix Keypad and LCD Display
To begin, you will need to set up your hardware components, including a 4x4 matrix keypad and an LCD display.
Equipment Required
- Arduino board (e.g., Arduino Uno)
- 4x4 matrix keypad
- LCD display (with I2C module if preferred)
- Jumper wires
- Breadboard (optional)
Wiring Instructions
- Connect the keypad to the Arduino:
- Rows to digital pins (e.g., 2-5)
- Columns to digital pins (e.g., 6-9)
- Connect the LCD to the Arduino:
- If using I2C, connect SDA to A4 and SCL to A5.
- Connect VCC to 5V and GND to ground.
Practical Tips
- Use a breadboard for easier connections.
- Double-check connections to avoid short circuits.
Step 2: Programming the Keypad to Display Inputs
Now, you will write the code to recognize key presses and display the corresponding characters on the LCD.
Essential Libraries
Include the following libraries at the beginning of your code:
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
Code Structure
-
Initialize the keypad and LCD:
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); LiquidCrystal_I2C lcd(0x27, 16, 2);
-
In the
loop()
function, read input and display it:void loop() { char key = keypad.getKey(); if (key) { lcd.clear(); lcd.print(key); } }
Common Pitfalls
- Ensure the library versions are compatible with your Arduino IDE.
- Verify the I2C address of your LCD if it's not displaying correctly.
Step 3: Implementing Password Protection
In this step, you'll create a simple password protection system using the keypad.
Password Logic
- Define a password as a string (e.g., "1234").
- Store user input in another string and compare it to the password.
Example Code Snippet
String password = "1234";
String inputPassword = "";
void loop() {
char key = keypad.getKey();
if (key) {
inputPassword += key;
lcd.clear();
lcd.print(inputPassword);
if (inputPassword.length() == 4) { // Assuming 4-digit password
if (inputPassword == password) {
lcd.print("Access Granted");
} else {
lcd.print("Access Denied");
}
inputPassword = ""; // Reset for next input
}
}
}
Practical Tips
- Allow a reset option if the user enters the wrong password multiple times.
- Consider adding a timeout feature to enhance security.
Step 4: Multi-User Password Management
This project introduces managing multiple users, each with their own password.
Implementation Steps
- Create a structure to store usernames and passwords (e.g., using arrays or a hash map).
- Modify the input logic to match against multiple passwords.
Example Structures
String usernames[9] = {"User1", "User2", "User3", "User4", "User5", "User6", "User7", "User8", "User9"};
String passwords[9] = {"pass1", "pass2", "pass3", "pass4", "pass5", "pass6", "pass7", "pass8", "pass9"};
Common Pitfalls
- Ensure the input system is user-friendly to avoid confusion.
- Provide clear feedback for successful or failed attempts.
Conclusion
In this tutorial, you learned how to set up a matrix keypad with an LCD, display inputs, and implement password protection for a single user and multiple users. These foundational skills can be applied to various Arduino projects, enhancing interactivity and security. For further exploration, consider expanding your projects with additional features like user identification or logging access attempts. Happy coding!