loading

This will be a tutorial on how to use a PIR motion sensor with Arduino to make a basic alarm. This is a very basic project for anybody, and all you need to know is Arduino and basic components, which is nothing almost.

Step 1: What You Need?

You will need

Arduino

USB cable

PIR motion sensor

wires

LED

breadboard

Step 2: What Is a PIR Sensor

A PIR sensor / passive infrared sensor is a electronic sensor that measures infrared light radiated from objects in field of view. Used in burglar alarms because of 30 foot range. You can set them to guard a certain area and range.

Step 3: The Circuit

This is the circuit, the black part that looks like eyes is the PIR sensor. the yellow wire is the output pin from the sensor which goes to pin 12, the green wire is the positive pin of the buzzer which goes to pin 11, then the red and black wires are power wires that go to 5 volts and ground.

Step 4:

Step 5: Code

int ledPin = 13; // choose the pin for the LED
int inputPin = 12; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status int pinSpeaker = 11; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input pinMode(pinSpeaker, OUTPUT); Serial.begin(9600); }

void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON playTone(900, 900); delay(100);

if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; } } else { digitalWrite(ledPin, LOW); // turn LED OFF playTone(0, 0); delay(300); if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; } } } // duration in mSecs, frequency in hertz 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); } }

This is the code, upload it to the Arduino and watch the magic happen!

Step 6: Closing

I hope this tutorial helped you on to making a PIR sensor alarm for the first time. when motion is detected the led will blink and the buzzer will go off. If you need help post in the comments.BYE!

<p>thx proffesor</p>
What does pirstate=HIGH mean in the code
<p>Hi, <br>don't take what i'm going to write as criticism made by a &quot;professor&quot;, i just wanna help you to improve your instrutable. First thing: I think that your code dosn't match the circuit diagram. You declare pin 10 as led pin, but led is plugged in led 13 in the schematic (where for the code should be a buzzer or speaker). Second problem: never connect led without resistor (i know that pin 13 has on board one but there colud be absolute beginner that changes led position and burn out led/arduino and not all arduino boards for example not original one has it). The third problem is that the code is not formatted well and is difficult to read, understand and pasted into arduino won't compile. <a href="http://pastebin.com/PKXjzXNw" rel="nofollow">http://pastebin.com/PKXjzXNw </a> Here is a better version. Last thing, in the sketch you say that speaker need PWM pin, but then you make a software pwm so you don't need PWM pin, any pins will work.<br>Sorry if i was too &quot;negative&quot; and again, don't look at my as a professor which want to know everything. Bye.</p>

About This Instructable

5,789views

45favorites

Bio: I am a 14 year old kid that is interested in mechanics and electronics. I build and fix things from earphones to cars.
More by coolyyz154:Simple Way to Convert Stereo to Mono How To Make A Simple Arduino Ultrasonic Distance Sensor How to make a Arduino pir sensor alarm 
Add instructable to: