Objective:
To measure the distance of the object present
Components Required
- Arduino Uno
- Ultrasound Sensor
Connection Diagram
Program
int trigPin = 2, echoPin = 3;
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print(“Distance: “);
Serial.print(distance);
Serial.println(” cm”);
delay(1000);
}
Working
The ultrasound sensor has a Transmitter and a Receiver. The transmitter transmits the ultrasound wave and the receiver used to receive the wave reflected by the object in front of it. The time difference between the Transmitted and the Received wave is calculated and also the velocity of the sound wave is 340 m/s. So that the distance of the object can be easily calculated using the formulae
Distance = Velocity x Time
Output
Distance of the object : 100