How to Build a Simple Motion Sensor Alarm Using an Arduino and PIR Sensor
Introduction
Building a motion sensor alarm using an Arduino and a passive infrared (PIR) sensor is an easy and fun electronics project that can be completed in under an hour. With just a few components, you can create your own security system to detect intruders and activate an alarm.
In this guide, I will show you step-by-step how to build a simple motion sensor alarm using an Arduino Uno and a PIR sensor. I will cover how PIR sensors work, how to connect the components together on a breadboard, how to program the Arduino, and how to build the alarm system enclosure.
By the end, you will have learned:
- How PIR sensors detect motion using infrared radiation
- How to connect a PIR sensor, LED, buzzer, and resistor to an Arduino
- How to write an Arduino program to detect motion and activate an alarm
- Tips for constructing the alarm enclosure and positioning the sensor
So if you're ready to build your own motion-activated alarm, let's get started!
How PIR Sensors Work
The key component of this motion sensor alarm is the passive infrared (PIR) sensor, which detects motion using infrared radiation.
PIR sensors contain pyroelectric sensors that can detect levels of infrared radiation. Everything emits some low level of radiation, and the hotter something is, the more radiation is emitted. The human body emits a significant amount of infrared radiation, in the form of body heat.
When a human (or other heat-emitting object) passes in front of the PIR sensor, the sensor detects the sudden change in infrared radiation levels. This change triggers the output pin on the PIR sensor to go HIGH (3.3V or 5V on Arduino models).
The PIR sensor has a fresnel lens that helps focus infrared radiation on the pyroelectric sensor. It also has a dome-shaped plastic cover that helps eliminate false alarms from ambient temperature changes.
So in summary, when the PIR sensor detects a sudden change in infrared radiation levels caused by motion, its output pin is triggered HIGH to signal that motion has been detected.
Components Needed
To build the motion sensor alarm, you will need the following components:
- Arduino Uno - The microcontroller board that will run the alarm program code
- PIR motion sensor - To detect motion and trigger the alarm
- Breadboard - To connect the components to the Arduino
- Jumper wires - For breadboard connections
- Buzzer or piezo speaker - To sound the audible alarm
- LED - Visual indicator when alarm is triggered
- Resistor (1k ohm to 220 ohm) - For LED current limiting
- Enclosure (cardboard box) - To house the alarm system
Optionally, you can also add an LCD screen to display alarm messages.
Circuit Diagram
Here is a circuit diagram showing how to connect the components to the Arduino:
The PIR sensor has three pins: GND, VCC, and OUT.
- GND connects to ground
- VCC connects to 5V (can use 3.3V instead)
- OUT is the output pin that goes HIGH when motion is detected.
The buzzer, LED, and resistor connect to any digital GPIO pin on the Arduino.
Setting Up the Hardware
Follow these steps to setup the hardware for the motion sensor alarm:
-
Insert the Arduino Uno into your breadboard. Make sure it is oriented with the USB port lining up with the notch on the breadboard.
-
Connect the PIR sensor pins to the breadboard.
- GND to GND rail
- VCC to 5V rail
-
OUT to digital pin 2
-
Connect the positive leg of LED to digital pin 6.
-
Connect 220ohm resistor from negative leg of LED to GND rail.
-
Connect the positive lead of buzzer to digital pin 10.
-
Connect negative lead of buzzer to GND rail.
Double check that all components match the circuit diagram connections.
Loading the Arduino Program
With the hardware connected up, it's time to load the Arduino program that will monitor the PIR sensor to detect motion and trigger the alarm.
Here is the full program code:
```arduino
// Motion Sensor Alarm by Circuit Basics
// Attach motion sensor to pin 2
int pirPin = 2;
// Buzzer connected to pin 10
int buzzerPin = 10;
// LED on pin 6
int ledPin = 6;
void setup() {
// Init serial connection
Serial.begin(9600);
// Set LED pin as output
pinMode(ledPin, OUTPUT);
// Set buzzer pin as output
pinMode(buzzerPin, OUTPUT);
// Set motion sensor pin as input
pinMode(pirPin, INPUT);
// Print startup message
Serial.println("Motion Sensor Alarm Active");
}
void loop(){
// Read motion sensor value
int sensorValue = digitalRead(pirPin);
// If motion detected
if(sensorValue == HIGH) {
// Sound alarm
digitalWrite(buzzerPin, HIGH);
// Turn on LED
digitalWrite(ledPin, HIGH);
// Print message
Serial.println("Motion Detected!");
// Delay to avoid overloading
delay(500);
// If no motion
} else {
// Turn off alarm
digitalWrite(buzzerPin, LOW);
// Turn off LED
digitalWrite(ledPin, LOW);
}
}
```
To summarize, the code first sets up the IO pins and prints a startup message. In the main loop, it reads the PIR sensor pin. If motion is detected (pin goes HIGH), it turns on the LED, sounds the buzzer, and prints a message. It delays for 500ms to avoid over triggering. If no motion is detected, it turns the LED and buzzer off.
Follow these steps to upload the program:
-
Copy the code into the Arduino IDE.
-
Verify the code compiles with no errors.
-
Upload the program to your Arduino board.
-
Open the Serial Monitor at 9600 baud to see debug messages.
Once uploaded, the alarm system will start running! Wave your hand in front of the PIR sensor to test it.
Constructing the Enclosure
To build a nice alarm system enclosure, you can follow these tips:
-
Use a cardboard box, plastic container, or wood to create the main enclosure.
-
Cut an opening on one side for the PIR sensor to fit through. Position it at the top, angled down.
-
Drill small holes for the buzzer wires and LED wires to feed through.
-
Hot glue or tape the Arduino, breadboard, and other components securely inside the box.
-
Consider adding a power switch and 9V battery connector so it can operate standalone.
-
Mount the enclosure high on a wall aiming down, for best PIR sensor coverage.
Positioning is important for the PIR sensor. Point it at the area you want to detect motion, keeping in mind its conical sensing pattern.
Usage Tips
Here are some tips for using your motion sensor alarm:
-
Let the PIR sensor warm up for 1-2 minutes before testing motion detection.
-
Start with the PIR sensitivity potentiometer at minimum. Slowly increase it until you get reliable motion detection without false alarms.
-
Try different resistor values on the LED to change brightness. Lower values make a brighter LED.
-
For the buzzer, you can substitute different tones or noises by adding a resistor or capacitor.
-
Consider adding a relay to connect the alarm to bigger speakers, lights, or other devices.
-
To improve range and coverage, add multiple PIR sensors connected to different Arduino pins.
-
Experiment with creative housings and positioning to customize the alarm for different applications.
And that covers the basics of building your own motion sensor alarm with Arduino! Let me know if you have any other questions.