Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Evan Rust
Published © GPL3+

Arduino PIR Intruder System!

A simple alarm system that detects people and tells the distance from them.

IntermediateFull instructions provided1 hour3,913
Arduino PIR Intruder System!

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Ultrasonic Distance Sensor
×1
Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
I used Red
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Connect as-is

Code

Arduino Code

C/C++
Just copy and paste into your Arduino IDE.
int pingPin = 3;
int echoPin = 2;
int PIRPin = 4;
long duration = 0;

long Ping(){
  digitalWrite(pingPin,LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin,HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin,LOW);
  duration = pulseIn(echoPin,HIGH);
  return duration;
}
void blinkLED(){
  for (int x=0;x < 10;x++){
    digitalWrite(13,HIGH);
    delay(250);
    digitalWrite(13,LOW);
    delay(250);
}
}
long microsecondstoinches(long microseconds){
  return microseconds / 74 / 2;
}

void setup() {
  pinMode(13,OUTPUT);
  pinMode(pingPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(PIRPin, INPUT);
  Serial.begin(9600);

}

void loop() {
  if (digitalRead(PIRPin) == HIGH){
    int distance = Ping();
    int lengths = microsecondstoinches(distance);
    Serial.println(lengths);
    Serial.println("Someone has been detected!");
    blinkLED();
    digitalWrite(13,LOW);
    delay(5000);
  }
}

Credits

Evan Rust

Evan Rust

122 projects • 1093 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments