Introduction: 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.

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);
Serial.begin(9600);
}

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


if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW);
playTone(0, 0);
delay(300);
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) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}