Distance Measurement Using Ultrasonic Sensor with Arduino

In this project we are going to measure distance using ultrasonic sensor with Arduino. Here ultrasonic sensor is a great distance measuring tool because this is measured the accurate distance without any type of touch or actual connect. In this Distance measuring tool is used at many several place like water level measurement.

Component Requirement of  distance measurement using ultrasonic sensor: –

Ultrasonic Sensor Module: –

The HC-SR04 ultrasonic sensor uses sonar to determine distance in range of 2cm-400cm and its accuracy is 3mm. The ultrasonic sensor module consists of transmitter, receiver and the control circuit. 

Feature of Ultrasonic Sensor: –

  • Power Supply: +5V DC
  • Quiescent Current:  <2mA
  • Working Current: 15mA
  • Effectual Angle: <15°
  • Ranging Distance: 2cm – 400 cm or 1″ – 13ft
  • Resolution: 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS

Pinout of Ultrasonic Sensor: –

DISTANCE MEASUREMENT USING ULTRASONIC SENSOR WITH ARDUINO

  • Vcc: – power supply pin
  • Trig: – input pin
  • Echo: – output pin
  • GND: – Ground pin

Circuit Diagram distance measurement using ultrasonic sensor: –

DISTANCE MEASUREMENT USING ULTRASONIC SENSOR WITH ARDUINO

Explanation of distance measurement using ultrasonic sensor: –

we will connect Ultrasonic Sensor Module with the Arduino. Connect the Sensor pins to the Arduino as follows 

Distance Measurement Using Ultrasonic Sensor with ArduinoPositive terminal of Red Led is connect to Arduino pin 4 and 560-ohm resistance to both negative terminal of Led and ground power rail.

How it’s Works: –

The ultrasonic sensor is connected to Arduino as given in the above table. Ultrasonic Sensor emits an ultrasound at 40 000 Hz which travels through the air and there is presented an object or obstacle on the path. The ultrasound   will bounce back to the module.  We can calculate the total distance using the travel time and speed of the sound.

DISTANCE MEASUREMENT USING ULTRASONIC SENSOR WITH ARDUINO

In order the ultrasonic sensor is generated sonic signal. The trigger pin set on the high state for 10us. Then the sonic signal will send out an 8-cycle sonic burst which will travel at the speed of sound and it will be received in the Echo pin which will output the time in microsecond the sound travelled.

DISTANCE MEASUREMENT USING ULTRASONIC SENSOR WITH ARDUINO

Calculation of Distance using distance measurement using sensor: –

So, we can calculate distance by using given formula:

         Distance= (travel time/2) * speed of sound

         Speed of sound(S)= 340m/sec

#define trigPin 13
#define echoPin 12
#define led 4

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4)
 { 
 digitalWrite(led,HIGH); 
}
  else {
   digitalWrite(led,LOW);
  }
  if (distance >= 200 || distance <= 0)
{
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.