You are on page 1of 8

ECE 2030

Arduino Based System Design

Daw Khaing Su Wai


Experiment 14

Building an Intrusion alarm system


(Motion sensor and speaker)
Building an Intrusion alarm system
Components
• Arduino UNO Board
• PIR Sensor
• Buzzer

https://arduinogetstarted.com/tutorials/arduino-motion-sensor-piezo-buzzer
Building an Intrusion alarm system
Circuit Diagram

https://arduinogetstarted.com/tutorials/arduino-motion-sensor-piezo-buzzer
Building an Intrusion alarm system

• If the motion is detected, make sound.


• If the motion is not detected, stop making sound.
Building an Intrusion alarm system
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-motion-sensor-piezo-buzzer
*/

const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to the OUTPUT pin of motion sensor
const int BUZZER_PIN = 3; // Arduino pin connected to Buzzer's pin
int motionStateCurrent = LOW; // current state of motion sensor's pin
int motionStatePrevious = LOW; // previous state of motion sensor's pin

void setup() {
Serial.begin(9600); // initialize serial
pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}
Building an Intrusion alarm system
void loop() {
motionStatePrevious = motionStateCurrent; // store old state
motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state

if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state


change: LOW -> HIGH
Serial.println("Motion detected!");
digitalWrite(BUZZER_PIN, HIGH); // turn on
}
else
if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state
change: HIGH -> LOW
Serial.println("Motion stopped!");
digitalWrite(BUZZER_PIN, LOW); // turn off
}
Building an Intrusion alarm system

Exercise

• If the motion is detected, make sound, blink LED and print


“Motion Detected!” on LCD.

You might also like