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:
PIR Motion Sensoror Ultrasonic Sensor
Wire Components
RGB LED:
Red → Pin 3
Green → Pin 5
Blue → Pin 6
Common cathode → GND (with 220Ω resistors on each color)
Buzzer:
Positive → Pin 7
Negative → GND
Button:
One side → Pin 9
Other side → GND
(Use a 10kΩ pull-down resistor if needed)
Motion Sensor Wiring:
OUT → Pin 8
VCC → 5V
GND → GND
Ultrasonic Sensor 4 Pin Wiring (HC-SR04):
Trig → Pin 11
Echo → Pin 12
VCC → 5V
GND → GND
Ultrasonic Sensor 3 Pin Wiring:
Sig → Pin 8
VCC → 5V
GND → GND
Upload Code
Overview
Both codes are designed to:
Detect presence (either motion or proximity)
Trigger an alarm using:
An RGB LED (flashing red)
A piezo buzzer
Let the user silence/reset the alarm with a pushbutton
Flash yellow while "armed" and waiting for detection
PIR Motion Sensor Alarm (Digital Input)
How It Works:
Sensor Used: PIR (Passive Infrared)
Trigger Condition: Detects heat changes from moving people or animals
Sensor Output: HIGH (motion) or LOW (no motion)
Main Flow:
Armed State
LED blinks yellow
If motion is detected (val == HIGH), switch to alarm state
Alarm State
LED flashes red
Buzzer beeps
If button is pressed (digitalRead(buttonPin) == HIGH), reset the alarm
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:
Sensor Used: Ultrasonic Sensor (3-pin or 4-pin HC-SR04)
Trigger Condition: Detects distance to an object using sound pulses
Sensor Output: Distance in inches (calculated via pulseIn())
Main Flow:
Armed State
LED blinks yellow
Continuously measure distance
If object is closer than distanceThreshold, trigger the alarm
Alarm State
LED flashes red
Buzzer beeps
If button is pressed, turn off alarm
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:
RGB LED (Pins 3, 5, 6) for color signals
Piezo Buzzer (Pin 7) for audible alarm
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