Arduino Based Smart Security System
A home security system that combines a keypad-controlled door lock, motion detection, visual status indicators, and an audible alarm to simulate a smart home security system.
Devices & Components
1
40 colored male-male jumper wires
1
Grove - Servo
1
Breadboard - 830 contacts
1
16x2 LCD display with I²C interface
1
Grove - PIR Motion Sensor
1
Arduino Uno Rev3
1
100 ohm Resistor
3
163 ohm Resistor
1
Keypad 4x4
1
Piezo Speaker
1
160 ohm Resistor
1
RCGB LED
1
250 kohm Potentiometer
Software & Tools
1
Tinkercad
Project description
Code
Smart Security System
cpp
1#include <Servo.h> 2#include <LiquidCrystal.h> 3#include <Keypad.h> 4 5// declare constants for lcd 6const int RS = 2; 7const int E = 3; 8const int DB4 = 4; 9const int DB5 = 5; 10const int DB6 = 6; 11const int DB7 = 7; 12 13// constants needed for servo 14// Declare SERVO pin 15const int SERVO_PIN = A0; 16 17// constants for motion sensor and RGB LED 18const int PIR = A5; 19const int ALARM = A4; 20const int RED_LED = A3; 21const int BLUE_LED = A2; 22const int GREEN_LED = A1; 23 24/*********************** Piezo Speaker/Alarm ***********************/ 25// different states the security system can be in 26const int UNARMED = 0; 27const int ARMED = 1; 28const int ALARM_STATUS = 2; 29 30// stores current state of security system 31int systemState = UNARMED; 32 33/*********************** MOTION SENSOR ***********************/ 34// keep track of PIR motion sensor 35int pirState; 36 37/*********************** LCD ***********************/ 38// instantiate LCD object 39LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 40 41/*********************** Servo Motor ***********************/ 42// servo in locked and unlocked positions 43const int LOCKED_POS = 0; 44const int UNLOCKED_POS = 90; 45 46// create servo object 47Servo servo; 48 49/*********************** Keypad ***********************/ 50// Constants for keypad 51// only using digits 1 - 9 52// only need col 1 - 3 and rows 1 - 3 53const byte ROWS = 3; 54const byte COLS = 3; 55 56// keymap defines keys pressed according row & col 57const char KEYMAP[ROWS][COLS]= 58{ 59 {'1', '2', '3'}, 60 {'4', '5', '6'}, 61 {'7', '8', '9'}, 62}; 63 64// code for Keypad 65const String CODE = "1235"; 66 67// code that shows keypad connections to arduino pins 68byte ROW_PINS[ROWS] = {13, 12, 11}; // Rows 0 - 2 on keypad 69byte COL_PINS[COLS] = {10, 9, 8}; // columns 0 - 2 on keypad 70 71 72// instantiate keypad object 73Keypad keypad = Keypad(makeKeymap(KEYMAP), ROW_PINS, COL_PINS, ROWS, COLS); 74 75// stores keypad input from user 76String input = ""; 77 78// stores the current key pressed 79char key; 80 81// counts number of incorrect code attempts 82int failedAttempts = 0; 83 84// keeps track of alarm disarm screen 85bool disarmScreen = false; 86 87 88void setup(){ 89 // establish size of lcd screen 90 lcd.begin(16, 2); 91 92 // attach servo to pin 93 servo.attach(SERVO_PIN); 94 95 // set sensor pins as inputs and output devices as outputs 96 pinMode(PIR, INPUT); 97 pinMode(ALARM, OUTPUT); 98 pinMode(RED_LED, OUTPUT); 99 pinMode(GREEN_LED, OUTPUT); 100 pinMode(BLUE_LED, OUTPUT); 101 102 // update all outputs to match starting state 103 updateOutputs(); 104} 105 106void loop(){ 107 // check if user entered into keypad 108 readKeypad(); 109 110 // check if motion is detected 111 readPIR(); 112 113 // update outputs based on system status 114 updateOutputs(); 115} 116 117/*********************** FUNCTIONS ***********************/ 118 119// readKeypad: reads keypad input and controls the 120// arming/disarming and activates the alarm 121void readKeypad(){ 122 // read key from keypad 123 key = keypad.getKey(); 124 125 // check to see if there is a key (user typed something) 126 // is not null 127 if (key != NO_KEY) { 128 129 // show disarm screen when alarm is active 130 if (systemState == ALARM_STATUS) 131 { 132 disarmScreen = true; 133 } 134 135 input += key; 136 137 // check to see if input is 4 digits long 138 if (input.length() == 4){ 139 140 // check to see if code is correct (1235) 141 if (input == CODE) { 142 143 // reset failed attempts after successful code entered 144 failedAttempts = 0; 145 146 lcd.setCursor(6, 0); 147 lcd.print("CORRECT"); 148 delay(500); 149 150 // to turn alarm off and disarm system 151 if(systemState == ALARM_STATUS){ 152 alarmOff(); 153 systemState = UNARMED; 154 disarmScreen = false; 155 } 156 157 // arm system if currently unarmed 158 else if(systemState == UNARMED){ 159 systemState = ARMED; 160 } 161 162 // unarm system if currently armed 163 else{ 164 systemState = UNARMED; 165 } 166 167 } 168 else { 169 170 // increase failed attempt counter 171 failedAttempts++; 172 173 lcd.setCursor(6, 0); 174 lcd.print("WRONG"); 175 176 delay(500); 177 178 // activate alarm after 3 incorrect tries 179 if(failedAttempts >= 3){ 180 systemState = ALARM_STATUS; 181 disarmScreen = false; 182 alarmOn(); 183 } 184 } 185 186 // clear keypad input after checking code 187 input = ""; 188 } 189 } 190} 191 192 193// readPIR: detects motion from PIR sensor and sets 194// off alarm when the system is armed 195void readPIR() { 196 197 // read motion sensor value 198 pirState = digitalRead(PIR); 199 200 // activate alarm if motion is detected while armed 201 if(systemState == ARMED && pirState == HIGH) 202 { 203 systemState = ALARM_STATUS; 204 disarmScreen = false; 205 alarmOn(); 206 } 207} 208 209// updateOutputs: updated the LCD, servo, LED and alarm 210// based on if system is armed or disarmed 211void updateOutputs(){ 212 213 // update lcd status display 214 // unarmed vs armed 215 showStatus(); 216 217 // change outputs depending on system state 218 if(systemState == UNARMED){ 219 220 // unlock door and display safe color 221 unlockDoor(); 222 displayColor(0, 255, 0); // green light 223 alarmOff(); 224 225 } 226 else if(systemState == ARMED){ 227 228 // lock door and display armed color 229 lockDoor(); 230 displayColor(255, 0, 0); // red light 231 alarmOff(); 232 233 } 234 else if(systemState == ALARM_STATUS){ 235 236 // lock door and activate alarm color 237 lockDoor(); 238 // combine red and white to simulate flashing light 239 displayColor(255, 0, 0); // red light 240 delay(50); 241 242 displayColor(255, 255, 255); // white light 243 delay(50); 244 alarmOn(); 245 } 246} 247 248// displayColor: controls RGB LED brightness 249// red, green, blue 250void displayColor(int r, int g, int b){ 251 252 // control brightness of each RGB LED color 253 analogWrite(RED_LED, r); 254 analogWrite(GREEN_LED, g); 255 analogWrite(BLUE_LED, b); 256} 257 258 259// lockDoor: rotates servo to lock 260// lock -> 0 degrees 261void lockDoor(){ 262 263 // move servo to locked position 264 servo.write(LOCKED_POS); 265} 266 267// unlockDoor: rotates servo to unlock 268// unlock -> 90 degrees 269void unlockDoor(){ 270 271 // move servo to unlocked position 272 servo.write(UNLOCKED_POS); 273} 274 275// showStatus: displays keypad input and status onto LCD 276void showStatus(){ 277 278 // display keypad input when alarm is not active 279 if(systemState != ALARM_STATUS) 280 { 281 lcd.setCursor(0,0); 282 lcd.print("CODE: "); 283 284 // clear old input before displaying new input 285 lcd.setCursor(6,0); 286 lcd.print(" "); 287 lcd.setCursor(6,0); 288 lcd.print(input); 289 290 lcd.setCursor(0,1); 291 } 292 293 // display message based on current system state 294 switch(systemState) 295 { 296 case UNARMED: 297 lcd.print("Status: UNARMED"); 298 break; 299 300 case ARMED: 301 lcd.print("Status: ARMED "); 302 break; 303 304 case ALARM_STATUS: 305 306 // display security warning until user enters code 307 if (!disarmScreen) 308 { 309 lcd.setCursor(0,0); 310 lcd.print("CALLING SECURITY"); 311 lcd.setCursor(0,1); 312 lcd.print(" ***INTRUDER*** "); 313 } 314 315 // allow user to disarm alarm 316 else{ 317 lcd.setCursor(0,0); 318 lcd.print("Enter Code To "); 319 320 lcd.setCursor(0,1); 321 lcd.print("Disarm: "); 322 lcd.print(input); 323 lcd.print(" "); 324 } 325 326 break; 327 } 328 329 // move cursor back to input location 330 lcd.setCursor(6,0); 331} 332 333// alarmOn: turns on alarm speaker 334void alarmOn(){ 335 336 // activate speaker alarm 337 tone(ALARM, 500); 338} 339 340//// alarmOn: turns off alarm speaker 341void alarmOff(){ 342 343 // turn off speaker alarm 344 noTone(ALARM); 345}
Documentation
Arduino Based Smart Security System Schematics
Arduino Based Smart Security System Schematics.pdf
Comments
Only logged in users can leave comments