I want to start this book with a simple project that any secret agent will want to have, a simple alarm system that will be activated whenever motion is detected by a sensor. This simple system is not only fun to make but will also help us to go over the basics of Arduino programming and electronics, which are the skills that we will use in this whole book.
It will basically be a simple alarm (a buzzer that makes sound, plus a red LED) combined with a motion detector. The user will also be able to stop the alarm by pressing a button.
We are going to do the following in this chapter:
First, we are going to see what the requirements for this project are, in terms of hardware and software
Then, we will see how to assemble the hardware parts for this project
After that, we will configure our system using the Arduino IDE
First, let's see what the required components for this project are. As this is the first chapter of the book, we will spend a bit more time here to detail the different components, as these are components that we will be using in the whole book.
The first component that will be central to the project is the Arduino Uno board:

In several chapters of this book, this will be the 'brain' of the projects that we will make. In all the projects, I will be using the official Arduino Uno R3 board. However, you can use an equivalent board from another brand or another Arduino board, such as an Arduino Mega board.
Another crucial component of our alarm system will be the buzzer:

This is a very simple component that is used to make simple sounds with Arduino. You couldn't play an MP3 with it but it's just fine for an alarm system. You can, of course, use any buzzer that is available; the goal is to just make a sound.
After that, we are going to need a motion detector:

Here, I used a very simple PIR motion detector. This sensor will measure the infrared (IR) light that is emitted by moving objects in its field of view, for example, people moving around. It is really easy and quite cheap to interface with Arduino. You can use any brand that you want for this sensor; it just needs a voltage level of 5V in order to be compatible with the Arduino Uno board.
Finally, here is the list of all the components that we will use in this project:
Arduino Uno (https://www.sparkfun.com/products/11021)
330 Ohm resistor (https://www.sparkfun.com/products/8377)
Button (https://www.sparkfun.com/products/97)
1k Ohm resistor (https://www.sparkfun.com/products/8980)
Breadboard (https://www.sparkfun.com/products/12002)
Jumper wires (https://www.sparkfun.com/products/8431)
On the software side, the only thing that we will need in the first chapter is the latest version of the Arduino IDE that you can download from the following URL: https://www.arduino.cc/en/main/software.
Note that we are going to use the Arduino IDE in all the projects of this book, so make sure to install the latest version.
We are now going to assemble the hardware for this project. As this is the first project of this book, it will be quite simple. However, there are quite a lot of components, so be sure to follow all the steps.
Here is a schematic to help you out during the process:

Let's start by putting all the components on the board. Place the buzzer, button, and LED on the board first, according to the schematics. Then, place the 330 Ohm resistor in series with the LED anode (the longest pin) and connect the 1k Ohm resistor to one pin of the push button.
This is how it should look at this stage:

Now we are going to connect each component to the Arduino board.
Let's start with the power supply. Connect the 5V pin of the Arduino board to one red power rail of the breadboard, and the GND pin of the Arduino board to one blue power rail of the breadboard.
Then, we are going to connect the buzzer. Connect one pin of the buzzer to pin number 5 of the Arduino board and the other pin to the blue power rail of the breadboard.
After that, let's connect the LED. Connect the free pin of the resistor to pin number 6 of the Arduino board and the free pin of the LED (the cathode) to the ground via the blue power rail.
Let's also connect the push button to our Arduino board. Refer to the schematic to be sure about the connections since it is a bit more complex. Basically, you need to connect the free pin of the resistor to the ground and connect the pin that is connected to the button to the 5V pin via the red power rail. Finally, connect the other side of the button to pin 12 of the Arduino board.
Finally, let's connect the PIR motion sensor to the Arduino board. Connect the VCC pin of the motion sensor to the red power rail and the GND pin to the blue power rail. Finally, connect the SIG pin (or OUT pin) to Arduino pin number 7.
The following is the final result:

If your project looks similar to this picture, congratulations, you just assembled your first secret agent project! You can now go on to the next section.
Now that the hardware for our project is ready, we can write down the code for the project so that we have a usable alarm system. The goal is to make the buzzer produce a sound whenever motion is detected and also to make the LED flash. However, whenever the button is pressed, the alarm will be switched off.
Here is the complete code for this project:
// Code for the simple alarm system // Pins const int alarm_pin = 5; const int led_pin = 6; const int motion_pin = 7; const int button_pin = 12; // Alarm boolean alarm_mode = false; // Variables for the flashing LED int ledState = LOW; long previousMillis = 0; long interval = 100; // Interval at which to blink (milliseconds) void setup() { // Set pins to output pinMode(led_pin,OUTPUT); pinMode(alarm_pin,OUTPUT); // Set button pin to input pinMode(button_pin, INPUT); // Wait before starting the alarm delay(5000); } void loop() { // Motion detected ? if (digitalRead(motion_pin)) { alarm_mode = true; } // If alarm mode is on, flash the LED and make the alarm ring if (alarm_mode){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; if (ledState == LOW) ledState = HIGH; else ledState = LOW; // Switch the LED digitalWrite(led_pin, ledState); } tone(alarm_pin,1000); } // If alarm is off if (alarm_mode == false) { // No tone & LED off noTone(alarm_pin); digitalWrite(led_pin, LOW); } // If button is pressed, set alarm off int button_state = digitalRead(button_pin); if (button_state) {alarm_mode = false;} }
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We are now going to see, in more detail, the different parts of the code. It starts by declaring which pins are connected to different elements of the project, such as the alarm buzzer:
const int alarm_pin = 5; const int led_pin = 6; const int motion_pin = 7; const int button_pin = 12;
After that, in the setup()
function of the sketch, we declare these pins as either inputs or outputs, as follows:
// Set pins to output pinMode(led_pin,OUTPUT); pinMode(alarm_pin,OUTPUT); // Set button pin to input pinMode(button_pin, INPUT);
Then, in the loop()
function of the sketch, we check whether the alarm was switched on by checking the state of the motion sensor:
if (digitalRead(motion_pin)) { alarm_mode = true; }
Note that if we detect some motion, we immediately set the alarm_mode
variable to true. We will see how the code makes use of this variable right now.
Now, if the alarm_mode
variable is true, we have to enable the alarm, make the buzzer emit a sound, and also flash the LED. This is done by the following code snippet:
if (alarm_mode){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; if (ledState == LOW) ledState = HIGH; else ledState = LOW; // Switch the LED digitalWrite(led_pin, ledState); } tone(alarm_pin,1000); }
Also, if alarm_mode
is returning false, we need to deactivate the alarm immediately by stopping the sound from being emitted and shutting down the LED. This is done with the following code:
if (alarm_mode == false) { // No tone & LED off noTone(alarm_pin); digitalWrite(led_pin, LOW); }
Finally, we continuously read the state of the push button. If the button is pressed, we will immediately set the alarm off:
int button_state = digitalRead(button_pin); if (button_state) {alarm_mode = false;}
Usually, we should take care of the bounce effect of the button in order to make sure that we don't have erratic readings when the button is pressed. However, here we only care about the button actually being pressed so we do not need to add an additional debouncing code for the button.
Note that you can find all the code for this project inside the GitHub repository of the book:
https://github.com/marcoschwartz/arduino-secret-agents
Now that we have written down the code for the project, it's time to get to the most exciting part of the chapter: testing the alarm system!
We are now ready to test our simple alarm system. Just grab the code for this project (either from the preceding code or the GitHub repository of the book) and put it into your Arduino IDE.
In the IDE, choose the right board type (for example, Arduino Uno) and also the correct serial port.
You can now upload the code to the board. Once it is done, simply pass your hand in front of the PIR motion sensor; the alarm should go off immediately. Then, simply press the push button to stop it.
To illustrate the behavior of the alarm, I simply used a battery pack to make it work when it is not connected to my computer. The following is the result when the alarm goes off:

If this works as expected, congratulations, you just built your first secret agent project: a simple alarm system based on Arduino!
If it doesn't work well at this point, there are several things you can check. First, go through the hardware configuration part again to make sure that your project is correctly configured.
Also, you can verify that when you pass your hand in front of the PIR sensor, it goes red. If this is not the case, most probably your PIR motion sensor has a problem and must be replaced.
In this first chapter, we built a simple alarm based on Arduino with only a few components.
There are several ways to go further and improve this project. You can add more functions to the project just by adding more lines to the code. For example, you can add a timer so that the alarm only goes off after a given amount of time, or you can build a mode where a push of the button actually activates or deactivates the alarm mode.
In the next chapter, we are going to build another project that is very useful for secret agents: an audio recording device based on Arduino!