First of all, the theory:
If we know that the sound take a time t to reach the obstacle and come back and that
speed s is equal to s=d/t where d is the distance then d=s*t .
We consider that speed of sound is 340.29 meters/seconds that is equal to
0.034029 centimeters/microseconds.
Now let's see the hardware parts, which is very simple:
To start measuring distance you just have to upload this code to the arduino
int trigPin = 2;
int echoPin = 4;
void setup() {
Serial.begin(9600);
}
void loop(){
long duration;
float cm;
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
float microsecondsToCentimeters(long microseconds){
return (microseconds*0.034029)/2;
}
Here is a video of me testing this project:
I hope you like this post, if you have any question please comment or send me an email to damianoandre@gmail.com
Bye,Dami
Hi.....nice work ....can we use 16x2 LCd to display the distance ..... if you have time please give code for LCD display ...regards, Ashok Shah ...ashok.shah16151@yahoo.com
RispondiEliminaHey bro how did you get the distance in computer
RispondiElimina