Surilli
Published © LGPL

Calculate Distance Using Ultrasonic Sensor (HC-SR04)

Calculate distance to a certain object using an ultrasonic sensor. It can be used for car distance alerts, security systems etc.

BeginnerFull instructions provided30 minutes1,870
Calculate Distance Using Ultrasonic Sensor (HC-SR04)

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×4
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Surilli WiFi
Surilli WiFi
×1

Software apps and online services

Arduino IDE
Arduino IDE
Blynk
Blynk

Story

Read more

Schematics

Untitled file

File missing, please reupload.

Code

Untitled file

C/C++
const int trigPin = 2;
const int echoPin = 16;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delay(500);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(500);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Credits

Surilli

Surilli

196 projects • 43 followers
Surilli is a premiere Internet of Things centric Technology Company aimed at providing cutting edge innovative solutions.

Comments

Add projectSign up / Login