Introduction: Intruder Sensor - Arduino

If you are looking for a cheaper security alarm or a detector that warns when someone is getting closer this project is perfect for you. It is easy and you can do it at home. This project is based on the “project 18: Intruder Sensor” of the book Arduino Project Handbook by Mark Geddes.
What you will need for this Project:

Arduino Board


Jumper Wires

Four-pin-HC-SR04 ultra-sonic sensor



Green LED

2 220 - ohm resistors

Step 1: Step 1: Building the Project

After having all the materials required you will need to connect the Breadboard to the Arduino. The ultra-sonic sensor has four pins, GND, VCC, Trig and Echo to connect this pins to the arduino you must insert the sensor into the Breadboard, and use the jumper wires to connect them. Connect the sensor’s GND to the arduino’s GND with a jumper wire, the VCC to the +5V, the Trig’s pin to the Pin 12 and the Echo to the Pin 13.

Step 2: Step 2

Then, you will connect the Servomotor wires to Arduino. The red wire goes to the arduino’s +5V, the brown wire to the arduino’s GND and the yellow/orange to the Pin 9.

Step 3: Step 3

The 3 step is basically connecting the leds to the arduino. Insert the green and red LEDs in the breadboard with the negative legs in the GND’s arduino rail, add a 220-ohm resistor to the positive legs and connect the red LED to the pin 2 of Arduino, and the green LED to the pin 3 via resistors.

Step 4: Code

The code is the same as the book’s one, and basically explain the function of each part and how to do the project:
#include // Call NewPin library #include // Call Servo library #define trigPin 12 // Trig pin connected to Arduino 12 #define echoPin 13 // Trig pin connected to Arduino 13 #define MAX_DISTANCE 500 NewPing sonar(trigPin, echoPin, MAX_DISTANCE); // Library setting int greenLed = 3, redLed = 2; // Set green LED to pin 3, red to pin 2 int pos = 20; Servo myservo; void setup() { Serial.begin (115200); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(greenLed, OUTPUT); pinMode(redLed, OUTPUT); myservo.attach(9); // Servo attached to pin 9 } void loop() { int duration, distance, pos =0, i; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); // Trig pin sends a ping delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); // Echo receives the ping distance = (duration / 2) / 29.1; Serial.print(distance); Serial.println(" cm"); if (distance <= 15) { digitalWrite(greenLed, LOW); digitalWrite(redLed, HIGH); myservo.write(180); delay(450); digitalWrite(redLed, LOW); myservo.write(90); delay(450); digitalWrite(redLed, HIGH); myservo.write(0); delay(450); digitalWrite(redLed, LOW); myservo.write(90); } else { digitalWrite(redLed, LOW); digitalWrite(greenLed, HIGH); myservo.write(90); } delay(450); }