Modular alarm system with motion detection!
Build your own alarm system with Arduino board, PIR motion sensor, siren, 4x4 keypad and oled display, with possibility to add more modules.
Components and supplies
1
Arduino Mega 2560
1
PIR Motion Sensor (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Arduino_AlarmSystemCODE
arduino
Codo for alarm system with motion detection.
1#include <Keypad.h> 2#include <SoftwareSerial.h> 3#include <U8g2lib.h> 4#include <SPI.h> 5#include <Wire.h> 6 7#define Password_Length 6 8 9char Data[Password_Length]; 10char Master[Password_Length] = "B1234; 11byte data_count = 0; 12char customKey; 13 14// *** KEYPAD CONFIGURATION *** 15const byte ROWS = 4; 16const byte COLS = 4; 17 18char hexaKeys [ROWS][COLS]={ 19 {'1', '2', '3', 'A'}, 20 {'4', '5', '6', 'B'}, 21 {'7', '8', '9', 'C'}, 22 {'*', '0', '#', 'D'} 23 }; // Keys on keypad 24 25byte rowPins[ROWS] = {9,8,7,6}; 26byte colPins[COLS] = {5,4,3,2}; 27 28Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 29 30unsigned long tiempo1, tiempo2; 31 32int siren = 10; 33int signalPin = 11; // digitalni pin 34int pirSensor = 12; // digitalni pin 35int led_Alarm = 13; // digitalni pin 36 37int alarmSystem = 1; //ALARM IS OFF (0) OR ON (1) 38 39int senzorValue = 0; 40 41int ledHigh = 0; 42 43int counter = 0; 44 45int state = 0; 46 47U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 48 49 50void setup() { 51 52 Serial.begin(9600); 53 u8g2.begin(); 54 delay(5000); 55 Serial.println("Secutiry Alarm System"); 56 pinMode(signalPin, OUTPUT); 57 pinMode(pirSensor, INPUT); 58 pinMode(led_Alarm, OUTPUT); 59 pinMode(siren, OUTPUT); 60 Serial.print("Alarmsystem has status:"); 61 Serial.println(alarmSystem); 62 Serial.println("Program started"); 63 delay(1999); 64} 65 66void loop() { 67 68 if (alarmSystem == 1){ 69 70 if (state == 0){ 71 digitalWrite(led_Alarm, LOW); 72 digitalWrite(siren, LOW); 73 u8g2.clearBuffer(); 74 u8g2.setFont(u8g2_font_courR10_tf); 75 u8g2.drawStr(8,10, "Alarm is"); 76 u8g2.drawStr(8,24, "ON!"); 77 u8g2.sendBuffer(); 78 tiempo1 = millis(); // read the internal clock 79 tiempo2 = millis(); // read the internal clock 80 Serial.println("WE ARE IN STATE 0"); 81 Event(); 82 senzorValue = digitalRead(pirSensor); // Sensor returns 1 - HIGH, if detects motion and 0 - LOW if does not detect motion 83 // Senzor nam vrne 1 - HIGH e je zaznal gibanje in 0 - LOW e ni zaznal gibanja 84 85 if (senzorValue == HIGH){ 86 state = 1; 87 u8g2.clearBuffer(); 88 u8g2.setFont(u8g2_font_courR14_tf); 89 u8g2.drawStr(16,12, "MOTION"); 90 u8g2.drawStr(16,28, "DETECTED!"); 91 u8g2.sendBuffer(); 92 93 while (tiempo1 < tiempo2 + 20000){ 94 customKey = customKeypad.getKey(); 95 if (customKey == '*'){ 96 Event2(); 97 tiempo1 = tiempo1 + 50000; 98 //Serial.println("SMO TUKAJ 1"); 99 Serial.println(tiempo1); 100 Serial.println(tiempo2); 101 delay(2000); 102 } 103 else{tiempo1 = millis(); 104 //Serial.println("SMO TUKAJ 2"); 105 } 106 } 107 } 108 } 109 110 if (state == 1){ 111 112 // ADD COUNT DOWN FOR TURN ON ALARM SYSTEM, IF PASSWORD IS 113 // DODAMO COUNT DOWN ZA PRIIG ALARMA, E V TEM ASU VNESE GESLO SE ALARM RESETIRA! 114 115 tiempo2 = millis(); 116 unsigned long i = 5000; 117 tiempo2 = tiempo2 + i; 118 int x = 0; 119 int y = 0; 120 121 while (tiempo1 < tiempo2) // loop for counting down 30 seconds if password is correct 122 { 123 //Serial.println("SMO TUKAJ 3"); 124 tiempo1 = millis(); 125 x = (tiempo2 - tiempo1) / 1000; 126 127 // if is oply for print on the display! 128 if ( y != x){ 129 u8g2.clearBuffer(); 130 u8g2.setFont(u8g2_font_courR14_tf); 131 u8g2.drawStr(16,12, "Countdown: "); 132 u8g2.setCursor(40,32); 133 u8g2.print(x); 134 u8g2.sendBuffer(); 135 } 136 y = x; 137 } 138 139 while (state != 0){ 140 //Serial.println("SMO TUKAJ 4"); 141 u8g2.clearBuffer(); 142 u8g2.setFont(u8g2_font_courR14_tf); 143 u8g2.drawStr(16,12, "Siren is"); 144 u8g2.drawStr(16,32, "turnedOn"); 145 u8g2.sendBuffer(); 146 digitalWrite(led_Alarm, HIGH); 147 digitalWrite(siren, HIGH); 148 Event(); 149 } 150 digitalWrite(led_Alarm, LOW); 151 digitalWrite(siren, LOW); 152 } 153 } 154 155 // ALARM SYSTEM IS OFF (alarmSystem = 0) 156 else { 157 158 //Serial.print("Counter je: "); 159 //Serial.println(counter); 160 //Serial.println("SMO TUKAJ 0"); 161 162 u8g2.clearBuffer(); 163 u8g2.setFont(u8g2_font_courR10_tf); 164 u8g2.drawStr(8,10, "Alarm is"); 165 u8g2.drawStr(8,24, "turnedOff"); 166 u8g2.setCursor(96,24); 167 u8g2.print(alarmSystem); 168 u8g2.sendBuffer(); 169 Event(); 170 } 171} 172 173 174//////// ******* Methods ******* //////// 175 176void PreverjanjeGesla(){ 177 178 u8g2.clearBuffer(); 179 180 if(!strcmp(Data, Master)){ // if password is correct, e je geslo pravilno 181 182 tiempo2 = millis(); 183 unsigned long i = 0; 184 185 if (alarmSystem == 0){ 186 i = 30000; // by default here is 30 seconds time to move out from PIR sensor, 187 // po defaultu bo tukaj 30 sekund, da se lahko v miru umakne, ko bo alarmni sistem vkljuen 188 } 189 else{ 190 i = 5000; 191 } 192 tiempo2 = tiempo2 + i; 193 int x = 0; 194 int y = 0; 195 196 while (tiempo1 < tiempo2) // loop for countdown of 10 seconds if password was typed correctly, 197 // zanka za odtevanje 10 sekund e je geslo PRAVILNO 198 { 199 digitalWrite(signalPin, HIGH); 200 201 tiempo1 = millis(); 202 x = (tiempo2 - tiempo1) / 1000; 203 204 // if is only for output on display 205 // if je samo za izpis na zaslon! 206 if ( y != x){ 207 u8g2.clearBuffer(); 208 u8g2.setFont(u8g2_font_courR14_tf); 209 u8g2.drawStr(16,12, "Countdown: "); 210 u8g2.setCursor(40,32); 211 u8g2.print(x); 212 u8g2.sendBuffer(); 213 } 214 y = x; 215 } 216 217 digitalWrite(signalPin, LOW); 218 u8g2.clearBuffer(); 219 220 if (alarmSystem == 1){ 221 alarmSystem = 0; // Alarm turns off 222 state = 0; 223 } 224 else{ 225 alarmSystem = 1; // Alarm turns on 226 state = 0; 227 } 228 } 229 230 else{ 231 while ( ledHigh < 5 ){ 232 Serial.println("Password is incorect!"); 233 u8g2.setFont(u8g2_font_courR10_tf); 234 u8g2.drawStr(24,14, "False"); 235 u8g2.drawStr(24,30, "Password!"); 236 u8g2.sendBuffer(); 237 digitalWrite(signalPin, HIGH); 238 delay(500); 239 digitalWrite(signalPin, LOW); 240 delay(500); 241 ledHigh++; 242 } 243 counter = 0; 244 ledHigh = 0; 245 } 246 247 u8g2.clearBuffer(); 248 clearData(); 249} 250 251void Event(){ 252 customKey = customKeypad.getKey(); 253 if (customKey == '*'){ 254 u8g2.clearBuffer(); 255 u8g2.setFont(u8g2_font_courR10_tf); 256 u8g2.drawStr(8,10, "*pressed" ); 257 u8g2.drawStr(8,22, "Typepassword!"); 258 u8g2.sendBuffer(); 259 counter = 1; 260 tiempo2 = millis(); 261 262 unsigned long i = 10000; 263 tiempo2 = tiempo2 + i; 264 } 265 266 while (counter == 1){ 267 tiempo1 = millis(); 268 customKey = customKeypad.getKey(); 269 if (customKey){ 270 Data[data_count] = customKey; 271 data_count++; 272 } 273 if(tiempo1 > tiempo2) { 274 // A sentence where we check whether we have entered the password within 10 seconds after pressing the * key. 275 // If the password has not been entered within 10 seconds, the counter is reset to 0. 276 // This means that the alarm system is switched off. 277 counter = 0; 278 Serial.print("Counter:"); 279 Serial.println(counter); 280 Serial.println("tiempo1:"); 281 Serial.println(tiempo1); 282 Serial.println("tiempo2:"); 283 Serial.println(tiempo2); 284 } 285 if(data_count == Password_Length-1){PreverjanjeGesla();} 286 } 287} 288 289void Event2(){ 290 291 u8g2.clearBuffer(); 292 u8g2.setFont(u8g2_font_courR10_tf); 293 u8g2.drawStr(8,10, "*pressed"); 294 u8g2.drawStr(8,22, "Typepassword!"); 295 u8g2.sendBuffer(); 296 counter = 1; 297 tiempo2 = millis(); // in the variable we get the current time tiempo2 298 299 unsigned long i = 10000; 300 tiempo2 = tiempo2 + i; // we add 10s to the tiempo, this time is later compared to tiempo1, whose time increases in the if statement (tiempo1> tiempo2) 301 302 while (counter == 1){ 303 tiempo1 = millis(); 304 customKey = customKeypad.getKey(); // When the * key has already been pressed, the following key presses up to length of six are recorded in Data. 305 if (customKey){ 306 Data[data_count] = customKey; 307 data_count++; 308 } 309 if(tiempo1 > tiempo2) { 310 // A sentence where we check whether we have entered the password within 10 seconds after pressing the * key. If the password has not been entered within 10 seconds, the counter is reset to 0. 311 // This means that the alarm system is switched off. 312 counter = 0; 313 u8g2.clearBuffer(); 314 u8g2.setFont(u8g2_font_courR10_tf); 315 u8g2.drawStr(2,10, "Passwordwasnot"); 316 u8g2.drawStr(2,22, "typed in a timely manner"); 317 u8g2.sendBuffer(); 318 delay(3000); 319 } 320 if(data_count == Password_Length-1){PreverjanjeGesla();} 321 } 322} 323 324void clearData() 325{ 326 while(data_count != 0) 327 { 328 Data[data_count--] = 0; 329 } 330 return; 331}
Arduino_AlarmSystemCODE
arduino
Codo for alarm system with motion detection.
1#include <Keypad.h> 2#include <SoftwareSerial.h> 3#include <U8g2lib.h> 4#include <SPI.h> 5#include <Wire.h> 6 7#define Password_Length 6 8 9char Data[Password_Length]; 10char Master[Password_Length] = "B1234; 11byte data_count = 0; 12char customKey; 13 14// *** KEYPAD CONFIGURATION *** 15const byte ROWS = 4; 16const byte COLS = 4; 17 18char hexaKeys [ROWS][COLS]={ 19 {'1', '2', '3', 'A'}, 20 {'4', '5', '6', 'B'}, 21 {'7', '8', '9', 'C'}, 22 {'*', '0', '#', 'D'} 23 }; // Keys on keypad 24 25byte rowPins[ROWS] = {9,8,7,6}; 26byte colPins[COLS] = {5,4,3,2}; 27 28Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 29 30unsigned long tiempo1, tiempo2; 31 32int siren = 10; 33int signalPin = 11; // digitalni pin 34int pirSensor = 12; // digitalni pin 35int led_Alarm = 13; // digitalni pin 36 37int alarmSystem = 1; //ALARM IS OFF (0) OR ON (1) 38 39int senzorValue = 0; 40 41int ledHigh = 0; 42 43int counter = 0; 44 45int state = 0; 46 47U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 48 49 50void setup() { 51 52 Serial.begin(9600); 53 u8g2.begin(); 54 delay(5000); 55 Serial.println("Secutiry Alarm System"); 56 pinMode(signalPin, OUTPUT); 57 pinMode(pirSensor, INPUT); 58 pinMode(led_Alarm, OUTPUT); 59 pinMode(siren, OUTPUT); 60 Serial.print("Alarmsystem has status:"); 61 Serial.println(alarmSystem); 62 Serial.println("Program started"); 63 delay(1999); 64} 65 66void loop() { 67 68 if (alarmSystem == 1){ 69 70 if (state == 0){ 71 digitalWrite(led_Alarm, LOW); 72 digitalWrite(siren, LOW); 73 u8g2.clearBuffer(); 74 u8g2.setFont(u8g2_font_courR10_tf); 75 u8g2.drawStr(8,10, "Alarm is"); 76 u8g2.drawStr(8,24, "ON!"); 77 u8g2.sendBuffer(); 78 tiempo1 = millis(); // read the internal clock 79 tiempo2 = millis(); // read the internal clock 80 Serial.println("WE ARE IN STATE 0"); 81 Event(); 82 senzorValue = digitalRead(pirSensor); // Sensor returns 1 - HIGH, if detects motion and 0 - LOW if does not detect motion 83 // Senzor nam vrne 1 - HIGH e je zaznal gibanje in 0 - LOW e ni zaznal gibanja 84 85 if (senzorValue == HIGH){ 86 state = 1; 87 u8g2.clearBuffer(); 88 u8g2.setFont(u8g2_font_courR14_tf); 89 u8g2.drawStr(16,12, "MOTION"); 90 u8g2.drawStr(16,28, "DETECTED!"); 91 u8g2.sendBuffer(); 92 93 while (tiempo1 < tiempo2 + 20000){ 94 customKey = customKeypad.getKey(); 95 if (customKey == '*'){ 96 Event2(); 97 tiempo1 = tiempo1 + 50000; 98 //Serial.println("SMO TUKAJ 1"); 99 Serial.println(tiempo1); 100 Serial.println(tiempo2); 101 delay(2000); 102 } 103 else{tiempo1 = millis(); 104 //Serial.println("SMO TUKAJ 2"); 105 } 106 } 107 } 108 } 109 110 if (state == 1){ 111 112 // ADD COUNT DOWN FOR TURN ON ALARM SYSTEM, IF PASSWORD IS 113 // DODAMO COUNT DOWN ZA PRIIG ALARMA, E V TEM ASU VNESE GESLO SE ALARM RESETIRA! 114 115 tiempo2 = millis(); 116 unsigned long i = 5000; 117 tiempo2 = tiempo2 + i; 118 int x = 0; 119 int y = 0; 120 121 while (tiempo1 < tiempo2) // loop for counting down 30 seconds if password is correct 122 { 123 //Serial.println("SMO TUKAJ 3"); 124 tiempo1 = millis(); 125 x = (tiempo2 - tiempo1) / 1000; 126 127 // if is oply for print on the display! 128 if ( y != x){ 129 u8g2.clearBuffer(); 130 u8g2.setFont(u8g2_font_courR14_tf); 131 u8g2.drawStr(16,12, "Countdown: "); 132 u8g2.setCursor(40,32); 133 u8g2.print(x); 134 u8g2.sendBuffer(); 135 } 136 y = x; 137 } 138 139 while (state != 0){ 140 //Serial.println("SMO TUKAJ 4"); 141 u8g2.clearBuffer(); 142 u8g2.setFont(u8g2_font_courR14_tf); 143 u8g2.drawStr(16,12, "Siren is"); 144 u8g2.drawStr(16,32, "turnedOn"); 145 u8g2.sendBuffer(); 146 digitalWrite(led_Alarm, HIGH); 147 digitalWrite(siren, HIGH); 148 Event(); 149 } 150 digitalWrite(led_Alarm, LOW); 151 digitalWrite(siren, LOW); 152 } 153 } 154 155 // ALARM SYSTEM IS OFF (alarmSystem = 0) 156 else { 157 158 //Serial.print("Counter je: "); 159 //Serial.println(counter); 160 //Serial.println("SMO TUKAJ 0"); 161 162 u8g2.clearBuffer(); 163 u8g2.setFont(u8g2_font_courR10_tf); 164 u8g2.drawStr(8,10, "Alarm is"); 165 u8g2.drawStr(8,24, "turnedOff"); 166 u8g2.setCursor(96,24); 167 u8g2.print(alarmSystem); 168 u8g2.sendBuffer(); 169 Event(); 170 } 171} 172 173 174//////// ******* Methods ******* //////// 175 176void PreverjanjeGesla(){ 177 178 u8g2.clearBuffer(); 179 180 if(!strcmp(Data, Master)){ // if password is correct, e je geslo pravilno 181 182 tiempo2 = millis(); 183 unsigned long i = 0; 184 185 if (alarmSystem == 0){ 186 i = 30000; // by default here is 30 seconds time to move out from PIR sensor, 187 // po defaultu bo tukaj 30 sekund, da se lahko v miru umakne, ko bo alarmni sistem vkljuen 188 } 189 else{ 190 i = 5000; 191 } 192 tiempo2 = tiempo2 + i; 193 int x = 0; 194 int y = 0; 195 196 while (tiempo1 < tiempo2) // loop for countdown of 10 seconds if password was typed correctly, 197 // zanka za odtevanje 10 sekund e je geslo PRAVILNO 198 { 199 digitalWrite(signalPin, HIGH); 200 201 tiempo1 = millis(); 202 x = (tiempo2 - tiempo1) / 1000; 203 204 // if is only for output on display 205 // if je samo za izpis na zaslon! 206 if ( y != x){ 207 u8g2.clearBuffer(); 208 u8g2.setFont(u8g2_font_courR14_tf); 209 u8g2.drawStr(16,12, "Countdown: "); 210 u8g2.setCursor(40,32); 211 u8g2.print(x); 212 u8g2.sendBuffer(); 213 } 214 y = x; 215 } 216 217 digitalWrite(signalPin, LOW); 218 u8g2.clearBuffer(); 219 220 if (alarmSystem == 1){ 221 alarmSystem = 0; // Alarm turns off 222 state = 0; 223 } 224 else{ 225 alarmSystem = 1; // Alarm turns on 226 state = 0; 227 } 228 } 229 230 else{ 231 while ( ledHigh < 5 ){ 232 Serial.println("Password is incorect!"); 233 u8g2.setFont(u8g2_font_courR10_tf); 234 u8g2.drawStr(24,14, "False"); 235 u8g2.drawStr(24,30, "Password!"); 236 u8g2.sendBuffer(); 237 digitalWrite(signalPin, HIGH); 238 delay(500); 239 digitalWrite(signalPin, LOW); 240 delay(500); 241 ledHigh++; 242 } 243 counter = 0; 244 ledHigh = 0; 245 } 246 247 u8g2.clearBuffer(); 248 clearData(); 249} 250 251void Event(){ 252 customKey = customKeypad.getKey(); 253 if (customKey == '*'){ 254 u8g2.clearBuffer(); 255 u8g2.setFont(u8g2_font_courR10_tf); 256 u8g2.drawStr(8,10, "*pressed" ); 257 u8g2.drawStr(8,22, "Typepassword!"); 258 u8g2.sendBuffer(); 259 counter = 1; 260 tiempo2 = millis(); 261 262 unsigned long i = 10000; 263 tiempo2 = tiempo2 + i; 264 } 265 266 while (counter == 1){ 267 tiempo1 = millis(); 268 customKey = customKeypad.getKey(); 269 if (customKey){ 270 Data[data_count] = customKey; 271 data_count++; 272 } 273 if(tiempo1 > tiempo2) { 274 // A sentence where we check whether we have entered the password within 10 seconds after pressing the * key. 275 // If the password has not been entered within 10 seconds, the counter is reset to 0. 276 // This means that the alarm system is switched off. 277 counter = 0; 278 Serial.print("Counter:"); 279 Serial.println(counter); 280 Serial.println("tiempo1:"); 281 Serial.println(tiempo1); 282 Serial.println("tiempo2:"); 283 Serial.println(tiempo2); 284 } 285 if(data_count == Password_Length-1){PreverjanjeGesla();} 286 } 287} 288 289void Event2(){ 290 291 u8g2.clearBuffer(); 292 u8g2.setFont(u8g2_font_courR10_tf); 293 u8g2.drawStr(8,10, "*pressed"); 294 u8g2.drawStr(8,22, "Typepassword!"); 295 u8g2.sendBuffer(); 296 counter = 1; 297 tiempo2 = millis(); // in the variable we get the current time tiempo2 298 299 unsigned long i = 10000; 300 tiempo2 = tiempo2 + i; // we add 10s to the tiempo, this time is later compared to tiempo1, whose time increases in the if statement (tiempo1> tiempo2) 301 302 while (counter == 1){ 303 tiempo1 = millis(); 304 customKey = customKeypad.getKey(); // When the * key has already been pressed, the following key presses up to length of six are recorded in Data. 305 if (customKey){ 306 Data[data_count] = customKey; 307 data_count++; 308 } 309 if(tiempo1 > tiempo2) { 310 // A sentence where we check whether we have entered the password within 10 seconds after pressing the * key. If the password has not been entered within 10 seconds, the counter is reset to 0. 311 // This means that the alarm system is switched off. 312 counter = 0; 313 u8g2.clearBuffer(); 314 u8g2.setFont(u8g2_font_courR10_tf); 315 u8g2.drawStr(2,10, "Passwordwasnot"); 316 u8g2.drawStr(2,22, "typed in a timely manner"); 317 u8g2.sendBuffer(); 318 delay(3000); 319 } 320 if(data_count == Password_Length-1){PreverjanjeGesla();} 321 } 322} 323 324void clearData() 325{ 326 while(data_count != 0) 327 { 328 Data[data_count--] = 0; 329 } 330 return; 331}
Downloadable files
Hardware and wiring
Hardware and wiring

Comments
Only logged in users can leave comments