The Best DIY STEM Tutorials and Projects

OVERVIEW
Discover how Ultrasonic Sensors work with Arduino! This sensor uses sound waves to measure distance, just like a bat finding its way in the dark. With this project, you can make your Arduino “see” without eyes! 🦇📏🚀
You can follow this video and the instructions below.
Video
Components Required
Here is a list of Components that we will need to make this Project.

Sr # | Item Name | Quantity |
---|---|---|
1 | Arduino Uno | 1 |
2 | Breadboard | 1 |
3 | Ultrasonic sensor | 1 |
4 | Arduino cable | 1 |
5 | M-F Jumper Wires | 4 |
Wiring Diagram
Here is a Complete Wiring Diagram along with Instructions for this Project

Program / Code
Code is Explained in the Comments of the Code
const int trigPin = 3; const int echoPin = 6; const int distanceThreshold = 5; // cm void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { int distance = calculateDistance(); Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); if (distance <= distanceThreshold) { Serial.println("Object detected within threshold"); } delay(1000); // Print data every 1 second } int calculateDistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); int duration = pulseIn(echoPin, HIGH); int distance = duration * 0.034 / 2; return distance; }
Thank you so much for going through our tutorial, we hope it was easy to follow and you enjoyed it, please share your feedback and pictures of the project (if you have made it) in the comments below .!