The Arduino Alarm System

We learned this one over at Make Use Of! We’ve made Arduino Alarm Systems before, but this is probably our favorite setup. The LED flashing adds a really nice effect.

While you probably won’t use this exact setup as an actual alarm system, you could definitely refine the build and link a few together and actually make a relatively useful motion detection security system.

I personally made one of these to detect when my new puppy gets up from sleeping under my chain and tries to sneak a pee in the house. Pop one of these by the door and BEEEP, Melly is walking out of the room.

Anyway, let’s get started.

 

Things You’ll Need

  • An Arduino
  • A piezo buzzer or speaker
  • Ultrasonic motion sensor. These are pretty cheap and work relatively well.
  • MakeUseOf suggests and LED strip light like this one, but you can also wire this up with a few regular LEDs.
finished-wiring
Photo: Make Use Of

Setup The LED Strip

Use the circuit diagram below from MakeUseOf to wire up your LED strip. Ignore everything but the LED strip in the upper right. You’ll be connecting the 12v directly to your power supply, the G pin to the #11 PWM input, R pin to the #10 PWM input and the B pin to the #9 PWM input on your Arduino Uno.

media center light
Photo: MakeUseOf

Distance Sensor

On the SR04 module, you’ll find 4 pins. VCC and GND go to +5V rail and ground respectively; TRIG is the pin used to send a sonar signal, put this on pin 6; ECHO is used to read the signal back (and therefore calculate the distance) — put this on 7.

sr04

To make things incredibly simple, there’s a library we can use called NewPing. Download and place in your Arduino’s Library folder and restart the IDE before continuing. Test using this code; open up the serial monitor and make sure the speed is set to 115200 baud. With any luck, you should see some distance measurements being send back to you at a pretty high speed. You may find a variance of 1 or 2 centimeters, but this is fine. Try running your hand in front of the sensor, moving it up and down to observe the changing readings.

MakeUseOf

 

 

Set Up The Piezo Alarm

To Set Up the Piezo alarm you’ll need to connect #3 PWM to a pin on the alarm and one to the blue ground rail.

To stop the alarm, you’ll need to pull the plug from you’re Arduino.

The Complete Code

Everything should be wired up now and working on and individual basis. Now it’s time to put them all together in a single sketch and make them work in unison.

Make a new sketch, called Alarm and follow the step for the code from MakeUseOf below!

Start by combining all the variables and pin definitions we’ve in the test examples until now.

#include <NewPing.h>

// Select which PWM-capable pins are to be used.
#define RED_PIN    10
#define GREEN_PIN   11
#define BLUE_PIN  9

#define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters).

#define ALARM 3

float sinVal;
int toneVal;

Begin by writing a basic setup() function – we’ll only deal with the lights for now. I’ve added a 5 second delay before the main loop is started to give us some time to get out of the way if needed.

void setup(){

   //set pinModes for RGB strip
   pinMode(RED_PIN,OUTPUT);
   pinMode(BLUE_PIN,OUTPUT);
   pinMode(GREEN_PIN,OUTPUT);

   //reset lights
   analogWrite(RED_PIN,0);
   analogWrite(BLUE_PIN,0);
   analogWrite(RED_PIN,0);

  delay(5000); 
}

Let’s use a helper function that allows us to quickly write a single RGB value out to the lights.

//helper function enabling us to send a colour in one command
void color (unsigned char red, unsigned char green, unsigned char blue)     // the color generating function
{     
    analogWrite(RED_PIN, red);     
    analogWrite(BLUE_PIN, blue);
    analogWrite(GREEN_PIN, green);
}

Finally, our loop for now is going to consist of a simple color flash between red and yellow (or, whatever you want your alarm to be — just change the RGB values).

void loop(){
   color(255,0,0); //red
   delay(100);
   color(255,255,0); //yellow
   delay(100);
}

Upload and test that to ensure you’re on the right track.

Code Continued

Now, let’s integrate the distance sensor to trigger those lights only when something comes within, say, 50 cm (just less than the width of a door frame). We’ve already defined the right pins and imported the library, so before your setup() function add the following line to instantiate it:

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

Underneath that, add a variable to store the state of the alarm being triggered or not, defaulting to false, of course.

boolean triggered = false; 

Add a line to the setup() function so we can monitor the output on serial and debug.

Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.  

Next, let’s rename the current loop to alarm() – this is what’s will be called if the alarm has been tripped.

void alarm(){
   color(255,0,0); //red
   delay(100);
   color(255,255,0); //yelow
   delay(100);
}

Now create a new loop() function, one in which we fetch a new ping, read the results, and trigger the alarm if something is detected within the meter range.

void loop(){
    if(triggered == true){
      alarm();
    }
    else{
      delay(50);// Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
      unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
      unsigned int distance = uS / US_ROUNDTRIP_CM;
      Serial.println(distance);
      if(distance < 100){
         triggered = true;
      }
   }
}

Let me explain the code briefly:

  • Start by checking to see if the alarm has been triggered, and if so, fire off the alarm function (just flashing the lights at the moment).
  • If it’s not triggered yet, get the current reading from the sensor.
  • If the sensor is reading <100 cm, something has padded the beam (adjust this value if it’s triggering too early for you, obviously).

Give it a trial run now, before we add the annoying piezo buzzer.

Working? Great. Now let’s add that buzzer back. Add pinMode to the setup() routine.

pinMode(ALARM, OUTPUT);

Then add the piezo buzzer loop to the alarm() function:

for (int x=0; x<180; x++) {
    // convert degrees to radians then obtain sin value
    sinVal = (sin(x*(3.1412/180)));
    // generate a frequency from the sin value
    toneVal = 2000+(int(sinVal*1000));
    tone(ALARM, toneVal);
  }

If you try to compile at this point, you’re going to run into an error — I’ve left this in deliberately so you can see some common issues. In this case, both the NewPing and standard tone library use the same interrupts — they are conflicting basically, and there’s not a lot you can do to fix it. Oh dear.

No worries though. It’s a common problem, and someone has a solution already — download and add this NewTone to your Arduino Libraries folder. Adjust the beginning of your program to include this:

#include <NewTone.h>

And adjust the line:


  tone(ALARM, toneVal); 

to


    NewTone(ALARM, toneVal);

instead.

That’s it. Set your alarm up in the doorway of your bedroom for the next hapless would-be burglar.

Or, a dopey dog, which seemed completely unfazed by the alarm.

You can simply copy and paste the code if you’d like to. Grab it here.
If you run into any issue, feel free to drop a comment and we’ll try to help you out! Or head on over to the original MakeUseOf guide, there are quite a few comments troubleshooting common problems, and you can drop a comment there as well!