Motion Sensing Alarm With Keypad & Password
An Arduino Uno-based alarm with motion sensor, three LED outputs and a keypad with password input.
Components and supplies
Buzzer
LEDs
PIR Motion Sensor (generic)
Resistor 330 ohm
Arduino UNO
Breadboard (generic)
Project description
Code
Motion Sensing Alarm with Keypad & Password
arduino
1#include <Keypad.h> // library for keyboard 2#include <Password.h> // library for password 3 4Password password = Password( "1234" ); // password 5 6const byte rows = 4; // four rows 7const byte cols = 4; // three columns 8char keys[rows][cols] = { // keys on keypad 9 10{'1','2','3','A'}, 11{'4','5','6','B'}, 12{'7','8','9','C'}, 13{'*','0','#','D'}, 14 15}; 16 17byte rowPins[rows] = {9,8,7,6}; 18byte colPins[cols] = {5,4,3,2}; 19Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); 20 21 22 23#define sensorz A3 // pin for PIR sensor data 24//#define contact 0 // pin for on/off alarm 25#define alrm 12 // pin for siren, buzzer 26#define redLed A2 // pin for red led 27#define greenLed A0 // pin for green led 28#define yellowLed A1 // pin for blue led 29int contact = 10; //used to immediately on/off alarm 30int val; 31int ledBlink; 32 33int sensorzData; 34unsigned long ceas, timpmemorat; 35 36int intarziereactivare = 20; // To delay for standby to armed 37int intarzieredezactivare = 10; // To delay for triggered to alarm activated 38int timpurlat = 10; // Time of alarm is on 39 40// This is the variable for states "0" 41char caz = 0; 42 43int sistem = 0; // system is 0 for off and 1 for on 44 45 46/* 47States for 48 49 0. - off 50 1. - stand-by 51 2. - waitting 52 3. - countdown 53 4. - alarm 54 55*/ 56 57void setup() 58 { 59 keypad.addEventListener(keypadEvent); // an object is created for tracking keystrokes 60 61 Serial.begin(9600); //Used for troubleshooting 62 pinMode(alrm, OUTPUT); 63 pinMode(sensorz, INPUT); 64 pinMode(contact, INPUT); 65 pinMode(redLed, OUTPUT); 66 pinMode(yellowLed, OUTPUT); 67 pinMode(greenLed, OUTPUT); 68 digitalRead(contact); 69 Serial.println("System startup"); //Used for troubleshooting 70 Serial.println("Alarm button status:"); //used for troubleshooting 71 Serial.println(digitalRead(contact)); //used for troubleshooting 72 } 73 74void loop() 75 76 { 77 78 ceas = millis(); // read the internal clock 79 val = digitalRead(contact); 80 81keypad.getKey(); 82 83 84 if (sistem%2 == 0) 85 { 86 // alarm is off 87 digitalWrite(greenLed, LOW); 88 digitalWrite(redLed, LOW); 89 digitalWrite(yellowLed, HIGH); 90 //Serial.println(contact); //Used for troubleshooting 91 92 digitalWrite(alrm, LOW); 93 caz = 0; 94 // Serial.println("System is OFF !"); // Used for troubleshooting 95 96 } 97 98 else 99 { 100 // alarm is on 101 if(caz == 0) 102 { 103 caz = 1; 104 timpmemorat = ceas; 105 digitalWrite(yellowLed, HIGH); 106 } 107 108 if(caz == 1) // system waiting 109 { 110 111 if ((ceas%1000)<500) digitalWrite(greenLed, HIGH); 112 else digitalWrite(greenLed, LOW); 113 keypad.getKey(); 114 if(ceas >= timpmemorat + intarziereactivare * 1000) {caz = 2;} 115 //Serial.println("System is arming !"); // Used for troubleshooting 116 } 117 118 if(caz == 2) // system is armed 119 { 120 digitalWrite(greenLed, HIGH); 121 keypad.getKey(); 122 123 sensorzData = digitalRead(sensorz); 124 //Serial.print("sensorzdData = "); //Used for troubleshooting 125 //Serial.println(sensorzData); //Used for troubleshooting 126 127 // if(sensorzData > 600) {caz = 3; timpmemorat = ceas;} 128 if(sensorzData == HIGH) 129 { 130 caz = 3; 131 timpmemorat = ceas; 132 digitalWrite(greenLed, LOW); 133 } 134 Serial.println("System is armed !"); // Used for Troubleshooting 135 } 136 137 if(caz == 3) // system is triggered and countdown 138 { 139 140 if ((ceas%500)<100) digitalWrite(redLed, HIGH); 141 else digitalWrite(redLed, LOW); 142 keypad.getKey(); 143 if(ceas >= timpmemorat + intarzieredezactivare * 10) {caz = 4; timpmemorat = ceas;} 144 Serial.println("System is triggered and is countdown !"); //Used for troubleshooting 145 } 146 147 if(caz == 4) // siren (buzzer) is active 148 { 149 //digitalWrite(alrm, HIGH); 150 digitalWrite(redLed, HIGH); 151 Serial.println("Siren is active !"); //Used for troubleshooting 152 153 154// For siren 155 156 //tone( 10, 10000, 100); // Simple Alarm Tone 157 for(double x = 0; x < 0.92; x += 0.01){ // Elegant Alarm Tone 158 tone(10, sinh(x+8.294), 10); 159 delay(1); 160 } 161 162 163 keypad.getKey(); 164 if(ceas >= timpmemorat + timpurlat * 1000) {caz = 2; digitalWrite(alrm, LOW); digitalWrite(redLed, LOW);} 165 } 166 } 167 } 168 169 //take care of some special events 170void keypadEvent(KeypadEvent eKey){ 171 switch (keypad.getState()){ 172 case PRESSED: 173 Serial.print("Pressed: "); 174 Serial.println(eKey); 175 switch (eKey){ 176 case '*': checkPassword(); break; 177 case '#': password.reset(); break; 178 default: password.append(eKey); 179 } 180 } 181} 182 183 184 void checkPassword(){ 185 if (password.evaluate()){ 186 Serial.println("Success"); //Used for troubleshooting 187 sistem++; 188 password.reset(); 189 Serial.println("Disarmed");//Add code to run if it works 190 }else{ 191 Serial.println("Wrong"); //Used for troubleshooting 192 //add code to run if it did not work 193 ledBlink = 0; 194 while (ledBlink <= 5){ 195 digitalWrite(redLed, HIGH); 196 delay(100); 197 digitalWrite(redLed, LOW); 198 delay(100); 199 ledBlink++; 200 } 201 password.reset(); 202 } 203}
Motion Sensing Alarm with Keypad & Password
arduino
1#include <Keypad.h> // library for keyboard 2#include 3 <Password.h> // library for password 4 5Password password = Password( 6 "1234" ); // password 7 8const byte rows = 4; // four 9 rows 10const byte cols = 4; // three columns 11char 12 keys[rows][cols] = { // keys on keypad 13 14{'1','2','3','A'}, 15{'4','5','6','B'}, 16{'7','8','9','C'}, 17{'*','0','#','D'}, 18 19}; 20 21byte 22 rowPins[rows] = {9,8,7,6}; 23byte colPins[cols] = {5,4,3,2}; 24Keypad keypad = 25 Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); 26 27 28 29#define sensorz 30 A3 // pin for PIR sensor data 31//#define contact 0 // pin for on/off 32 alarm 33#define alrm 12 // pin for siren, buzzer 34#define redLed A2 // 35 pin for red led 36#define greenLed A0 // pin for green led 37#define 38 yellowLed A1 // pin for blue led 39int contact = 10; //used to immediately 40 on/off alarm 41int val; 42int ledBlink; 43 44int sensorzData; 45unsigned long 46 ceas, timpmemorat; 47 48int intarziereactivare = 20; // To delay for standby 49 to armed 50int intarzieredezactivare = 10; // To delay for triggered to alarm activated 51int 52 timpurlat = 10; // Time of alarm is on 53 54// This is the variable 55 for states "0" 56char caz = 0; 57 58int sistem = 0; // system is 0 for 59 off and 1 for on 60 61 62/* 63States for 64 65 0. - off 66 1. - stand-by 67 68 2. - waitting 69 3. - countdown 70 4. - alarm 71 72*/ 73 74void setup() 75 76 { 77 keypad.addEventListener(keypadEvent); // an object is created for tracking 78 keystrokes 79 80 Serial.begin(9600); //Used for troubleshooting 81 pinMode(alrm, 82 OUTPUT); 83 pinMode(sensorz, INPUT); 84 pinMode(contact, INPUT); 85 pinMode(redLed, 86 OUTPUT); 87 pinMode(yellowLed, OUTPUT); 88 pinMode(greenLed, OUTPUT); 89 digitalRead(contact); 90 91 Serial.println("System startup"); //Used for troubleshooting 92 Serial.println("Alarm 93 button status:"); //used for troubleshooting 94 Serial.println(digitalRead(contact)); 95 //used for troubleshooting 96 } 97 98void loop() 99 100 { 101 102 ceas 103 = millis(); // read the internal clock 104 val = digitalRead(contact); 105 106keypad.getKey(); 107 108 109 110 if (sistem%2 == 0) 111 { 112 // alarm is off 113 digitalWrite(greenLed, 114 LOW); 115 digitalWrite(redLed, LOW); 116 digitalWrite(yellowLed, HIGH); 117 118 //Serial.println(contact); //Used for troubleshooting 119 120 digitalWrite(alrm, 121 LOW); 122 caz = 0; 123 // Serial.println("System is OFF !"); // Used for 124 troubleshooting 125 126 } 127 128 else 129 { 130 // alarm is on 131 if(caz 132 == 0) 133 { 134 caz = 1; 135 timpmemorat = ceas; 136 digitalWrite(yellowLed, 137 HIGH); 138 } 139 140 if(caz == 1) // system waiting 141 { 142 143 144 if ((ceas%1000)<500) digitalWrite(greenLed, HIGH); 145 else digitalWrite(greenLed, 146 LOW); 147 keypad.getKey(); 148 if(ceas >= timpmemorat + intarziereactivare 149 * 1000) {caz = 2;} 150 //Serial.println("System is arming !"); // Used for 151 troubleshooting 152 } 153 154 if(caz == 2) // system 155 is armed 156 { 157 digitalWrite(greenLed, HIGH); 158 keypad.getKey(); 159 160 161 sensorzData = digitalRead(sensorz); 162 //Serial.print("sensorzdData 163 = "); //Used for troubleshooting 164 //Serial.println(sensorzData); //Used 165 for troubleshooting 166 167 // if(sensorzData > 600) {caz = 3; timpmemorat 168 = ceas;} 169 if(sensorzData == HIGH) 170 { 171 caz = 3; 172 timpmemorat 173 = ceas; 174 digitalWrite(greenLed, LOW); 175 } 176 Serial.println("System 177 is armed !"); // Used for Troubleshooting 178 } 179 180 if(caz == 3) // 181 system is triggered and countdown 182 { 183 184 if ((ceas%500)<100) 185 digitalWrite(redLed, HIGH); 186 else digitalWrite(redLed, LOW); 187 keypad.getKey(); 188 189 if(ceas >= timpmemorat + intarzieredezactivare * 10) {caz = 4; timpmemorat 190 = ceas;} 191 Serial.println("System is triggered and is countdown !"); //Used 192 for troubleshooting 193 } 194 195 if(caz == 4) // siren (buzzer) 196 is active 197 { 198 //digitalWrite(alrm, HIGH); 199 digitalWrite(redLed, 200 HIGH); 201 Serial.println("Siren is active !"); //Used for troubleshooting 202 203 204// 205 For siren 206 207 //tone( 10, 10000, 100); // Simple Alarm Tone 208 for(double 209 x = 0; x < 0.92; x += 0.01){ // Elegant Alarm Tone 210 tone(10, sinh(x+8.294), 211 10); 212 delay(1); 213 } 214 215 216 keypad.getKey(); 217 218 if(ceas >= timpmemorat + timpurlat * 1000) {caz = 2; digitalWrite(alrm, 219 LOW); digitalWrite(redLed, LOW);} 220 } 221 } 222 } 223 224 //take care 225 of some special events 226void keypadEvent(KeypadEvent eKey){ 227 switch (keypad.getState()){ 228 229 case PRESSED: 230 Serial.print("Pressed: "); 231 Serial.println(eKey); 232 233 switch (eKey){ 234 case '*': checkPassword(); break; 235 case '#': password.reset(); 236 break; 237 default: password.append(eKey); 238 } 239 } 240} 241 242 243 244 void checkPassword(){ 245 if (password.evaluate()){ 246 Serial.println("Success"); 247 //Used for troubleshooting 248 sistem++; 249 password.reset(); 250 Serial.println("Disarmed");//Add 251 code to run if it works 252 }else{ 253 Serial.println("Wrong"); //Used for 254 troubleshooting 255 //add code to run if it did not work 256 ledBlink = 0; 257 258 while (ledBlink <= 5){ 259 digitalWrite(redLed, HIGH); 260 delay(100); 261 262 digitalWrite(redLed, LOW); 263 delay(100); 264 ledBlink++; 265 } 266 267 password.reset(); 268 } 269}
Downloadable files
3D Printed Box 07
3D Printed Box 07
3D Printed Box 08
3D Printed Box 08
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
3D Printed Box 04
3D Printed Box 04
3D Printed Box 10
3D Printed Box 10
3D Printed Box 01
3D Printed Box 01
3D Printed Box 06
3D Printed Box 06
3D Printed Box 09
3D Printed Box 09
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
3D Printed Box 03
3D Printed Box 03
3D Printed Box 05
3D Printed Box 05
3D Printed Box 02
3D Printed Box 02
3D Printed Box 11
3D Printed Box 11
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
3D Printed Box 11
3D Printed Box 11
3D Printed Box 05
3D Printed Box 05
3D Printed Box 03
3D Printed Box 03
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
3D Printed Box 01
3D Printed Box 01
3D Printed Box 09
3D Printed Box 09
3D Printed Box 02
3D Printed Box 02
3D Printed Box 08
3D Printed Box 08
3D Printed Box 06
3D Printed Box 06
3D Printed Box 07
3D Printed Box 07
3D Printed Box 04
3D Printed Box 04
3D Printed Box 10
3D Printed Box 10
Comments