Measuring Distance with ultrasonic Sensor

in this 4th tutorial we are going to interface Ultrasonic sensor with arduino.

WHAT IS ULTRASONIC SENSOR?

As the name indicates, ultrasonic sensors measure distance by using ultrasonic waves.
The sensor head emits an ultrasonic wave and receives the wave reflected back from the target. Ultrasonic Sensors measure the distance to the target by measuring the tim between the emission and reception.


    An optical sensor has a transmitter and receiver, whereas an ultrasonic sensor uses a single ultrasonic element for both emission and reception. In a reflective model ultrasonic sensor, a single oscillator emits and receives ultrasonic waves alternately. This enables miniaturization of the sensor head.
The distance can be calculated with the following formula:

Distance L = 1/2 × T × C

where L is the distance, T is the time between the emission and reception, and C is the sonic speed. (The value is multiplied by 1/2 because T is the time for go-and-return distance.)
Circuit Diagram:-

Code:-

// subcribe to AMAZING MJ youtube channel for more project tutorials
//This code is for measuring distance by using ultrasonic sensor
//Visit the website www.amazingmj.blogspot.com
//video link:- 

/********************************CODE***************************************/


int trigPin = 11;    //Trig 
int echoPin = 10;    //Echo 
long duration, cm, inches;  

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

void loop()
{   
  digitalWrite(trigPin, LOW);   
  delayMicroseconds(5);   
  digitalWrite(trigPin, HIGH);   
  delayMicroseconds(10);   
  digitalWrite(trigPin, LOW);
      
  pinMode(echoPin, INPUT);   
  
  duration = pulseIn(echoPin, HIGH);  
  cm = (duration/2) / 29.1;  
  inches = (duration/2) / 74;  
      
  Serial.print(inches); 
  Serial.print("in, ");  
  Serial.print(cm);  
  Serial.print("cm");  
  Serial.println();
  delay(250);


follow me on social media
instagram :-  @amazing_mj_official

Comments