TechTorch

Location:HOME > Technology > content

Technology

Building a Door Locking System with Keypad using PIC Microcontroller

April 26, 2025Technology4356
Building a Door Locking System with Keypad using PIC Microcontroller C

Building a Door Locking System with Keypad using PIC Microcontroller

Creating a door locking system with a keypad using a PIC microcontroller is both an intellectually stimulating and practical mini project. This guide provides detailed steps to help you build this system from scratch, ensuring comprehensive coverage of each component and coding process.

Components Required

PIC Microcontroller: e.g. PIC16F877A 4x4 Matrix Keypad Relay Module for controlling the door lock Buzzer for feedback LEDs for status indication Power Supply appropriate for your PIC and relay Breadboard and Jumper Wires

Step 1: Circuit Design

Keypad Connection: Connect the rows and columns of the 4x4 keypad to the GPIO pins of the PIC microcontroller.

Relay Module: Connect the control pin of the relay module to another GPIO pin on the PIC. This relay will control the door lock.

Buzzer and LED: Connect the buzzer to a GPIO pin for sound feedback. Connect an LED to indicate when the door is locked or unlocked.

Step 2: Write the Code

Basic Code Structure: Start by including necessary header files for the PIC microcontroller, defining constants, and initializing GPIO pins for input (keypad) and output (relay, buzzer, LED).

Read Keypad Input: Implement a function to scan and read the keypad inputs.

Password Verification: Create a function to compare the entered password with the stored correct password.

Control Relay: If the password is correct, activate the relay to unlock the door. If incorrect, trigger the buzzer.

Sample Code in C:

#include xc.h// Define configuration bits and other necessary settings here#define RELAY_PIN LATB0#define BUZZER_PIN LATB1#define LED_PIN LATB2// Define the correct passwordconst char correct_password[]  "1234";char entered_password[4];int password_index  0;// Function to initialize GPIOvoid init_gpio() {    TRISB  0; // Set PORTB as output    // Initialize other ports as necessary}// Function to read from keypadchar read_keypad() {    // Implement keypad scanning logic    // Return the pressed key}// Function to check passwordint check_password() {    for (int i  0; i  4; i  ) {        if (entered_password[i] ! correct_password[i]) {            return 0; // Password incorrect        }    }    return 1; // Password correct}// Main functionvoid main() {    init_gpio();    while (1) {        char key  read_keypad();        if (key) {            entered_password[password_index]  key;            if (password_index  4) {                if (check_password()) {                    RELAY_PIN  1; // Unlock door                    LED_PIN  1; // Indicate unlocked                } else {                    BUZZER_PIN  1; // Sound buzzer                }                password_index  0; // Reset index            }        }    }}

Step 3: Testing

Upload the Code: Use a programmer to upload your code to the PIC microcontroller.

Test the System: Enter the password using the keypad and check if the relay activates to unlock the door.

Debug: If the system does not work as expected, check the wiring and debug your code.

Step 4: Finalization

Once testing is complete, you can solder the components onto a PCB for a more permanent solution. Consider adding features such as a timeout for incorrect attempts, an LCD for displaying messages, or additional security measures.

Conclusion

This project combines hardware and software skills and can be expanded with additional features like remote access or logging attempts. Good luck with your mini project! If you have specific questions about any part of the process, feel free to ask!