MylesBartlett
Published © CC BY-SA

Ultrasonic Security System with Object Distance Display

A measuring device that also doubles as a burglar alarm, using an ultrasonic sensor and LCD.

BeginnerFull instructions provided4,350
Ultrasonic Security System with Object Distance Display

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Buzzer
Buzzer
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
I used 220 ohm resistors
×5
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1

Software apps and online services

Arduino IDE
Arduino IDE
Note that if the other is used, this is not required.
Arduino Web Editor
Arduino Web Editor
Note that if the other is used, this is not required.

Story

Read more

Schematics

Finished schematic

The Fritzing schematic of the finished circuit

Code

Measuring_tape_security_system

Arduino
This is the code that makes the Arduino function in the way intended for the project. Upload it using the Arduino web editor, or the Arduino IDE.
const int trigPin = 2;
const int echoPin = 3;
const int LEDlampRed = 4;
const int LEDlampYellow = 5;
const int LEDlampGreen = 6;
const int buzzer = 7;
int sound = 500;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
long durationindigit, distanceincm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durationindigit = pulseIn(echoPin, HIGH);
distanceincm = (durationindigit*0.034) / 2;

if (distanceincm > 50) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed,LOW);
noTone(buzzer);
}
else if (distanceincm <= 50 && distanceincm > 20) {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed,LOW);
noTone(buzzer);
}
else if (distanceincm <= 20 && distanceincm > 5) {
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampRed,LOW);
tone(buzzer, 500);
}
else if (distanceincm <= 0) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, HIGH);
tone(buzzer, 1000);
digitalWrite(LEDlampRed, HIGH);
delay(300);
digitalWrite(LEDlampRed, LOW);
}

Serial.print(distanceincm);
Serial.println(" cm");

delay(300);
}

Credits

MylesBartlett

MylesBartlett

1 project • 0 followers

Comments