How to Implement Keypad Password System in Embedded C
To implement a keypad password system in
Embedded C, read keypad inputs by scanning rows and columns, store the entered digits, and compare them against a predefined password. Use simple loops and conditional checks to verify the password and trigger actions like unlocking or error messages.Syntax
The basic syntax involves initializing the keypad pins, scanning the keypad matrix to detect pressed keys, storing the input in an array, and comparing it with the stored password.
- Initialize Pins: Set rows as outputs and columns as inputs with pull-ups.
- Scan Keypad: Drive rows low one by one and read columns to detect key press.
- Store Input: Save pressed keys in a buffer array.
- Compare Password: Check if input matches the stored password.
c
void keypad_init(void) { // Configure row pins as output // Configure column pins as input with pull-up } char keypad_get_key(void) { // Loop through rows // Set one row low at a time // Read columns to detect pressed key // Return detected key character or 0 if none return 0; // Added return to avoid compiler warning } void read_password(char *input_buffer, int length) { for (int i = 0; i < length; i++) { char key = 0; while (key == 0) { key = keypad_get_key(); } input_buffer[i] = key; } } int check_password(char *input, char *password, int length) { for (int i = 0; i < length; i++) { if (input[i] != password[i]) return 0; } return 1; }
Example
This example shows a simple 4x4 keypad password system that reads a 4-digit password and checks if it matches "1234". It prints "Access Granted" or "Access Denied" accordingly.
c
#include <stdio.h> #include <string.h> // Simulated keypad input for demonstration char simulated_keys[] = {'1', '2', '3', '4'}; int key_index = 0; char keypad_get_key(void) { if (key_index < 4) { return simulated_keys[key_index++]; } return 0; } void read_password(char *input_buffer, int length) { for (int i = 0; i < length; i++) { char key = 0; while (key == 0) { key = keypad_get_key(); } input_buffer[i] = key; } } int check_password(char *input, char *password, int length) { for (int i = 0; i < length; i++) { if (input[i] != password[i]) return 0; } return 1; } int main() { char password[4] = {'1', '2', '3', '4'}; char input[4]; read_password(input, 4); if (check_password(input, password, 4)) { printf("Access Granted\n"); } else { printf("Access Denied\n"); } return 0; }
Output
Access Granted
Common Pitfalls
Common mistakes when implementing keypad password systems include:
- Not debouncing keys, causing multiple detections of a single press.
- Incorrectly configuring input/output pins leading to no key detection.
- Not waiting for key release before reading the next key, causing repeated inputs.
- Buffer overflow by entering more keys than the password length.
Always debounce keys by adding a small delay after detection and wait for key release before continuing.
c
/* Wrong: No debounce and no key release wait */ char keypad_get_key_wrong(void) { // Directly read key without delay or release check return detected_key; } /* Right: Simple debounce and wait for release */ char keypad_get_key_right(void) { char key = 0; while (key == 0) { key = scan_keypad(); } _delay_ms(20); // debounce delay while (scan_keypad() != 0) { ; // wait for key release } return key; }
Quick Reference
Keypad Password System Tips:
- Initialize rows as outputs and columns as inputs with pull-ups.
- Scan keypad by driving rows low one at a time and reading columns.
- Store pressed keys in a buffer and compare with stored password.
- Debounce keys with small delays and wait for release.
- Limit input length to password size to avoid overflow.
Key Takeaways
Scan keypad rows and columns to detect pressed keys accurately.
Store user input in a buffer and compare it with the predefined password.
Always debounce keys and wait for key release to avoid multiple detections.
Limit input length to prevent buffer overflow and errors.
Configure microcontroller pins correctly for keypad interfacing.