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);
}
}
7 Comments
2 years ago
I'm like a super newbie so if anything requires me to figure something out, I'm pretty much getting ready to cry. So, when I copied and pasted the code, there were nonstop errors that I had no idea how to solve when it didn't specifically tell me what to do. If you are in my position, this is not a good project to use because the code has several errors. I tried this code from this project and it had zero errors:
https://create.arduino.cc/projecthub/hassanshettimal/motion-detection-alarm-system-c5fdd5
3 years ago on Step 5
you never declared the value of ledPin in the beginning. this code is crippled. even when i fixed that there were plenty of more errors to be found throughout...
Question 4 years ago
Hi How do i get around the playTone error on the air sensor sketch regads Tony
5 years 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
6 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
7 years ago
Cool! I needed a simple but helpful arduino project to do for my school assignment and I think I've found it!
7 years ago
Very nicely done, thanks for sharing!