Distance Finder
Hello Friends,
In this tutorial, I am going to show you how to interface ultra sonic sensor with Arduino to measure distance and print in serial monitor. So we need to complete 3 section. First to complete the circuit. Then we need to write the code for the project. And final section is to test the project. So let's move.
Required Items
Connection
Now we need to connect the ultra sonic sensor to Arduino. So first attach the sensor to the breadboard. Then follow the connections :
- VCC to 5V
- GND to GND
- Trig to D7
- Echo to D8
After completing circuit let's move to the coding section.
Coding
So guys let's start coding. This is the code for the project.
#define trigPin 7 #define echoPin 8 float duration; float distance; void setup() { Serial.begin(9600); pinMode(trigPin,OUTPUT); pinMode(echoPin,INPUT); } void loop() { digitalWrite(trigPin,HIGH); delay(10); digitalWrite(trigPin,LOW); duration = pulseIn(echoPin,HIGH); distance = (duration/2)* 0.034; Serial.print("Distance "); Serial.print(distance); Serial.println(" cm"); delay(250); }
If you want to download the code click on the download button.
After coding let's upload it to the Arduino board.
Downloads
Test the Project
Now all the work is done. The last step is to test the project and make sure whether it works or not.So guy's if you like this project please support and follow me. Thanks for reading this tutorial. And if you have any doubts please leave a comment and feel free to contact me.