Will_the_Star
Published © GPL3+

Ultrasonic Tripwire

Create a burglar-tripwire with the ultrasonic sensor.

BeginnerFull instructions provided21,307
Ultrasonic Tripwire

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Male/Male Jumper Wires
×1
Buzzer
Buzzer
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Computer

Story

Read more

Schematics

How to make the circuit

Code

Tripwire with Nano and Ultrasonic

C/C++
#define trigPin 9
#define echoPin 10
long duration;
int distance;
long firstoff;
int distancefirst;

void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(1000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
firstoff = pulseIn(echoPin, HIGH);
distancefirst = firstoff*0.034/2; // cm
}

void loop()
{

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2; // cm
//distance = duration * 0.0133 / 2; // in
Serial.println(distance);
delay(1000);
Serial.println(distancefirst);



if (distance <= distancefirst - 5)
{
tone(3, 500, 500);
delay(500);
tone(3, 800, 500);
delay(500);
delay(50);
}
}

Credits

Will_the_Star

Will_the_Star

6 projects • 26 followers
I am eleven years old. I have a proven IQ of 154. I am not lying, joking, kidding or trying to mislead you in any other way.

Comments