Building a motion sensor alarm with Arduino is an easy and inexpensive way to add security to your home or office. With just a few electronic components, you can construct a system that will sound an audible alarm and flash lights when motion is detected. The whole project can be completed in around 15 minutes.
What You Will Need
To build the motion sensor alarm, you will need:
Hardware
-
Arduino Uno - The brains of the alarm that processes the sensor input and triggers the outputs. The Arduino Uno is the most common and inexpensive Arduino board.
-
Passive Infrared (PIR) Motion Sensor - Detects motion in a wide detection angle. PIR sensors allow you to sense motion, usually used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don't wear out.
-
Buzzer - An audible alarm that will sound when motion is detected.
-
LEDs - For visual alarm indicators. Red, green and blue LEDs work well.
-
Resistors - Current limiting resistors for the LEDs. Value depends on LED type, 220 ohms is common.
-
Jumper wires - For making connections between components.
-
Breadboard - To easily connect the circuit without soldering.
-
9V Battery - To power the Arduino. Can also use a 9V DC power adapter.
Tools
- Soldering iron (optional)
- Wire cutters
- Wire strippers
Circuit Diagram
The circuit diagram below shows how all the components are connected.
The PIR motion sensor has three pins - VCC, OUT and GND. VCC and GND are connected to 5V and GND on the Arduino. The OUT pin is connected to Arduino digital pin 2.
The buzzer positive leg is connected to Arduino pin 3 and the negative leg is connected to GND.
The LED positive legs are connected through resistors to Arduino pins 4, 5 and 6. The negative legs are connected to GND.
Setting Up the Hardware
Follow these steps to setup the motion sensor alarm circuit on a breadboard:
-
Insert the Arduino into the breadboard. Connect the GND pin to the negative rail and the 5V pin to the positive rail.
-
Insert the PIR sensor into the breadboard. Connect the VCC pin to the positive rail and the GND pin to the negative rail.
-
Connect the OUT pin on the PIR sensor to Arduino pin 2.
-
Insert the buzzer into the breadboard. Connect the positive leg to Arduino pin 3 and the negative leg to the negative rail.
-
Insert the LEDs into the breadboard. Connect the positive legs through resistors to Arduino pins 4, 5 and 6. Connect the negative legs to the negative rail.
-
Connect the 9V battery to the Arduino VIN and GND pins to power the circuit.
Once the hardware is setup, you are ready to program the alarm.
Arduino Motion Sensor Alarm Code
The Arduino sketch to program the motion sensor alarm is simple. It sets the sensor pin and buzzer pin as outputs, and the LED pins as inputs.
In the loop(), the code reads the state of the sensor. If motion is detected (sensor pin is HIGH), it sounds the buzzer and turns on the LEDs.
Here is the full Arduino code:
```cpp
// Motion Sensor Alarm
int buzzer = 3; // buzzer pin
int redLed = 4; // red LED pin
int greenLed = 5; // green LED pin
int blueLed = 6; // blue LED pin
int sensor = 2; // sensor pin
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(sensor, INPUT);
}
void loop(){
if (digitalRead(sensor) == HIGH) {
// sound alarm
digitalWrite(buzzer, HIGH);
// flash LEDs
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, HIGH);
digitalWrite(blueLed, HIGH);
delay(200);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
delay(200);
}
else {
noTone(buzzer); // turn off alarm
digitalWrite(redLed, LOW); // turn off leds
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
}
}
```
Upload this code to your Arduino board and open the serial monitor. Whenever motion is detected by the sensor, the alarm will sound and the LEDs will flash!
Testing and Using the Motion Sensor Alarm
Once the code is uploaded, do a quick test to ensure it works correctly:
- Wave your hand in front of the sensor - the alarm should sound and LEDs flash
- Block the sensor - the alarm should stop sounding and LEDs turn off
- Adjust the sensitivity if needed with the potentiometer on the PIR sensor
Some ways you can use your DIY motion sensor alarm:
- Security system for home, office or shed
- Intruder alert for boats or RVs
- Animal/pet alert to know when they enter an area
- Automated lighting by triggering lights when motion is detected
The great thing about this project is that it takes just 15 minutes to assemble, but has dozens of practical uses around the home and office!
Conclusion
Building a motion sensor alarm with Arduino is simple and requires minimal components. Within 15 minutes you can construct the hardware circuit and write a basic Arduino program to have it up and running. This is a great beginner Arduino project to learn about inputs, outputs and Arduino code.
The alarm can easily be expanded by adding a camera to take photos, WiFi module to send alerts to your phone, or speakers to play custom alarm sounds. Endless possibilities!
Let me know in the comments if you build this motion sensor alarm or need any help with the project. Now get detecting!