How its working
It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.
generating ultrasonic sound we need to set trig pin high for 10 micro seconds.It will send 8 cycle sonic burst at speed of sound. Finally It will received at echo pin. get time that sound wave travels.
For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.
v=340 m/s
v=0.0340 cm/micro s.
time=distance x speed
time= 10/0.034 micro seconds
need to dived by 2
time =294 micro seconds
distance = time / 0.034 (cm)
code for getting distance form object
const int trigPin=10;
const int echoPin=11;
long duration;
int distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
//pulseIn == read travel time
duration= pulseIn(echoPin,HIGH);
distance =duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(1000);
}
No comments:
Post a Comment