Ultrasonic Sensor


The ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It measure distance by sending out a sound wave at specific frequency and listening for that sound wave to bounce back. By recording the time between the sound being generated and the sound bouncing back it is possible to calculate the distance between sensor and object using this formula below


Distance=Time*speed of sound divided/2  (as it travelled in two ways)


Pin Configuration:


1)VCC: it power the sensor typically with +5V

2)Trigger: Trigger pin is an Input pin . This pin has to be kept high for 10us to initialize measurement by sending ultrasonic waves. It will connected analog pin of arduino.

3)Echo: Echo pin is an output pin. This pin goes high for a period of time which will equal to the time taken for the Ultrasonic waves to return back to the sensor. It will connected to analog pin of arduino.

4)GND: This pin is connected to ground of the system.



Interfacing With Arduino 



const int trigPin = 9;

const int echoPin = 10;

// defines variables

long duration;

int distance;

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication


void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);


// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

// Prints the distance on the Serial Monitor

Serial.print(“Distance: “);







  1. It is used to avoid and detect obstacles with robot like obstacle avoider robot, line follower robot etc.
  2. Used to measure the distance within a wide range of 2cm o 400cm
  3. Can be used to map the object surroundings the sensor by rotating it.
  4. Depth of certain places like wells, pits etc can be measures since the waves can penetrate through water.



  1. It has sensing capability to sense all the material type.
  2. This sensor is not affected due to atmospheric dust, rain, snow etc.
  3. it has higher sensing distance compare to other proximity sensor type.
  4. It provide good readings in sensing large sized objects with hard surfaces.



  1. It is very sensitive to variation in the temperature.

2. It has more difficulties in reading reflection from soft, curved, and small objects