Arduino Motion & Proximity Alarm Systems

by dmcits in Circuits > Sensors

4 Views, 0 Favorites, 0 Comments

Arduino Motion & Proximity Alarm Systems

F9FVRMYMC2HRSNT.jpg
FP993D4MC2HRSNE.jpg
FBYBRW9MC2HRSXV.jpg

A beginner-friendly Arduino project featuring two types of alarm systems — one using a PIR motion sensor and one using an ultrasonic distance sensor — to detect movement or nearby objects and trigger light and sound alerts.

Supplies

Arduino Uno

Breadboard

RGB LED (common cathode)

220Ω Resistors

Piezo Buzzer

Pushbutton

Jumper Wires as needed

USB Cable

Choose one sensor to build:

  1. PIR Motion Sensor or Ultrasonic Sensor

Wire Components

Screenshot 2025-06-20 at 1.06.42 PM.png
Screenshot 2025-06-20 at 1.06.52 PM.png

RGB LED:

  1. Red → Pin 3
  2. Green → Pin 5
  3. Blue → Pin 6
  4. Common cathode → GND (with 220Ω resistors on each color)

Buzzer:

  1. Positive → Pin 7
  2. Negative → GND

Button:

  1. One side → Pin 9
  2. Other side → GND
  3. (Use a 10kΩ pull-down resistor if needed)


Motion Sensor Wiring:

  1. OUT → Pin 8
  2. VCC → 5V
  3. GND → GND

Ultrasonic Sensor 4 Pin Wiring (HC-SR04):

  1. Trig → Pin 11
  2. Echo → Pin 12
  3. VCC → 5V
  4. GND → GND

Ultrasonic Sensor 3 Pin Wiring:

  1. Sig → Pin 8
  2. VCC → 5V
  3. GND → GND




Upload Code

Overview

Both codes are designed to:

  1. Detect presence (either motion or proximity)
  2. Trigger an alarm using:
  3. An RGB LED (flashing red)
  4. A piezo buzzer
  5. Let the user silence/reset the alarm with a pushbutton
  6. Flash yellow while "armed" and waiting for detection

PIR Motion Sensor Alarm (Digital Input)

How It Works:

  1. Sensor Used: PIR (Passive Infrared)
  2. Trigger Condition: Detects heat changes from moving people or animals
  3. Sensor Output: HIGH (motion) or LOW (no motion)

Main Flow:

  1. Armed State
  2. LED blinks yellow
  3. If motion is detected (val == HIGH), switch to alarm state
  4. Alarm State
  5. LED flashes red
  6. Buzzer beeps
  7. If button is pressed (digitalRead(buttonPin) == HIGH), reset the alarm
  8. Wait until motion stops to avoid immediate re-triggering

Key Variables:

int val = 0; // Motion sensor reading
bool alarmTriggered = false; // Tracks alarm state

Ultrasonic Proximity Alarm

How It Works:

  1. Sensor Used: Ultrasonic Sensor (3-pin or 4-pin HC-SR04)
  2. Trigger Condition: Detects distance to an object using sound pulses
  3. Sensor Output: Distance in inches (calculated via pulseIn())

Main Flow:

  1. Armed State
  2. LED blinks yellow
  3. Continuously measure distance
  4. If object is closer than distanceThreshold, trigger the alarm
  5. Alarm State
  6. LED flashes red
  7. Buzzer beeps
  8. If button is pressed, turn off alarm
  9. Wait until object moves out of range before rearming

Key Variables:

long duration; // Time for echo to return
float inches; // Calculated distance in inches
bool alarmTriggered = false;

Both codes share the following hardware:

  1. RGB LED (Pins 3, 5, 6) for color signals
  2. Piezo Buzzer (Pin 7) for audible alarm
  3. Pushbutton (Pin 9) to reset the alarm

Shared Utility Functions:

void setColor(int red, int green, int blue) // Set LED color using PWM
void blinkYellow() // "Armed" visual
void flashRed() // "Alarm triggered" visual

Both use analogWrite() to control the RGB LED and tone()/noTone() to activate and stop the buzzer.


Motion Sensor:

Upload the code to your Arduino.

Open the Serial Monitor.

Walk in front of the sensor — it should sound the alarm.

Press the button to deactivate and reset.


// Pin assignments for RGB LED
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

// Pin for piezo buzzer
const int buzzerPin = 7;

// Button pin
const int buttonPin = 9;

// PIR motion sensor pin
const int sensor = 8;

// Variable to store motion sensor state
int val = 0;

// Tracks whether the alarm is currently active
bool alarmTriggered = false;

void setup() {

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(buzzerPin, OUTPUT);

pinMode(buttonPin, INPUT);
pinMode(sensor, INPUT);

Serial.begin(9600);
Serial.println("System armed...waiting for motion.");

}

void loop() {
// Read the current state of the motion sensor
val = digitalRead(sensor);

// If the alarm is NOT currently triggered
if (!alarmTriggered) {

blinkYellow();// Blink the RGB LED yellow to indicate the system is armed

if (val == HIGH) {
//triggers the alarm if motion is detected
Serial.println("Motion detected!");
alarmTriggered = true;
}
}
else {
//alarm IS currently triggered
flashRed();
tone(buzzerPin, 1000); // Play tone at 1000 Hz

// Check if the button is pressed to deactivate the alarm
if (digitalRead(buttonPin) == HIGH) {
// Turn off the buzzer
noTone(buzzerPin);

// Reset the alarm state
alarmTriggered = false;
Serial.println("Alarm deactivated and system resetting");
delay(5000);
Serial.println("System armed...waiting for motion.");

// Wait until the PIR sensor stops detecting motion before rearming
while (digitalRead(sensor) == HIGH) {
delay(100); // Short delay to avoid freezing the system
}

delay(1000); // Small delay to prevent bouncing issues
}
}
}

// Function to set the RGB LED color
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}


void blinkYellow() {
setColor(255, 255, 0); // Yellow
delay(500);
setColor(0, 0, 0); // LED off
delay(500);
}

void blinkBlue() {
setColor(0, 0, 255); // Blue
delay(500);
setColor(0, 0, 0); // LED off
delay(500);
}

void flashRed() {
setColor(255, 0, 0); // Red
delay(200);
setColor(0, 0, 0); // LED off
delay(200);
}


Ultrasonic Sensor:

Upload the code to your Arduino.

Open the Serial Monitor.

Place your hand or an object within 10 inches — the alarm triggers.

Press the button to reset after removing the object.


3 Pin:


// Ultrasonic sensor (3-pin)
const int ultrasonicPin = 8;

// Distance threshold in inches
float distanceThreshold = 10;

// Duration for pulseIn
long duration;

// Distance in inches
float inches;

// Tracks whether the alarm is currently active
bool alarmTriggered = false;

// RGB LED, buzzer, button
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

const int buzzerPin = 7;

const int buttonPin = 9;

void setup()
{
Serial.begin(9600);

// Set up pins
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(buzzerPin, OUTPUT);

pinMode(buttonPin, INPUT);

Serial.println("System armed...waiting for object.");
}

void loop()
{
// Send pulse to ultrasonic sensor
pinMode(ultrasonicPin, OUTPUT);
digitalWrite(ultrasonicPin, LOW);
delayMicroseconds(5);
digitalWrite(ultrasonicPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicPin, LOW);

// Now wait for response
pinMode(ultrasonicPin, INPUT);
duration = pulseIn(ultrasonicPin, HIGH);

// Calculate distance in inches
inches = (duration * 0.0133) / 2;

Serial.print("Distance: ");
Serial.print(inches);
Serial.println(" in");

if (!alarmTriggered) {
// If alarm is not triggered, blink yellow while waiting
blinkYellow();

// If object is within threshold, activate alarm
if (inches < distanceThreshold) {
Serial.println("Object Detected!");
alarmTriggered = true;
}
}
else {
// alarm IS triggered
flashRed();
tone(buzzerPin, 1000);
Serial.println("Alarm triggered.");

// Check if button is pressed to deactivate
if (digitalRead(buttonPin) == HIGH) {
noTone(buzzerPin);
alarmTriggered = false;
Serial.println("Alarm deactivated.");

// Wait until object moves away
while (inches < distanceThreshold) {
// Repeat pulse to check if it's gone
pinMode(ultrasonicPin, OUTPUT);
digitalWrite(ultrasonicPin, LOW);
delayMicroseconds(5);
digitalWrite(ultrasonicPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicPin, LOW);
pinMode(ultrasonicPin, INPUT);
duration = pulseIn(ultrasonicPin, HIGH);
inches = (duration * 0.0133) / 2;

delay(100);
}

Serial.println("System re-armed.");

delay(1000);
}
}
}

void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

void blinkYellow()
{
setColor(255, 255, 0);
delay(500);
setColor(0, 0, 0);
delay(500);
}

void flashRed()
{
setColor(255, 0, 0);
delay(200);
setColor(0, 0, 0);
delay(200);
}


4 Pin:


// Ultrasonic sensor (4-pin)
// Pin assignments for RGB LED
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

// Pin for piezo buzzer
const int buzzerPin = 7;

// Button pin to deactivate alarm
const int buttonPin = 9;

// Ultrasonic sensor pins
const int trigPin = 11;
const int echoPin = 12;

// Distance threshold in inches
const float distanceThreshold = 10;

// Duration for pulseIn
long duration;

// Distance in inches
float inches;

// Tracks whether the alarm is currently active
bool alarmTriggered = false;

void setup()
{
Serial.begin(9600);

// Set up pins
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(buzzerPin, OUTPUT);

pinMode(buttonPin, INPUT);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.println("System armed...waiting for object.");

}

void loop()
{
// Send pulse to trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure pulse duration
duration = pulseIn(echoPin, HIGH);

// Calculate distance in inches
inches = (duration * 0.0133) / 2;

Serial.print("Distance: ");
Serial.print(inches);
Serial.println(" in");

if (!alarmTriggered) {
// If alarm is not triggered, blink yellow while waiting
blinkYellow();

// If object is within threshold, activate alarm
if (inches < distanceThreshold) {
Serial.println("Object Detected!");
alarmTriggered = true;
}
}
else {
// alarm IS triggered
flashRed();
tone(buzzerPin, 1000);
Serial.println("Alarm triggered.");

// Check if button is pressed to deactivate
if (digitalRead(buttonPin) == HIGH) {
noTone(buzzerPin);
alarmTriggered = false;
Serial.println("Alarm deactivated.");

// Wait until object moves away
while (inches < distanceThreshold) {
// Send pulse again
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
inches = (duration * 0.0133) / 2;

delay(100);
}

Serial.println("System re-armed.");

delay(1000);
}
}
}

void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

void blinkYellow()
{
setColor(255, 255, 0);
delay(500);
setColor(0, 0, 0);
delay(500);
}

void flashRed()
{
setColor(255, 0, 0);
delay(200);
setColor(0, 0, 0);
delay(200);
}