Introduction: How to Make a Arduino Pir Sensor Alarm

About: I am a 14 year old kid that is interested in mechanics and electronics. I build and fix things from earphones to cars.

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!