Ultrasonic Distance Sensor HC-SR04 interfacing with Arduino Uno

 Ultrasonic Distance Sensor HC-SR04

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:

  1. Ultrasonic Distance Sensor.
  2. Arduino Uno.
  3. Breadboard.
  4. Connecting Wires.

Procedure:

  1. First of all, connect the Ultrasonic sensor to the breadboard. 
    Ultrasonic Distance Sensor

    Ultrasonic Distance Sensor

     
  2. Connect the Vcc pin to the 5v pin of Arduino (red wire). 
    Ultrasonic Distance Sensor

  3. Connect the GND pin of the sensor to the GND pin of Arduino (black wire). 
    Ultrasonic Distance Sensor

  4. Connect the trigger pin to the 9th digital pin of the Arduino (orange wire). 
    Ultrasonic Distance Sensor

  5. Now finally connect the echo pin to the 10th pin of the Arduino (green wire). 
    Ultrasonic Distance Sensor

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!

Post a Comment

0 Comments