You are on page 1of 16

Arduino Security and Alarm System Project

In this project we will learn how to make an Arduino Security and Alarm System. You
can watch the following video or read the written tutorial below.
Overview

The alarm activates in 10 seconds after pressing the A button. For detecting objects it
uses an ultrasonic sensor, and once the alarm detect something a buzzer starts emitting
a sound.  In order to stop the alarm we need to insert a 4 digits password. The preset
password is 1234 but we also have the possibility to change it.

By pressing the B button we enter the password change menu where first we need to
enter the current password in order to continue and then enter the new 4 digits
password. Once the password is changed, the next time we activate the alarm, we will
only be able to stop the alarm by entering the new password. If we enter a wrong
password we will get a message that we need to try again.

Required Components

Now let’s see the required components for this project. Obviously, we need an Arduino
board, an ultrasonic sensor, an LCD display, a buzzer and a 4×4 keypad.
You can get the components needed for this Arduino Project from the links below:

 Keypad
 LCD 16×02
 Ultrasonic Sensor
 Buzzer
 Arduino Board
Circuit Schematics

Here’s the circuit schematics.


So for the buzzer we need just a single pin but one with PWM support. The 4×4 keypad
has 8 pins, 4 of them are for the rows and 4 of them for the columns of the keypad.
Each button is actually a push button switch which makes a short between one row and
column when pressed.
So, for example, if we set the row 1 line low, and all column lines high, when we will
press, for example, the button 3, due to the short between the two lines, the column 3
line will drop to low so in such a case we can register that the button 3 has been
pressed.

As for the other two components on this project, the ultrasonic sensor and the LCD
display, you can check my previous detailed tutorials on how to connect and use them.

Arduino Alarm System Source Code

Next let’s see the Arduino code. As the code is a bit longer, for better understanding, I
will post the source code of the program in sections with description for each section.
And at the end of this article I will post the complete source code.

So we need to include the standard LiquidCrystal library for the LCD and the Keypad
library which needs to be additionally installed. Then we need to define the buzzer and
the ultrasonic sensor’s pins, define some variables needed for the program, define the
keys of the keypad, as well as create the two objects for the keypad and the LCD.

1. #include <LiquidCrystal.h> // includes the LiquidCrystal Library


2. #include <Keypad.h>
3.
4. #define buzzer 8
5. #define trigPin 9
6. #define echoPin 10
7.
8. long duration;
9. int distance, initialDistance, currentDistance, i;
10. int screenOffMsg =0;
11. String password="1234";
12. String tempPassword;
13. boolean activated = false; // State of the alarm
14. boolean isActivated;
15. boolean activateAlarm = false;
16. boolean alarmActivated = false;
17. boolean enteredPassword; // State of the entered password to stop the alarm
18. boolean passChangeMode = false;
19. boolean passChanged = false;
20.
21. const byte ROWS = 4; //four rows
22. const byte COLS = 4; //four columns
23. char keypressed;
24. //define the cymbols on the buttons of the keypads
25. char keyMap[ROWS][COLS] = {
26. {'1','2','3','A'},
27. {'4','5','6','B'},
28. {'7','8','9','C'},
29. {'*','0','#','D'}
30. };
31. byte rowPins[ROWS] = {14, 15, 16, 17}; //Row pinouts of the keypad
32. byte colPins[COLS] = {18, 19, 20, 21}; //Column pinouts of the keypad
33.
34. Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS,
COLS);
35. LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable,
d4, d5, d6, d7)
36.
37. void setup() {
38. lcd.begin(16,2);
39. pinMode(buzzer, OUTPUT); // Set buzzer as an output
40. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
41. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
42. }

In the setup section, we just need to initialize the LCD and define the pin modes for the
buzzer and the ultrasonic sensor.

In the loop section, first we check whether the alarm is activated or not. So if the alarm
is not activated, on the LCD we will have the home screen of the program which offers
two options, A for activating the alarm and B for changing the password. Then using the
myKeypad.getKey() function we read which button from the keypad has been pressed
and if that’s the button A, the  buzzer will produce a 200 milliseconds sound and the
activateAlarm variable will become true.
1. if (!alarmActivated) {
2. if (screenOffMsg == 0 ){
3. lcd.clear();
4. lcd.setCursor(0,0);
5. lcd.print("A - Activate");
6. lcd.setCursor(0,1);
7. lcd.print("B - Change Pass");
8. screenOffMsg = 1;
9. }
10. keypressed = myKeypad.getKey();
11. if (keypressed =='A'){ //If A is pressed, activate the alarm
12. tone(buzzer, 1000, 200);
13. activateAlarm = true;
14. }

In that case on the LCD we will print the message “Alarm will be activated in”, and using
a while loop we will make a countdown of 9 seconds before the alarm is activated. Then
the message “Alarm Activated” will appear and we will measure the initial distance from
our alarm device to the objects opposite of it.

1. if (activateAlarm) {
2. lcd.clear();
3. lcd.setCursor(0,0);
4. lcd.print("Alarm will be");
5. lcd.setCursor(0,1);
6. lcd.print("activated in");
7.
8. int countdown = 9; // 9 seconds count down before activating the alarm
9. while (countdown != 0) {
10. lcd.setCursor(13,1);
11. lcd.print(countdown);
12. countdown--;
13. tone(buzzer, 700, 100);
14. delay(1000);
15. }
16. lcd.clear();
17. lcd.setCursor(0,0);
18. lcd.print("Alarm Activated!");
19. initialDistance = getDistance();
20. activateAlarm = false;
21. alarmActivated = true;
22. }
So the next step is that the ultrasonic sensor will constantly check whether the currently
measured distance is smaller than the initial distance, corrected by a value of 10 cms,
and if that’s true it means an object has appeared in front of the sensor and the alarm
will be activated. The tune() function will activate the buzzer and the enterPassword()
custom function will be called.

1. if (alarmActivated == true){
2. currentDistance = getDistance() + 10;
3. if ( currentDistance < initialDistance) {
4. tone(buzzer, 1000); // Send 1KHz sound signal
5. lcd.clear();
6. enterPassword();
7. }
8. }
This custom function will print a message that the alarm is activated and that we need to
enter a password in order to stop the alarm.  So using the next while loop we are
constantly checking whether we have pressed a button on the keypad, and each button
press is added to the tempPassword variable. If we enter more than 4 digits or press the
sharp button the previously entered digits will be cleared so we can type them again
from begin.

1. void enterPassword() {
2. int k=5;
3. tempPassword = "";
4. activated = true;
5. lcd.clear();
6. lcd.setCursor(0,0);
7. lcd.print(" *** ALARM *** ");
8. lcd.setCursor(0,1);
9. lcd.print("Pass>");
10. while(activated) {
11. keypressed = myKeypad.getKey();
12. if (keypressed != NO_KEY){
13. if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
14. keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
15. keypressed == '8' || keypressed == '9' ) {
16. tempPassword += keypressed;
17. lcd.setCursor(k,1);
18. lcd.print("*");
19. k++;
20. }
21. }
22. if (k > 9 || keypressed == '#') {
23. tempPassword = "";
24. k=5;
25. lcd.clear();
26. lcd.setCursor(0,0);
27. lcd.print(" *** ALARM *** ");
28. lcd.setCursor(0,1);
29. lcd.print("Pass>");
30. }
31. if ( keypressed == '*') {
32. if ( tempPassword == password ) {
33. activated = false;
34. alarmActivated = false;
35. noTone(buzzer);
36. screenOffMsg = 0;
37. }
38. else if (tempPassword != password) {
39. lcd.setCursor(0,1);
40. lcd.print("Wrong! Try Again");
41. delay(2000);
42. lcd.clear();
43. lcd.setCursor(0,0);
44. lcd.print(" *** ALARM *** ");
45. lcd.setCursor(0,1);
46. lcd.print("Pass>");
47. }
48. }
49. }
50. }

On the other hand, if we press the asterisk button, we will check whether the currently
entered password is the same as the originally set password.  If that’s true the alarm will
be deactivated, the buzzer will stop producing sound and we will get back to the home
screen. But if we entered password was wrong the message “Wrong! Try Again!” will
appear and we will have to try to enter the correct password again.

For changing the password we use a similar method. Here first we need to enter the
current password in order to be able to set a new password.

1. else if (keypressed =='B') {


2. lcd.clear();
3. int i=1;
4. tone(buzzer, 2000, 100);
5. tempPassword = "";
6. lcd.setCursor(0,0);
7. lcd.print("Current Password");
8. lcd.setCursor(0,1);
9. lcd.print(">");
10. passChangeMode = true;
11. passChanged = true;
12. while(passChanged) {
13. keypressed = myKeypad.getKey();
14. if (keypressed != NO_KEY){
15. if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
16. keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
17. keypressed == '8' || keypressed == '9' ) {
18. tempPassword += keypressed;
19. lcd.setCursor(i,1);
20. lcd.print("*");
21. i++;
22. tone(buzzer, 2000, 100);
23. }
24. }
25. if (i > 5 || keypressed == '#') {
26. tempPassword = "";
27. i=1;
28. lcd.clear();
29. lcd.setCursor(0,0);
30. lcd.print("Current Password");
31. lcd.setCursor(0,1);
32. lcd.print(">");
33. }
34. if ( keypressed == '*') {
35. i=1;
36. tone(buzzer, 2000, 100);
37. if (password == tempPassword) {
38. tempPassword="";
39. lcd.clear();
40. lcd.setCursor(0,0);
41. lcd.print("Set New Password");
42. lcd.setCursor(0,1);
43. lcd.print(">");
44. while(passChangeMode) {
45. keypressed = myKeypad.getKey();
46. if (keypressed != NO_KEY){
47. if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
48. keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
49. keypressed == '8' || keypressed == '9' ) {
50. tempPassword += keypressed;
51. lcd.setCursor(i,1);
52. lcd.print("*");
53. i++;
54. tone(buzzer, 2000, 100);
55. }
56. }
57. if (i > 5 || keypressed == '#') {
58. tempPassword = "";
59. i=1;
60. tone(buzzer, 2000, 100);
61. lcd.clear();
62. lcd.setCursor(0,0);
63. lcd.print("Set New Password");
64. lcd.setCursor(0,1);
65. lcd.print(">");
66. }
67. if ( keypressed == '*') {
68. i=1;
69. tone(buzzer, 2000, 100);
70. password = tempPassword;
71. passChangeMode = false;
72. passChanged = false;
73. screenOffMsg = 0;
74. }
75. }
76. }
77. }
78. }
79. }

Here’s the complete source code of the Arduino Alarm System:

1. /*
2. * Arduino Security and Alarm System
3. *
4. * by Dejan Nedelkovski,
5. * www.HowToMechatronics.com
6. *
7. */
8.
9. #include <LiquidCrystal.h> // includes the LiquidCrystal Library
10. #include <Keypad.h>
11.
12. #define buzzer 8
13. #define trigPin 9
14. #define echoPin 10
15.
16. long duration;
17. int distance, initialDistance, currentDistance, i;
18. int screenOffMsg =0;
19. String password="1234";
20. String tempPassword;
21. boolean activated = false; // State of the alarm
22. boolean isActivated;
23. boolean activateAlarm = false;
24. boolean alarmActivated = false;
25. boolean enteredPassword; // State of the entered password to stop the alarm
26. boolean passChangeMode = false;
27. boolean passChanged = false;
28.
29. const byte ROWS = 4; //four rows
30. const byte COLS = 4; //four columns
31. char keypressed;
32. //define the cymbols on the buttons of the keypads
33. char keyMap[ROWS][COLS] = {
34. {'1','2','3','A'},
35. {'4','5','6','B'},
36. {'7','8','9','C'},
37. {'*','0','#','D'}
38. };
39. byte rowPins[ROWS] = {14, 15, 16, 17}; //Row pinouts of the keypad
40. byte colPins[COLS] = {18, 19, 20, 21}; //Column pinouts of the keypad
41.
42. Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS,
COLS);
43. LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable,
d4, d5, d6, d7)
44.
45. void setup() {
46. lcd.begin(16,2);
47. pinMode(buzzer, OUTPUT); // Set buzzer as an output
48. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
49. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
50. }
51.
52. void loop() {
53. if (activateAlarm) {
54. lcd.clear();
55. lcd.setCursor(0,0);
56. lcd.print("Alarm will be");
57. lcd.setCursor(0,1);
58. lcd.print("activated in");
59.
60. int countdown = 9; // 9 seconds count down before activating the alarm
61. while (countdown != 0) {
62. lcd.setCursor(13,1);
63. lcd.print(countdown);
64. countdown--;
65. tone(buzzer, 700, 100);
66. delay(1000);
67. }
68. lcd.clear();
69. lcd.setCursor(0,0);
70. lcd.print("Alarm Activated!");
71. initialDistance = getDistance();
72. activateAlarm = false;
73. alarmActivated = true;
74. }
75.
76. if (alarmActivated == true){
77. currentDistance = getDistance() + 10;
78. if ( currentDistance < initialDistance) {
79. tone(buzzer, 1000); // Send 1KHz sound signal
80. lcd.clear();
81. enterPassword();
82. }
83. }
84.
85. if (!alarmActivated) {
86. if (screenOffMsg == 0 ){
87. lcd.clear();
88. lcd.setCursor(0,0);
89. lcd.print("A - Activate");
90. lcd.setCursor(0,1);
91. lcd.print("B - Change Pass");
92. screenOffMsg = 1;
93. }
94. keypressed = myKeypad.getKey();
95. if (keypressed =='A'){ //If A is pressed, activate the alarm
96. tone(buzzer, 1000, 200);
97. activateAlarm = true;
98. }
99. else if (keypressed =='B') {
100.lcd.clear();
101.int i=1;
102.tone(buzzer, 2000, 100);
103.tempPassword = "";
104.lcd.setCursor(0,0);
105.lcd.print("Current Password");
106.lcd.setCursor(0,1);
107.lcd.print(">");
108.passChangeMode = true;
109.passChanged = true;
110.while(passChanged) {
111.keypressed = myKeypad.getKey();
112.if (keypressed != NO_KEY){
113.if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3'
||
114.keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
115.keypressed == '8' || keypressed == '9' ) {
116.tempPassword += keypressed;
117.lcd.setCursor(i,1);
118.lcd.print("*");
119.i++;
120.tone(buzzer, 2000, 100);
121.}
122.}
123.if (i > 5 || keypressed == '#') {
124.tempPassword = "";
125.i=1;
126.lcd.clear();
127.lcd.setCursor(0,0);
128.lcd.print("Current Password");
129.lcd.setCursor(0,1);
130.lcd.print(">");
131.}
132.if ( keypressed == '*') {
133.i=1;
134.tone(buzzer, 2000, 100);
135.if (password == tempPassword) {
136.tempPassword="";
137.lcd.clear();
138.lcd.setCursor(0,0);
139.lcd.print("Set New Password");
140.lcd.setCursor(0,1);
141.lcd.print(">");
142.while(passChangeMode) {
143.keypressed = myKeypad.getKey();
144.if (keypressed != NO_KEY){
145.if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3'
||
146.keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
147.keypressed == '8' || keypressed == '9' ) {
148.tempPassword += keypressed;
149.lcd.setCursor(i,1);
150.lcd.print("*");
151.i++;
152.tone(buzzer, 2000, 100);
153.}
154.}
155.if (i > 5 || keypressed == '#') {
156.tempPassword = "";
157.i=1;
158.tone(buzzer, 2000, 100);
159.lcd.clear();
160.lcd.setCursor(0,0);
161.lcd.print("Set New Password");
162.lcd.setCursor(0,1);
163.lcd.print(">");
164.}
165.if ( keypressed == '*') {
166.i=1;
167.tone(buzzer, 2000, 100);
168.password = tempPassword;
169.passChangeMode = false;
170.passChanged = false;
171.screenOffMsg = 0;
172.}
173.}
174.}
175.}
176.}
177.}
178.}
179.}
180.
181.void enterPassword() {
182.int k=5;
183.tempPassword = "";
184.activated = true;
185.lcd.clear();
186.lcd.setCursor(0,0);
187.lcd.print(" *** ALARM *** ");
188.lcd.setCursor(0,1);
189.lcd.print("Pass>");
190.while(activated) {
191.keypressed = myKeypad.getKey();
192.if (keypressed != NO_KEY){
193.if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3'
||
194.keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
195.keypressed == '8' || keypressed == '9' ) {
196.tempPassword += keypressed;
197.lcd.setCursor(k,1);
198.lcd.print("*");
199.k++;
200.}
201.}
202.if (k > 9 || keypressed == '#') {
203.tempPassword = "";
204.k=5;
205.lcd.clear();
206.lcd.setCursor(0,0);
207.lcd.print(" *** ALARM *** ");
208.lcd.setCursor(0,1);
209.lcd.print("Pass>");
210.}
211.if ( keypressed == '*') {
212.if ( tempPassword == password ) {
213.activated = false;
214.alarmActivated = false;
215.noTone(buzzer);
216.screenOffMsg = 0;
217.}
218.else if (tempPassword != password) {
219.lcd.setCursor(0,1);
220.lcd.print("Wrong! Try Again");
221.delay(2000);
222.lcd.clear();
223.lcd.setCursor(0,0);
224.lcd.print(" *** ALARM *** ");
225.lcd.setCursor(0,1);
226.lcd.print("Pass>");
227.}
228.}
229.}
230.}
231.// Custom function for the Ultrasonic sensor
232.long getDistance(){
233.//int i=10;
234.
235.//while( i<=10 ) {
236.// Clears the trigPin
237.digitalWrite(trigPin, LOW);
238.delayMicroseconds(2);
239.
240.// Sets the trigPin on HIGH state for 10 micro seconds
241.digitalWrite(trigPin, HIGH);
242.delayMicroseconds(10);
243.digitalWrite(trigPin, LOW);
244.
245.// Reads the echoPin, returns the sound wave travel time in microseconds
246.duration = pulseIn(echoPin, HIGH);
247.
248.// Calculating the distance
249.distance = duration*0.034/2;
250.//sumDistance += distance;
251.//}
252.//int averageDistance= sumDistance/10;
253.return distance;
254.
255.}
256.

Final Touch

For finishing the project I used a plastic electrical box in which I fitted all of the
components and contacted them together.

You might also like