Arduino Motion Sensing Alarm




This Instructable will show you how to make a motion sensing alarm using an Arduino microcontroller. This Instructable accompanies the video 'Making a Motion Sensing Alarm', which I have embedded.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: What Components You Will Need

For this Arduino prject you will need:

1x Arduino Uno

1x PIR Motion Sensor

1x LED

1x Piezo Buzzer

1x Breadboard

1x Jumper Wires

You will also need a computer (minimum operating system XP) with the Arduino IDE installed.

Step 2: Wiring the PIR Sensor

The PIR Sensor has three pins:

- The one on the left is GND (needs to be connected to a GND pin)

-The one in the middle is OUT (needs to be connected to a digital pin)

- The one on the right is VCC (needs to be connected to 5v)

Wire it to your Arduino's digital pin 2 as the diagram shows.

Step 3: Wire the Piezoelectric Buzzer

The piezo buzzer has 2 pins:

- One is GND (needs to be wired to a ground pin on the Arduino)

- One needs to be connected to a digital pin, so we can decide it's tone

Step 4: Wire the LED

The LED has two pins, the ANODE and the CATHODE. The Anode is longer and is always wired to positive voltage. The Cathode is shorter and always wired to negative voltage.

Wire the Anode to pin 13, and the Cathode to the GND pin next to it. You don't have to put it on the breadboard, as there is no need to.

Step 5: Upload the Sketch to Your Arduino Board

int inputPin = 2;
int pirState = LOW;
int val = 0;
int pinSpeaker = 10;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(pinSpeaker, OUTPUT);

void loop(){
val = digitalRead(inputPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
playTone(300, 160);

if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
} else {
digitalWrite(ledPin, LOW);
playTone(0, 0);
if (pirState == HIGH);
Serial.println("Motion ended!");
pirState = LOW;
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);

Be the First to Share


    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    5 Discussions


    Question 8 months ago

    Hi How do i get around the playTone error on the air sensor sketch regads Tony


    1 year ago

    I am having the same problem as 'Squishtech' and It highlights the line playTone(0, 0); and says 'playTone' was not declared in this scope.

    Here are all the error messages:

    /Users/minkyi/Documents/Arduino/sketch_dec27a/sketch_dec27a.ino: In function 'void loop()':

    sketch_dec27a:19: error: 'playTone' was not declared in this scope

    playTone(300, 160);


    sketch_dec27a:29: error: 'playTone' was not declared in this scope

    playTone(0, 0);


    /Users/minkyi/Documents/Arduino/sketch_dec27a/sketch_dec27a.ino: At global scope:

    sketch_dec27a:36: error: expected declaration before '}' token



    exit status 1

    'playTone' was not declared in this scope


    2 years ago

    Hi I'm just beginning (and loving) the Arduino mission. I've worked through the very basic sketches but I just can't seem to get this sketch to compile, I copied it exactly as shown but get the error 'playtone' was not declared in this scope. Any help would be greatly appreciated


    3 years ago

    Cool! I needed a simple but helpful arduino project to do for my school assignment and I think I've found it!


    3 years ago

    Very nicely done, thanks for sharing!