← Previous Invention

Wakey Wakey

Next Invention →

Decision Maker


Project #13

Burglar Alarm

This invention uses a distance sensor that triggers when someone walks past, setting off an alarm. You can use this to guard your bedroom, give someone a fright, or for actually scaring off real burglars!

The Kit

The Amazing Annoyatron

Build this invention, and many more, with The Amazing Annoyatron!

What you’ll need


Brain Board




Ultrasonic Sensor


4 x Wires

Step 1

Let’s build this!

Burglar Alarm
This is the positive leg of the buzzer. It will have a ‘+’ sign on top of it and will also be longer than the other leg.
Don’t forget to plug the board into your computer with the included USB cable.


The +LEG is the longer leg.

+ LEG11

Light Sensor

Use wires to join these.


Step 2

Code some chaos!

Don’t forget to select your port, like ususal

Copy and paste the sample code

int distance() {
  int16_t array[3] = {};
  for(int count = 0; count < 2; count++){
    float duration, distance;
    digitalWrite(7, HIGH);
    digitalWrite(7, LOW);
    duration = pulseIn(6, HIGH) / 2 * 0.0544;
    array[count] = duration;
  int measure = array[0] += array[1] += array[2];
  return measure / 3;
int repeat = 4;
void setup() {
  pinMode(7, OUTPUT);
  pinMode(6, INPUT);
  pinMode(11, OUTPUT);
void loop() {
  if (distance() < 10) {
    for (int count = 0; count < repeat; count++) {
      digitalWrite(11, HIGH);
      digitalWrite(11, LOW);

Step 3

Modify the madness!

Change how far away the alarm triggers

In the program, the alarm is triggered when someone comes closer than 10cm of the ultrasonic distance sensor. Take a look at line 22 in the code where this happens:

if (distance() < 10) { // If something is closer than 10 cm. . .

If we change the code on line 22, then we make the trigger distance shorter or longer. For example, if we change the number 10 in that line to something like 22, anything that came within 50cm of the sensor would set off the alarm. Here’s what that would look like:

if (distance() < 50) { // If something is closer than 50 cm. . .

Change how long the alarm stays on

Once the alarm has been triggered by someone (or something) coming too close to the ultrasonic distance sensor, we can control exactly how long it stays on for afterwards. In our program, we set the alarm to beep ten times before resetting, ready to be triggered again. Take a look at line 13:

int repeat = 10; // Trigger the alarm for 10 beeps

By changing this code, we can either increase or decrease the number of beeps that the alarm stays on for when triggered. If we wanted it to stay on for 20 beeps, we would use the following code:

int repeat = 20; // Trigger the alarm for 20 beeps

Change the sound of the alarm

We can change the tone – or in other words, the sound – of the beeping using something called the tone() function. Take a look at the code on lines 24-27, which is what we are currently using for our beeps:

digitalWrite(11, HIGH);
digitalWrite(11, LOW);

To be able to change the tone of the beeps, replace lines 24-27 of your program with the following code:

tone(11, 1000, 500);

The following code works like this: tone(buzzer, pitch, length). Replace the word pitch with any number you like, and this will control the sound of the beep. Replace the word length with exactly how long you want the beep to go for in milliseconds.

Here’s a different beeping sound that you can try out on your project:

tone(11, 5000, 750);