Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arduino for Secret Agents
Arduino for Secret Agents

Arduino for Secret Agents: Transform your tiny Arduino device into a secret agent gadget to build a range of espionage projects with this practical guide for hackers

By Marco Schwartz
$15.99 per month
Book Nov 2015 170 pages 1st Edition
eBook
$25.99 $17.99
Print
$32.99
Subscription
$15.99 Monthly
eBook
$25.99 $17.99
Print
$32.99
Subscription
$15.99 Monthly

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Nov 20, 2015
Length 170 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783986088
Vendor :
Arduino
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

Arduino for Secret Agents

Chapter 1. A Simple Alarm System with Arduino

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

Hardware and software requirements


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:

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.

Hardware configuration


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.

Configuring the alarm system


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!

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.

Summary


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!

Left arrow icon Right arrow icon

Key benefits

  • • Discover the limitless possibilities of the tiny Arduino and build your own secret agent projects
  • • From a fingerprint sensor to a GPS Tracker and even a robot– learn how to get more from your Arduino
  • • Build nine secret agent projects using the power and simplicity of the Arduino platform

Description

Q might have Bond’s gadgets– but he doesn’t have an Arduino (not yet at least). Find out how the tiny Arduino microcomputer can be used to build an impressive range of neat secret agent projects that can help you go undercover and get to grips with the cutting-edge of the world of espionage with this book, created for ardent Arduino fans and anyone new to the powerful device. Each chapter shows you how to construct a different secret agent gadget, helping you to unlock the full potential of your Arduino and make sure you have a solution for every tricky spying situation. You’ll find out how to build everything from an alarm system to a fingerprint sensor, each project demonstrating a new feature of Arduino, so you can build your expertise as you complete each project. Learn how to open a lock with a text message, monitor top secret data remotely, and even create your own Arduino Spy Robot, Spy Microphone System, and Cloud Spy Camera This book isn’t simply an instruction manual – it helps you put your knowledge into action so you can build every single project to completion.

What you will learn

• Get to know the full range of Arduino features so you can be creative through practical projects • Discover how to create a simple alarm system and a fingerprint sensor • Find out how to transform your Arduino into a GPS tracker • Use the Arduino to monitor top secret data • Build a complete spy robot! • Build a set of other spy projects such as Cloud Camera and Microphone System

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Nov 20, 2015
Length 170 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783986088
Vendor :
Arduino
Category :
Concepts :

Table of Contents

16 Chapters
Arduino for Secret Agents Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewer Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
A Simple Alarm System with Arduino Chevron down icon Chevron up icon
Creating a Spy Microphone Chevron down icon Chevron up icon
Building an EMF Bug Detector Chevron down icon Chevron up icon
Access Control with a Fingerprint Sensor Chevron down icon Chevron up icon
Opening a Lock with an SMS Chevron down icon Chevron up icon
Building a Cloud Spy Camera Chevron down icon Chevron up icon
Monitoring Secret Data from Anywhere Chevron down icon Chevron up icon
Creating a GPS Tracker with Arduino Chevron down icon Chevron up icon
Building an Arduino Spy Robot Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.