Devices & Components
16x2 LCD display with I²C interface
Grove - Servo
Arduino® UNO R4 WiFi
5mm Green LED
5mm Red LED
Resistor 470 ohm
Active buzzer
4X4 MATRIX KEYPAD
Hardware & Tools
nuts for screws
Solder Soldering Wire
Digital Multimeter
Wire cutter
Soldering Iron Kit
Software & Tools
Solid Edge 2025
Arduino IDE
Project description
Code
Final_Working_code_Smart_lock
c
Smart Lock
1#include <Wire.h> 2#include <LiquidCrystal_I2C.h> 3#include <Servo.h> 4#include <Keypad.h> 5 6// --- LCD Configuration --- 7LiquidCrystal_I2C lcd(0x27, 16, 2); 8 9// --- Servo --- 10Servo lockServo; 11 12// --- Keypad Configuration --- 13const byte ROWS = 4; 14const byte COLS = 4; 15char keys[ROWS][COLS] = { 16 {'1','2','3','A'}, 17 {'4','5','6','B'}, 18 {'7','8','9','C'}, 19 {'*','0','#','D'} 20}; 21byte rowPins[ROWS] = {2, 3, 4, 5}; 22byte colPins[COLS] = {6, 7, 9, 8}; // Note: matches first code's pin order 23 24Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 25 26// --- Pin Definitions --- 27#define RED_LED 12 28#define GREEN_LED 13 29#define SERVO_PIN 10 30#define BUZZER 11 31 32// --- Password Logic --- 33String correctPassword = "1234"; 34String enteredPassword = ""; 35 36void setup() { 37 Serial.begin(9600); 38 39 // Initialize LCD 40 lcd.init(); 41 lcd.backlight(); 42 43 // Initialize pins 44 pinMode(RED_LED, OUTPUT); 45 pinMode(GREEN_LED, OUTPUT); 46 pinMode(BUZZER, OUTPUT); 47 48 // Initialize Servo 49 lockServo.attach(SERVO_PIN); 50 lockServo.write(90); // Locked position 51 52 // Default state: locked 53 digitalWrite(RED_LED, HIGH); 54 digitalWrite(GREEN_LED, LOW); 55 56 // --- Startup Test (from first code) --- 57 lcd.print("System Testing"); 58 59 // LED Test 60 lcd.setCursor(0, 1); 61 lcd.print("Testing LEDs... "); 62 digitalWrite(RED_LED, HIGH); delay(500); 63 digitalWrite(RED_LED, LOW); 64 digitalWrite(GREEN_LED, HIGH); delay(500); 65 digitalWrite(GREEN_LED, LOW); 66 67 // Buzzer Test 68 lcd.clear(); 69 lcd.print("Buzzer: BEEP"); 70 digitalWrite(BUZZER, HIGH); delay(150); 71 digitalWrite(BUZZER, LOW); 72 73 // Servo Test 74 lcd.clear(); 75 lcd.print("Servo: Testing"); 76 lockServo.write(0); delay(1000); 77 lockServo.write(90); delay(1000); 78 79 lcd.clear(); 80 lcd.print("Test Complete!"); 81 delay(1000); 82 83 // Set locked state and show prompt 84 digitalWrite(RED_LED, HIGH); 85 resetSystem(); 86 87 Serial.println("--- System Online: Waiting for Input ---"); 88} 89 90void loop() { 91 char key = keypad.getKey(); // Uses Keypad library (no manual scan needed) 92 93 if (key) { 94 Serial.print(key); 95 96 if (key == '#') { 97 Serial.println("\n--- SUBMIT BUTTON PRESSED ---"); 98 processPassword(); 99 } 100 else if (key == '*') { 101 Serial.println("\n--- CLEAR BUTTON PRESSED ---"); 102 resetSystem(); 103 } 104 else { 105 if (enteredPassword.length() < 8) { 106 enteredPassword += key; 107 108 // Show asterisk for each character 109 lcd.setCursor(enteredPassword.length() - 1, 1); 110 lcd.print('*'); 111 112 Serial.print("Typed: "); Serial.print(key); 113 Serial.print(" | Buffer: "); Serial.println(enteredPassword); 114 115 // Key press beep 116 digitalWrite(BUZZER, HIGH); delay(50); 117 digitalWrite(BUZZER, LOW); 118 } 119 } 120 } 121} 122 123void processPassword() { 124 Serial.print("Entered: ["); Serial.print(enteredPassword); Serial.println("]"); 125 Serial.print("Expected: ["); Serial.print(correctPassword); Serial.println("]"); 126 127 lcd.clear(); 128 enteredPassword.trim(); 129 130 if (enteredPassword == correctPassword) { 131 Serial.println(">>> RESULT: MATCH FOUND! <<<"); 132 lcd.print("Access Granted!"); 133 134 digitalWrite(RED_LED, LOW); 135 digitalWrite(GREEN_LED, HIGH); 136 137 lockServo.write(0); // Unlock 138 delay(3000); 139 lockServo.write(90); // Lock again 140 141 digitalWrite(GREEN_LED, LOW); 142 digitalWrite(RED_LED, HIGH); 143 } 144 else { 145 Serial.println(">>> RESULT: NO MATCH! <<<"); 146 lcd.print("Wrong Password!"); 147 148 for (int i = 0; i < 4; i++) { 149 digitalWrite(BUZZER, HIGH); delay(150); 150 digitalWrite(BUZZER, LOW); delay(150); 151 } 152 } 153 154 resetSystem(); 155} 156 157void resetSystem() { 158 enteredPassword = ""; 159 lcd.clear(); 160 lcd.print("Enter Password:"); 161 lcd.setCursor(0, 1); 162}
Downloadable files
3D design for Smart Lock
3D Design of Smart Lock
3D design for Smart Lock.zip
Comments
Only logged in users can leave comments