Ultrasonic Distance Sensor HC-SR04
Ultrasonic Distance Sensor
As the name suggests Ultrasonic distance sensor is used for measuring the
distance with the help of Sonar.
It emits a wave at a frequency of 40000hz. The wave travels in the air and
strikes the respective object and bounces back to the receiver in order to get
the distance.
An Ultrasonic sensor has four pins a VCC pin, trigger pin, echo pin, and a GND
pin.
Apparatus Required:
- Ultrasonic Distance Sensor.
- Arduino Uno.
- Breadboard.
- Connecting Wires.
Procedure:
-
First of all, connect the Ultrasonic sensor to the breadboard.
-
Connect the Vcc pin to the 5v pin of Arduino (red wire).
-
Connect the GND pin of the sensor to the GND pin of Arduino (black
wire).
-
Connect the trigger pin to the 9th digital pin of the Arduino (orange
wire).
-
Now finally connect the echo pin to the 10th pin of the Arduino (green
wire).
Working principle of Ultrasonic Distance Sensor:
First of all, we will keep the trigger pin at High for a few microseconds. After that, we will set the trigger pin Low and as soon as it is low it sends a pulse and sets the echo pin to High. As soon as the wave comes back it sets the echo pin to low. Finally, we need to measure the time for which the echo pin was High to get the actual distance.
Sketch:
const int trigPin = 9;
const int echoPin = 10;
float duration, 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*.0343)/2;
Serial.print("Distance: ");
Serial.println(String(distance)+"cm");
delay(1000);
}
Thank you!
0 Comments