Build a Smart Lock System

Difficulty Level: Intermediate

This project demonstrates how to create a smart lock system using an Arduino, a servo motor, and a keypad. You can unlock the system by entering a predefined code.

Components Required

Wiring the Circuit

Connect the components to the Arduino as follows:

Code Explanation

The following code enables the smart lock to unlock when the correct code is entered.

Arduino Code


#include 
#include 

// Define the keypad layout
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
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 to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo lockServo;

const String code = "1234"; // Predefined code
String inputCode;

void setup() {
    lockServo.attach(10); // Attach the servo on pin 10
    lockServo.write(0); // Initial position (locked)
    Serial.begin(9600);
}

void loop() {
    char key = keypad.getKey();
    if (key) {
        inputCode += key; // Append the pressed key to inputCode
        Serial.println(key);

        // Check if the inputCode length matches the predefined code length
        if (inputCode.length() == code.length()) {
            if (inputCode == code) {
                Serial.println("Access Granted!");
                lockServo.write(90); // Unlock the servo (open position)
                delay(5000); // Keep it unlocked for 5 seconds
                lockServo.write(0); // Lock the servo again
            } else {
                Serial.println("Access Denied!");
            }
            inputCode = ""; // Reset input code
        }
    }
}
        

How the Code Works

The code begins by including the necessary libraries for the keypad and servo motor. It defines the keypad layout and initializes the pins for rows and columns.

In the setup() function, the servo is attached, and the initial position is set to locked. The Serial Monitor is also started for debugging purposes.

In the loop() function, the program checks for any keypresses. If a key is pressed, it appends the key to inputCode. Once the length of inputCode matches the predefined code, it checks if the entered code is correct.

If the code is correct, the servo unlocks (rotates to 90 degrees) for 5 seconds, then locks again. If the code is incorrect, it resets the input code.

Testing the Smart Lock System

Upload the code to your Arduino, and open the Serial Monitor. Enter the code on the keypad and observe the response. The servo should unlock when the correct code is entered.

Conclusion

With this project, you've built a simple smart lock system that can be enhanced further by integrating additional features like a Wi-Fi or Bluetooth module for remote access. You can also use an LCD to display messages and feedback to the user.

Next Steps: Consider implementing a Bluetooth module for smartphone access control!