Detect motion, then scare the hell out of an intruder with high-pitched alarms and flashing lights. Sounds fun? Of course it is. That’s the goal of today’s beginner-friendly Arduino project. We’ll be writing from scratch and testing as we go so you can hopefully get some idea of ​​how it’s all done rather than just installing what I’ve already done.

Disclaimer: This is not going to actually protect your home. it maybe give your sister a nasty shock when she sneaks into your room.

You’ll need:

  • Arduino
  • Ultrasonic «ping» sensor, I use HC-SR04 A PIR will be better, but it’s expensive. A ping sensor can be secretly placed in a doorway and still do the same basic job, and only costs $5.
  • Piezo buzzer
  • LED strip, with the same wiring we used in this project. .

Since you are including this project, don’t delete everything every time — just keep working on the last block. By the time you get to the «Coding The Alarm System» section, you should have all the pieces and pieces that look something like this:

completed postings

flashing lights

Use the wiring diagram from this project. to connect the LED strip; don’t change the pins as we need a PWM output. Use this code to quickly test your wiring. If everything goes well, you should have this:

LED-RGB test

Distance sensor

On the SR04 module you will find 4 pins. VCC and GND connect to bus + 5V and ground, respectively; TRIG — this is the pin used to send the sonar signal, place it on pin 6; ECHO used to read the signal back (and therefore calculate the distance) — set this value to 7.


To make things incredibly simple, there is a library we can use called NewPing . Download and put in a folder libraries Arduino and restart the IDE before continuing. Check using this code ; open the serial monitor and make sure the baud rate is set to 115200. If you’re lucky, you should see some distance measurements being sent to you at a fairly high speed. You may find a deviation of 1 or 2 centimeters, but this is normal. Try moving your hand in front of the sensor, moving it up and down to see the readings change.

ping output

The code should be simple enough to understand. There are a few declarations about the relevant pins at the start, including the maximum distance — this can vary depending on exactly what kind of sensor you have, but if you can get accurate readings of less than 1 meter, you should be fine.

In the loop of this test application, we use the function ping() to send the echo sounder, returning a value in milliseconds of how long it took to return the value. To understand this, we use the NewPing libraries built into the constant US_ROUNDTRIP_CM which specifies how many microseconds it takes to travel one centimeter. There is also a 50ms delay between pings to avoid overloading the sensor.

Piezo alarm clock

The piezoelectric sensor is a simple and cheap buzzer and we can use PWM pin 3 to create different tones. Connect one wire to pin 3, the other to the ground bar, no matter which one.

Use this code for testing.

The only way to kill a rather obnoxious and loud alarm is to pull the plugs. The code is a bit difficult to explain, but it does involve using sine waves to generate a distinctive sound. Adjust the numbers to play different tones.

Alarm system coding

Now that we have all the pieces of this puzzle, let’s put them together.

Go ahead and make a new sketch called » Anxiety» . Start by concatenating all the variables and pin definitions we’ve used in the test cases so far.

#include  // 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; 

Start by writing a basic function setup() For now, we will deal only with indicators. I added a 5 second delay before starting the main loop to give us some time to get out of the way if necessary.

 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 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 will consist of a simple color burst between red and yellow (or whatever you want your alarm clock to be — just change the RGB values).

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

Download and check it out to make sure you’re on the right track.

Now let’s integrate a distance sensor to turn on these lights only when something is, say, 50 cm (slightly less than the width of the door frame). We have already defined the correct pins and imported the library, so before your function setup() add the following line to create it:

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

Below that, add a variable to store the state of the alarm being triggered or not, of course the default is false.

 boolean triggered = false; 

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

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

Next, let’s rename the current loop to alarm() is what will be called if the alarm is triggered.

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

Now create a new function loop() where we get a new ping, read the results, and trigger an alarm if something is found within the range of the counter.

 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 =; // 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 briefly explain the code:

  • Start by checking if the alarm has gone off and, if so, disable the alarm feature (just flashing for the moment).
  • If that hasn't worked yet, get the current reading from the sensor.
  • If the sensor reads <100 cm, something has hit the beam (obviously adjust this value if it fires too soon).

Give it a trial now before we add the pesky piezo buzzer.

Working? Excellent. Now let's add this buzzer back. Add pinMode into the procedure setup() .

 pinMode(ALARM, OUTPUT); 

Then add a piezo buzzer 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 will run into an error - I left this out on purpose so you can see some common problems. In this case, both the NewPing library and the standard tone library use the same interrupts - they basically conflict, and there's not much you can do to fix that. Oh my God.

Don't worry though. This is a common problem and someone already has a solution - download and add this NewTone to your Arduino Libraries folder. Adjust the start of your program to include this:


And adjust the line:

 tone(ALARM, toneVal); 


 NewTone(ALARM, toneVal); 

instead of.

That's all. Set an alarm at your bedroom door for the next unfortunate would-be burglar.

Or a sluggish dog who seemed completely indifferent to anxiety.

Having problems with the code? Here is the complete application. If you're getting random errors, try pasting them below and I'll see if I can help.

Image Credit: Fire alarm via Flickr

Похожие записи