Surilli
Published © LGPL

Calculate Distance Using Ultrasonic Sensor and Surilli Basic

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

BeginnerFull instructions provided30 minutes523
Calculate Distance Using Ultrasonic Sensor and Surilli Basic

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 Basic
Surilli Basic
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Calculate Distance Using Ultrasonic Sensor (HC-SR04)

Code

Ultrasonic_Sensor

C/C++
const int trigPin = 6;
const int echoPin = 5;
// 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
SerialUSB.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
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
SerialUSB.print("Distance: ");
SerialUSB.println(distance);
}

Credits

Surilli

Surilli

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

Comments