What is ultrasonic sensor? How to use ultrasonic sensor?

Ultrasonic sensors are used around the world, indoors and outdoors in the harshest conditions, for a variety of applications.Ultrasonic sensors have been used around for decades,only because of their capabilities, flexibility, and low cost., they continue to dominate the sensing business. With their integration in robotics, autonomous cars, and drones, demand has grown even more and more. This tutorial was written to provide an overview of ultrasonic sensing, its concepts, and how ultrasonic sensors function.

Introduction to Ultrasonic Sensor – Advanced Millennium Technologies

Introduction to Ultrasonic Sensor

An ultrasonic sensor is an electronic device that uses ultrasonic sound waves to detect the distance between a target item and converts the reflected sound into an electrical signal. Ultrasonic waves travel quicker than audible sound waves (i.e. the sound that humans can hear). Ultrasonic sensors have two main components: the transmitter (which emits the sound using piezoelectric crystals) and the receiver (which encounters the sound after it has travelled to and from the target).

The image below shows the HC-SR-04 ultrasonic sensor which has a transmitter, receiver. 

Ultrasonic Sensor HC-SR04 uses sonar to measure the distance of the object. It emits an ultrasonic wave at 40 kHz which will go and bounce back after hitting an object. By knowing the time travelled and the speed of the sound, we can calculate the distance (speed=distance/time). Ultrasonic waves are much faster than regular sound and as humans, we won’t be able to hear them. (Some animals and fishes like dolphins and bats are able to use it for communication and seeing in the dark).

An ultrasonic sensor detects and measures the distance of a target object using ultrasonic sound waves and converts the reflected sound into an electrical signal. This sensor has 4 pins which are VCC, TRIG, ECHO, GND. The two main components of this sensor are the transmitter and receiver. Hence the transmitter produces the sound and the receiver receives it. The voltage pin VCC connected to 5V and the GND connected to the ground and the trig and echo connected to the GPIO pins.


  • VCC: +5 V supply
  • TRIG:  Trigger input of the sensor. Microcontroller applies 10 us trigger pulse to the HC-SR04 ultrasonic module.
  • ECHO: Echo output of the sensor. Microcontroller reads/monitors this pin to detect the obstacle or to find the distance.
  • GND : Ground


  • Sensor Type: Ultrasonic
  • Output: Digital Sensor
  • Voltage: 5VDC
  • Operating Current: 15mA
  • Measure Angle: 15°
  • Ranging Distance: 2cm – 4m
  • Static current: < 2mA
  • Level output: high-5V
  • Output Signal:5V
  • High Precision: Up to 2 mm
  • Input Trigger Signal: 10µs TTL impulse
  • Echo Signal: Output TTL PWL Signal
  • High sensitivity ultrasonic range sensor
  • Works with Arduino board


The sensor sends an ultrasonic pulse out at 40kHz which travels through the air and if there is an obstacle or object, it will bounce back to the sensor. By calculating the travel time and the speed of sound, the distance can be calculated.

A transmitter and a receiver are required for an ultrasonic sensor. Which are arranged side by side as near together as possible in the most common layout. Sound travels in a straighter line from the transmitter to the detected object and back to the receiver when the receiver is close to the transmitter, resulting in reduced measurement errors. There are other ultrasonic transceivers that combine the transmitter and receiver functions into a single unit, lowering the PCB footprint and minimising error as much as practically possible.


To generate the ultrasound, the Trig set to a high state for a time of 10 µs. In response to it, the sensor transmits an 8 cycle sonic burst at 40KHz. This will travel at the speed of sound and it will be received in the Echo pin. The Echo pin goes high and will output the time in microseconds of the sound wave travelled. As soon as the signal is received, the echo pin goes low.

The microcontroller interprets the time signal into distance using the following functions:

The TRD (time/rate/distance) measurement formula can be used to compute the distance. Here, the calculated distance is the distance traveled from the ultrasonic transducer to the object and back to the transducer. The actual distance between the transducer and the object can be calculated by dividing this distance by two. Ultrasonic waves move at the speed of sound (343 metres per second at 20 degrees Celsius). The distance between the object and the sensor is half that of the sound wave’s journey. The distance to an object placed in front of an ultrasonic sensor is calculated using the following equation:


Below, the image shows the interfacing of the ultrasonic sensor with Arduino. The components required for the interfacing are,

  • Arduino Uno
  • HC-SR04-Ultrasonic Sensor
  • Connecting wires

Interfacing of ultrasonic sensor with arduino Source:

Kindly download the Arduino IDE for your system and upload the below code in your Arduino Uno,

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variableslong duration; // variable for the duration of sound wave travelint distance; // variable for the distance measurement
void setup() {  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed  Serial.println(“Ultrasonic Sensor HC-SR04 Test”); // print some text in Serial Monitor  Serial.println(“with Arduino UNO R3”);}void loop() {  // Clears the trigPin condition  digitalWrite(trigPin, LOW);  delayMicroseconds(2);  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds  digitalWrite(trigPin, HIGH);  delayMicroseconds(10);  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; // Speed of sound wave divided by 2 (go and back)  // Displays the distance on the Serial Monitor  Serial.print(“Distance: “);  Serial.print(distance);  Serial.println(” cm”);}


You can see the output on the serial monitor like,



  • Liquid level detection and control on production lines
  • Stacking and height control in warehouse facilities
  • Irregular parts detection on production lines
  • Counting people and people detection
  • Detecting breaks in wires or threads
  • Tank level detection
  • Box sorting for sizes and shapes
  • Robotics applications
  • High speed counting of objects
  • Car wash systems


Ultrasonic sensors are a well-known technology that is still widely used in a variety of industrial and consumer applications. They’re an excellent choice for many new products that require presence detection or distance measuring because of their simplicity, low cost, and sturdy construction.Hope this article helps you to understand the applications and ultrasonic sensor working principle.

Leave a Reply

Your email address will not be published.