Have you ever needed input from the real world to your robot or data collection system? Then an ultrasonic sensor is the right choice for your system.

Ultrasonic sensing is one of the best ways to sense proximity and detect levels with high reliability.

What is an ultrasonic sensor?

An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves.

The HC-SR04 offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package from 2 cm to 400 cm or 1” to 13 feet.

It is a device that can measure the distance to an object by using sound waves like bats do. It measures distance by sending out a sound wave at a specific frequency and listening for that sound wave to bounce back.

By recording the elapsed time between the sound wave being generated and the sound wave bouncing back, it is possible to calculate the distance between the sonar sensor and the object.

They can be divided into three broad categories:

  • Transmitters
  • Receivers
  • Transceivers
  • Transmitters convert electrical signals into ultrasound, receivers convert ultrasound into electrical signals, and transceivers can both transmit and receive ultrasound.

    Why use an Ultrasonic Sensor?

    Ultrasound is reliable in any lighting environment and can be used inside or outside. Ultrasonic sensors can handle collision avoidance for a robot, and are moved often, as long as it isn’t too fast.

    They are so widely used, they can be reliably implemented in grain bin sensing applications, water level sensing, drone applications and sensing cars at your local drive-thru restaurant or bank.

    Ultrasonic Sensors are best used in the non-contact detection of:

  • Presence
  • Level
  • Position
  • Distance
  • Non-contact sensors are also referred to as proximity sensors. Ultrasonic are Independent of:

  • Light
  • Smoke
  • Dust
  • Color
  • Material (except for soft surfaces, i.e. wool, because the surface absorbs the ultrasonic sound wave and doesn’t reflect sound.)
  • Ultrasonic Sensor Pin Configuration

    Pin Number Pin Name Description
    1 Vcc The Vcc pin powers 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 a US wave.
    3 Echo Echo pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back to the sensor.
    4 Ground This pin is connected to the Ground of the system.

    HC-SR04 Sensor Features

  • Operating voltage: +5V
  • Theoretical Measuring Distance: 2cm to 450cm
  • Practical Measuring Distance: 2cm to 80cm
  • Accuracy: 3mm
  • Measuring angle covered: <15°
  • Operating Current: <15mA
  • Operating Frequency: 40Hz
  • How Does an Ultrasonic Sensor Work?

    Ultrasonic sensors work by radiating sound waves at a frequency too high for humans to hear.

    They then wait for the sound to be reflected back, calculating distance based on the time required.

    This is similar to how radar measures the time it takes a radio wave to return after hitting an object.

    What are Ultrasonic Sensors Used for?

    So where can we use this sensor? I bet you are wondering this same question in your head! Robot navigation comes to mind, as well as factory automation.

    Water-level sensing is another good use, and can be accomplished by positioning one sensor above a water surface.

    Another aquatic application is to use these sensors to “see” the bottom of a body of water, traveling through the water, but reflecting off the bottom surface below.

    Here are other applications that ultrasonic sensor can be used in:

  • Used to avoid and detect obstacles with robots like biped robot, obstacle avoider robot, path finding robot etc.
  • Used to measure the distance within a wide range of 2cm to 400cm
  • Can be used to map the objects surrounding the sensor by rotating it
  • Depth of certain places like wells, pits, Tank Level etc can be measured since the waves can penetrate through water


    Connecting ultrasonic sensor to Arduino board

    Arduino codes

          	int trigger=3; //” trigger” on pin 7. 
          	int echo=2; //” echo” on pin 6. 
          	long time=0; 
          	long dist=0; 
          	void setup(){ 
          	  Serial.begin (9600); 
          	  pinMode(trigger, OUTPUT); 
          	  pinMode(echo, INPUT);        
         	void loop() { 
         	   digitalWrite(trigger, LOW); 
         	   digitalWrite(trigger, HIGH); 
         	   digitalWrite(trigger, LOW); 
         	   time = pulseIn(echo, HIGH);
         	   dist = (time/2) / 29.1; 
         	   if (dist >= 500 || dist <= 0)
        		    Serial.println("No measurement");   
        	   else { 


    Ultrasonic sensors are greater to infrared sensors because they aren’t affected by smoke or black materials, however, soft materials which don’t reflect the sonar (ultrasonic) waves very well may cause issues.

    Ultrasonic sensors are a reliable, cost-effective solution for distance sensing, level, and obstacle detection.

    Once you understand how ultrasonic sensors work and what ultrasonic technology is perfect for and not so good for, you can make a more educated decision on the right sensor system for your application.